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

下一步

安裝完成後,你可以:

相關鏈接