ubuntu

如何在ubuntu上升级nodejs版本

小樊
40
2025-05-11 13:30:39
栏目: 编程语言

在 Ubuntu 上升级 Node.js 版本,你可以选择使用 NodeSource 存储库或者 NVM(Node Version Manager)。下面是两种方法的详细步骤:

方法一:使用 NodeSource 存储库

  1. 首先,删除已安装的 Node.js:
sudo apt-get remove nodejs
sudo apt-get autoremove
  1. 安装 NodeSource 存储库。以下命令将添加适用于你的 Ubuntu 版本的 NodeSource 存储库。请将 maverick 替换为你的 Ubuntu 版本代号(例如:bionic、focal 等):
curl -fsSL https://deb.nodesource.com/setup_maverick.x | sudo -E bash -
  1. 安装最新版本的 Node.js:
sudo apt-get install -y nodejs
  1. 验证安装成功:
node --version
npm --version

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

  1. 安装 NVM。运行以下命令以安装 NVM:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
  1. 激活 NVM。在你的 shell 配置文件(例如:~/.bashrc、~/.zshrc 等)中添加以下行:
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
  1. 重新加载 shell 配置文件:
source ~/.bashrc
  1. 使用 NVM 安装最新版本的 Node.js:
nvm install node
  1. 切换到新安装的 Node.js 版本:
nvm use node
  1. 验证安装成功:
node --version
npm --version

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

0
看了该问题的人还看了