在 Ubuntu 下更新 Node.js 版本,你可以选择使用 NodeSource 存储库或者 NVM(Node Version Manager)。下面是两种方法的详细步骤:
方法一:使用 NodeSource 存储库
sudo apt-get remove --purge nodejs
sudo apt-get autoremove
sudo apt-get update
sudo apt-get install curl software-properties-common
maverick
替换为你需要的 Node.js 版本代号。例如,如果你想安装最新的 LTS 版本,可以使用 nodist
:curl -fsSL https://deb.nodesource.com/setup_maverick.x | sudo -E bash -
sudo apt-get install -y nodejs
node -v
npm -v
方法二:使用 NVM(Node Version Manager)
sudo apt-get remove --purge nodejs
sudo apt-get autoremove
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
source ~/.bashrc
nvm install --lts
nvm use --lts
node -v
npm -v
现在你已经成功地在 Ubuntu 上更新了 Node.js 版本。如果你想切换到其他版本,只需使用 nvm use <version>
命令。