在 Ubuntu 上升级 Node.js 版本,你可以选择使用 NodeSource 存储库或者 NVM(Node Version Manager)。下面是两种方法的详细步骤:
方法一:使用 NodeSource 存储库
sudo apt-get remove nodejs
sudo apt-get autoremove
maverick
替换为你的 Ubuntu 版本代号(例如:bionic、focal 等):curl -fsSL https://deb.nodesource.com/setup_maverick.x | sudo -E bash -
sudo apt-get install -y nodejs
node --version
npm --version
方法二:使用 NVM(Node Version Manager)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
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
source ~/.bashrc
nvm install node
nvm use node
node --version
npm --version
现在你已经成功升级了 Ubuntu 上的 Node.js 版本。如果你想切换到其他版本,只需使用 nvm use <version>
命令。