在 Ubuntu 上配置 Node.js 的软件源,通常有两种常见方式:
NodeSource 提供了较新版本的 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 -
其他版本替换数字即可:
- 18.x
- 20.x
- 22.x
sudo apt install -y nodejs
node -v
npm -v
sudo apt update
sudo apt install -y nodejs npm
查看版本:
node -v
通常版本较低,不推荐用于新项目
如果你不想动系统源,可以用 nvm 管理多个 Node 版本。
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install --lts
npm config set registry https://registry.npmmirror.com
查看:
npm config get registry
如果你是 服务器部署 还是 本地开发,我可以给你更具体的建议。