Ubuntu 22.04 上升级 Node.js 版本
在 Ubuntu 22.04 上升级 Node.js 版本有几种方法,推荐使用 NVM(Node Version Manager),因为它可以让你轻松管理多个 Node.js 版本。
方法 1: 使用 NVM(推荐)
1. 安装 NVM
# 下载并安装 NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# 或者使用 wget
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
2. 重启终端或重新加载配置
source ~/.bashrc # 或者 exec bash
3. 验证 NVM 安装
nvm --version
4. 查看可用的 Node.js 版本
# 查看所有可用版本
nvm list-remote
# 查看 LTS 版本
nvm list-remote --lts
5. 安装和使用 Node.js
# 安装最新的 LTS 版本
nvm install --lts
# 安装特定版本(比如 18.19.0)
nvm install 18.19.0
# 使用特定版本
nvm use 18.19.0
# 设置默认版本
nvm alias default 18.19.0
# 查看已安装的版本
nvm list
方法 2: 使用 NodeSource PPA
1. 添加 NodeSource 仓库
# 对于 Node.js 20.x
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
# 对于 Node.js 18.x
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
2. 安装 Node.js
sudo apt-get update
sudo apt-get install -y nodejs
方法 3: 使用 Snap
# 安装最新版本
sudo snap install node --classic
snap install node --classic --channel=18/stable

