在 Ubuntu 上设置(安装并配置)Node.js,常见有几种方式。下面按推荐程度从高到低说明。
sudo apt update
sudo apt upgrade -y
sudo apt install -y curl software-properties-common
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
其他版本替换
20.x即可,如18.x、22.x
sudo apt install -y nodejs
node -v
npm -v
sudo apt update
sudo apt install -y nodejs npm
查看版本:
node -v
⚠️ 版本通常较老,不推荐用于生产环境
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
node -v
npm -v
npm config set registry https://registry.npmmirror.com
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
sudo apt remove nodejs npm
sudo apt autoremove
nvm uninstall 20
如果你告诉我:
我可以给你最合适的具体方案。