在 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手动下载
- 访问 fnm Releases 页面
- 下载适合你架构的压缩包:
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- 解压并安装:
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/fnmCentOS / 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/fnmArch Linux
bash
# 使用 AUR helper
yay -S fnm-bin
# 或从 AUR 构建
git clone https://aur.archlinux.org/fnm-bin.git
cd fnm-bin
makepkg -siAlpine 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 ~/.bashrcZsh
编辑 ~/.zshrc:
bash
echo 'eval "$(fnm env --use-on-cd)"' >> ~/.zshrc
source ~/.zshrcFish
编辑 ~/.config/fish/config.fish:
fish
echo 'fnm env --use-on-c | source' >> ~/.config/fish/config.fish
source ~/.config/fish/config.fishShell 补全
Bash 补全
bash
# 安装 bash-completion(如果未安装)
sudo apt install bash-completion # Debian/Ubuntu
sudo dnf install bash-completion # Fedora
# 添加补全脚本
fnm completions --shell bash >> ~/.bashrc
source ~/.bashrcZsh 补全
bash
# 创建补全目录
mkdir -p ~/.zsh/completions
# 生成补全脚本
fnm completions --shell zsh > ~/.zsh/completions/_fnm
# 在 ~/.zshrc 中添加
fpath=(~/.zsh/completions $fpath)
autoload -U compinit && compinitFish 补全
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)"常用镜像:
| 镜像源 | 地址 |
|---|---|
| npmmirror | https://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下一步
安装完成后,你可以:
- 安装 Node.js - 使用 fnm 安装 Node.js
- 命令参考 - 了解所有可用命令
- 版本管理 - 学习版本管理
相关链接
- 安装 fnm - 通用安装指南
- Windows 安装 - Windows 安装指南
- macOS 安装 - macOS 安装指南
- Ubuntu 卸载 - Ubuntu 卸载指南