温馨提示×

怎么更新Ubuntu的Nodejs

小樊
53
2026-08-06 17:19:19
栏目: 编程语言

在 Ubuntu 上升级 Node.js,常见有 3 种方式,推荐按顺序选择:


✅ 方式一(推荐):使用 NodeSource(官方推荐,版本新、稳定)

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

sudo apt remove nodejs -y

2️⃣ 安装 NodeSource 指定版本(以 Node.js 20 为例)

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

支持版本:

  • 18.x(LTS)
  • 20.x(LTS,推荐)
  • 22.x(最新)

3️⃣ 安装 / 升级 Node.js

sudo apt install -y nodejs

4️⃣ 验证版本

node -v
npm -v

✅ 方式二:使用 nvm(适合开发者,切换版本方便)

1️⃣ 安装 nvm

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

然后执行:

source ~/.bashrc

2️⃣ 安装最新 LTS 版本

nvm install --lts

3️⃣ 设置默认版本

nvm use --lts
nvm alias default lts/*

4️⃣ 查看版本

node -v

优点

  • 不影响系统 Node
  • 可同时使用多个 Node.js 版本

✅ 方式三:使用 Snap(简单,但有时慢)

sudo snap refresh node --channel=20/stable

或安装:

sudo snap install node --classic --channel=20/stable

❌ 不推荐方式

  • 使用 apt install nodejs(Ubuntu 默认源版本非常旧)

✅ 推荐总结

场景 推荐方式
服务器 / 生产环境 NodeSource
开发 / 多版本切换 nvm
临时体验 Snap

如果你愿意,我可以:

  • 帮你 判断当前该用哪个版本
  • 给你 最小化冲突的升级方案
  • 或检查你系统里 是否已经装了多个 Node

直接把下面命令的结果发我:

node -v
which node

0