ubuntu中如何更新nodejs

1796
2025/4/1 9:31:59
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在 Ubuntu 系统中更新 Node.js 的方法有以下几种:

方法一:使用 NodeSource 仓库

  1. 首先,删除已安装的 Node.js(可选):
sudo apt-get remove --purge nodejs
sudo apt-get autoremove
  1. 添加 NodeSource 仓库。选择你需要的 Node.js 版本,例如 14.x:
curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
  1. 安装 Node.js:
sudo apt-get install -y nodejs
  1. 验证安装:
node -v
npm -v

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

  1. 安装 NVM:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

或者

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
  1. 激活 NVM:
source ~/.bashrc
  1. 安装最新版本的 Node.js:
nvm install node
  1. 切换到最新版本的 Node.js:
nvm use node
  1. 验证安装:
node -v
npm -v

方法三:使用 Ubuntu 软件包管理器(不推荐)

  1. 更新软件包列表:
sudo apt-get update
  1. 升级已安装的 Node.js:
sudo apt-get upgrade nodejs
  1. 验证安装:
node -v
npm -v

注意:使用此方法可能会导致 Node.js 版本不是最新的。建议使用 NodeSource 或 NVM 进行更新。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: Ubuntu如何开启SELinux