温馨提示×

如何设debian nodejs源

小樊
56
2026-10-08 06:16:27
栏目: 编程语言

在 Debian 上配置 Node.js 的软件源,通常有两种常见方式:


方式一:使用 NodeSource 官方源(推荐)

NodeSource 提供了最新稳定版和长期支持版(LTS)的 Node.js 源。

1. 安装依赖工具

sudo apt update
sudo apt install -y curl ca-certificates gnupg

2. 导入 NodeSource GPG 密钥

sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key \
  | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg

3. 添加 Node.js 源(以 Node.js 20 LTS 为例)

NODE_MAJOR=20
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" \
  | sudo tee /etc/apt/sources.list.d/nodesource.list

可替换 NODE_MAJOR 为 18、22 等版本号

4. 更新并安装 Node.js

sudo apt update
sudo apt install -y nodejs

5. 验证

node -v
npm -v

方式二:使用 Debian 官方源(版本较旧)

sudo apt update
sudo apt install -y nodejs npm

⚠️ Debian 仓库中的 Node.js 通常版本较低,不推荐用于生产环境


方式三:使用 NVM(适合开发环境)

如果不想改系统源,也可以用 NVM 管理多版本 Node.js:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install 20

总结建议

  • 服务器/生产环境:NodeSource 源 ✅
  • 本地开发:NVM ✅
  • 快速测试:Debian 官方源 ⚠️

如你需要指定 Debian 版本(如 bullseye / bookworm)或离线配置,也可以告诉我。

0 踩