ubuntu

如何在Ubuntu中升级Node.js

小樊
44
2025-05-29 17:58:32
栏目: 编程语言

在 Ubuntu 中升级 Node.js 的方法如下:

方法一:使用 NodeSource 存储库

  1. 首先,删除已安装的 Node.js(可选):
sudo apt-get remove --purge nodejs
sudo apt-get autoremove
  1. 导入 NodeSource 存储库的 GPG 密钥:
curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -

这里,我们使用的是 Node.js 14.x 版本。如果你想安装其他版本,请将 setup_14.x 替换为相应的版本,例如 setup_12.x

  1. 更新软件包列表:
sudo apt-get update
  1. 安装 Node.js:
sudo apt-get install -y nodejs
  1. 检查 Node.js 和 npm 的版本:
node --version
npm --version

方法二:使用 NVM(Node Version Manager)

  1. 首先,卸载已安装的 Node.js(可选):
sudo apt-get remove --purge nodejs
sudo apt-get autoremove
  1. 安装 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
  1. 关闭并重新打开终端。运行以下命令以确保 NVM 安装成功:
nvm --version
  1. 使用 NVM 安装最新版本的 Node.js:
nvm install node
  1. 切换到最新版本的 Node.js:
nvm use node
  1. 检查 Node.js 和 npm 的版本:
node --version
npm --version

现在,你已经成功地在 Ubuntu 中升级了 Node.js。如果你想切换到其他版本的 Node.js,只需使用 nvm use <version> 命令。

0
看了该问题的人还看了