在 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 卸載指南