Skip to content

在 Linux 上安装 fnm

fnm 在 Linux 上有多种安装方式,本指南将帮助你在各种 Linux 发行版上安装 fnm。

安装方式

使用 curl 脚本(推荐)

最简单的安装方式:

bash
curl -fsSL https://fnm.vercel.app/install | bash

安装后,按照提示重新加载 Shell 或手动添加到 PATH:

bash
export PATH="$HOME/.fnm:$PATH"
eval "$(fnm env --use-on-cd)"

使用 Homebrew

如果你在 Linux 上安装了 Homebrew:

bash
brew install fnm

手动下载

  1. 访问 fnm Releases 页面
  2. 下载适合你架构的压缩包:
bash
# x86_64
wget https://github.com/Schniz/fnm/releases/latest/download/fnm-linux.zip

# ARM64
wget https://github.com/Schniz/fnm/releases/latest/download/fnm-arm64.zip
  1. 解压并安装:
bash
unzip fnm-linux.zip -d ~/.local/bin
chmod +x ~/.local/bin/fnm

各发行版安装指南

Ubuntu / Debian

bash
# 方法 1: 使用 curl 脚本
curl -fsSL https://fnm.vercel.app/install | bash

# 方法 2: 手动安装
sudo apt update
sudo apt install -y unzip
wget https://github.com/Schniz/fnm/releases/latest/download/fnm-linux.zip
sudo unzip fnm-linux.zip -d /usr/local/bin
sudo chmod +x /usr/local/bin/fnm

CentOS / RHEL / Fedora

bash
# 安装依赖
sudo dnf install -y unzip  # Fedora
# 或
sudo yum install -y unzip  # CentOS/RHEL

# 使用 curl 脚本
curl -fsSL https://fnm.vercel.app/install | bash

# 或手动安装
wget https://github.com/Schniz/fnm/releases/latest/download/fnm-linux.zip
sudo unzip fnm-linux.zip -d /usr/local/bin
sudo chmod +x /usr/local/bin/fnm

Arch Linux

bash
# 使用 AUR helper
yay -S fnm-bin

# 或从 AUR 构建
git clone https://aur.archlinux.org/fnm-bin.git
cd fnm-bin
makepkg -si

Alpine Linux

bash
# 安装依赖
apk add bash curl unzip

# 使用 curl 脚本
curl -fsSL https://fnm.vercel.app/install | bash

# 或手动安装
wget https://github.com/Schniz/fnm/releases/latest/download/fnm-linux.zip
unzip fnm-linux.zip -d ~/.local/bin
chmod +x ~/.local/bin/fnm

配置 Shell

Bash

编辑 ~/.bashrc

bash
echo 'eval "$(fnm env --use-on-cd)"' >> ~/.bashrc
source ~/.bashrc

Zsh

编辑 ~/.zshrc

bash
echo 'eval "$(fnm env --use-on-cd)"' >> ~/.zshrc
source ~/.zshrc

Fish

编辑 ~/.config/fish/config.fish

fish
echo 'fnm env --use-on-c | source' >> ~/.config/fish/config.fish
source ~/.config/fish/config.fish

Shell 补全

Bash 补全

bash
# 安装 bash-completion(如果未安装)
sudo apt install bash-completion  # Debian/Ubuntu
sudo dnf install bash-completion  # Fedora

# 添加补全脚本
fnm completions --shell bash >> ~/.bashrc
source ~/.bashrc

Zsh 补全

bash
# 创建补全目录
mkdir -p ~/.zsh/completions

# 生成补全脚本
fnm completions --shell zsh > ~/.zsh/completions/_fnm

# 在 ~/.zshrc 中添加
fpath=(~/.zsh/completions $fpath)
autoload -U compinit && compinit

Fish 补全

bash
fnm completions --shell fish > ~/.config/fish/completions/fnm.fish

使用国内镜像

在中国大陆,建议配置镜像加速下载:

bash
# 在 Shell 配置文件中添加
export FNM_NODE_DIST_MIRROR=https://npmmirror.com/mirrors/node

# 或者在 eval 时指定
eval "$(fnm env --use-on-cd --node-dist-mirror=https://npmmirror.com/mirrors/node)"

常用镜像:

镜像源地址
npmmirrorhttps://npmmirror.com/mirrors/node
清华大学https://mirrors.tuna.tsinghua.edu.cn/nodejs-release
华为云https://mirrors.huaweicloud.com/nodejs

验证安装

bash
# 检查 fnm 版本
fnm --version

# 安装 Node.js
fnm install --lts

# 验证 Node.js
node --version
npm --version

常见问题

命令找不到

bash
# 检查 fnm 是否在 PATH 中
which fnm

# 手动添加到 PATH
export PATH="$HOME/.fnm:$PATH"

# 永久添加到 PATH
echo 'export PATH="$HOME/.fnm:$PATH"' >> ~/.bashrc

权限问题

bash
# 确保 fnm 有执行权限
chmod +x ~/.fnm/fnm
# 或
chmod +x /usr/local/bin/fnm

下载失败

bash
# 使用镜像
export FNM_NODE_DIST_MIRROR=https://npmmirror.com/mirrors/node
fnm install --lts

下一步

安装完成后,你可以:

相关链接