温馨提示×

Debian如何升级Nodejs版本

小樊
34
2026-07-15 03:50:17
栏目: 编程语言

在 Debian 系统上升级 Node.js 有多种方式,推荐使用 NodeSourcenvm。下面按常见场景给你详细说明。


一、先查看当前 Node.js 版本

node -v
npm -v

二、方式一(推荐):使用 NodeSource 升级(系统级)

适合服务器、生产环境。

1️⃣ 卸载旧版本(可选)

sudo apt remove nodejs npm -y
sudo apt autoremove -y

2️⃣ 安装依赖

sudo apt update
sudo apt install -y curl

3️⃣ 添加 NodeSource 源(以 Node 20 为例)

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

可替换版本号:

  • setup_18.x
  • setup_20.x
  • setup_21.x

4️⃣ 安装 Node.js

sudo apt install -y nodejs

5️⃣ 验证

node -v
npm -v

✅ 优点:稳定、适合服务器
❌ 缺点:切换版本不如 nvm 方便


三、方式二(最灵活):使用 nvm 升级(用户级)

适合开发环境、需要多版本切换。

1️⃣ 安装 nvm

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

重新加载 shell:

source ~/.bashrc
# 或
source ~/.zshrc

2️⃣ 查看可安装版本

nvm ls-remote

3️⃣ 安装指定版本

nvm install 20

或安装最新 LTS:

nvm install --lts

4️⃣ 设置默认版本

nvm alias default 20

5️⃣ 验证

node -v

✅ 优点:多版本共存、切换方便
❌ 缺点:仅对当前用户生效


四、方式三:使用 npm 的 n 模块(不推荐新手)

npm install -g n
sudo n lts

⚠️ 在 Debian 上容易和 apt 安装的 Node 冲突,不推荐。


五、常见问题

❓ 升级后 node -v 没变?

which node

可能是 PATH 问题,nvm 用户请确认:

source ~/.bashrc

❓ Debian 自带 Node 太旧?

Debian 官方源 Node 版本通常很旧,强烈建议用 NodeSource 或 nvm


六、推荐方案总结

场景 推荐方式
服务器 / 生产环境 ✅ NodeSource
本地开发 ✅ nvm
临时测试 n

如果你愿意,可以告诉我:

  • Debian 版本(如 11 / 12)
  • 当前 Node 版本
  • 是否用于生产环境

我可以给你最合适的具体升级命令

0