在 Ubuntu 上安装 Node.js 有多种方式,下面给你几种常用且推荐的方法,按场景选择即可。
适合:需要指定 Node.js 版本(如 18、20、22)
sudo apt update
sudo apt install -y curl
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
node -v
npm -v
适合:只是临时用一下
sudo apt update
sudo apt install -y nodejs npm
查看版本:
node -v
⚠️ Ubuntu 官方源中的 Node 版本通常偏旧
适合:需要多版本切换
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install 20
nvm use 20
nvm alias default 20
不要用 sudo npm -g
推荐:
npm config set prefix ~/.npm
which node
如果你告诉我:
我可以给你最合适的安装方案。