在 Ubuntu 中升级 Node.js 的方法如下:
方法一:使用 NodeSource 存储库
- 首先,删除已安装的 Node.js(可选):
sudo apt-get remove --purge nodejs sudo apt-get autoremove
- 导入 NodeSource 存储库的 GPG 密钥:
curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
这里,我们使用的是 Node.js 14.x 版本。如果你想安装其他版本,请将 setup_14.x
替换为相应的版本,例如 setup_12.x
。
- 更新软件包列表:
sudo apt-get update
- 安装 Node.js:
sudo apt-get install -y nodejs
- 检查 Node.js 和 npm 的版本:
node --version npm --version
方法二:使用 NVM(Node Version Manager)
- 首先,卸载已安装的 Node.js(可选):
sudo apt-get remove --purge nodejs sudo apt-get autoremove
- 安装 NVM。运行以下命令以在当前 shell 中安装 NVM:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
或者使用 wget:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
- 关闭并重新打开终端。运行以下命令以确保 NVM 安装成功:
nvm --version
- 使用 NVM 安装最新版本的 Node.js:
nvm install node
- 切换到最新版本的 Node.js:
nvm use node
- 检查 Node.js 和 npm 的版本:
node --version npm --version
现在,你已经成功地在 Ubuntu 中升级了 Node.js。如果你想切换到其他版本的 Node.js,只需使用 nvm use
命令。