温馨提示×

Debian怎么安装Nodejs环境

小樊
46
2026-07-06 18:04:40
栏目: 编程语言

Debian 上安装 Node.js 有多种方式,下面按推荐程度给你介绍几种常见方法,你可以根据需求选择。


✅ 方法一(推荐):使用 NodeSource 安装最新 LTS(官方推荐)

适合需要较新 Node.js 的用户。

1️⃣ 更新系统

sudo apt update
sudo apt upgrade -y

2️⃣ 安装依赖

sudo apt install -y curl ca-certificates gnupg

3️⃣ 导入 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

4️⃣ 添加 NodeSource 仓库(以 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

5️⃣ 安装 Node.js

sudo apt update
sudo apt install -y nodejs

6️⃣ 验证

node -v
npm -v

✅ 优点:版本新、官方支持
✅ 适合生产环境


✅ 方法二:使用 Debian 官方仓库(最简单)

适合不追求最新版本的用户。

sudo apt update
sudo apt install -y nodejs npm
node -v

⚠️ 缺点:

  • Debian 仓库中的 Node.js 版本通常较旧
  • 不建议用于现代前端 / 后端开发

✅ 方法三:使用 NVM(推荐给开发者)

适合多版本切换、本地开发环境。

1️⃣ 安装 NVM

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

重新加载 shell:

source ~/.bashrc

2️⃣ 安装 Node.js

nvm install --lts
nvm use --lts

3️⃣ 验证

node -v
npm -v

✅ 优点:

  • 可切换多个 Node 版本
  • 不影响系统环境

✅ 方法四:使用 Snap(不推荐但可用)

仅在你已经使用 Snap 时考虑:

sudo apt install -y snapd
sudo snap install node --classic --channel=20/stable

✅ 推荐总结

场景 推荐方式
服务器 / 生产环境 ✅ NodeSource
本地开发 / 多版本 ✅ NVM
快速测试 Debian 官方源
不推荐 Snap

如果你愿意,可以告诉我:

  • Debian 版本(如 11 / 12)
  • 用途(前端 / 后端 / Docker / 服务器)

我可以给你最合适的一套安装方案

0