在 Debian 上安装 Node.js 有多种方式,下面按推荐程度给你介绍几种常见做法。
适用于 Debian 10 / 11 / 12。
sudo apt update
sudo apt install -y curl
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
想要其他版本就改数字,比如
setup_18.x
sudo apt install -y nodejs
node -v
npm -v
适合对版本要求不高的场景。
sudo apt update
sudo apt install -y nodejs npm
查看版本:
node -v
⚠️ Debian 仓库里的 Node 通常比较老(比如 12 / 14)
适合开发环境,不影响系统 Node。
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
重新登录或执行:
source ~/.bashrc
nvm install 20
nvm use 20
node -v
npm -v
不要用 sudo npm -g
建议:
npm config set prefix ~/.npm-global
并加到 .bashrc:
export PATH=~/.npm-global/bin:$PATH
| 场景 | 推荐方式 |
|---|---|
| 服务器 | NodeSource |
| 本地开发 | nvm |
| 临时用 | apt |
如果你告诉我:
我可以直接给你一套完整命令。