温馨提示×

如何给 Debian 配 NodeJS 源

小樊
44
2026-09-22 21:31:57
栏目: 编程语言

在 Debian 上配置 Node.js 的软件源,通常有两种主流方式:使用 NodeSource 官方源(推荐,版本新且干净)或 使用 Debian 自身仓库(版本较旧但最稳定)。下面分别说明。


一、使用 NodeSource 源(推荐)

NodeSource 提供了各版本的 Node.js APT 源,适合需要新版本 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. 添加 NodeSource 源(以 Node 20 为例)

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

查看版本:

node -v

Debian 12 通常为 Node 18,Debian 11 为 Node 12


三、使用 NVM 管理 Node(可选,非系统源)

如果你不想动系统源,可以用 NVM:

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

四、常见问题

1. 切换 Node 版本

  • NodeSource:重加对应源并 apt install
  • NVM:nvm use 18

2. 清除旧 NodeSource 源

sudo rm /etc/apt/sources.list.d/nodesource.list
sudo apt update

如果你说明 Debian 版本(如 11/12)+ 需要的 Node 版本,我可以给你精确命令。

0 踩