温馨提示×

debian nodejs环境如何配

小樊
41
2026-09-04 17:41:40
栏目: 编程语言

下面给你一份在 Debian 上配置 Node.js 环境的实用指南,从「最推荐」到「备选方案」都列出来,你可以按自己需求选。


一、推荐方式:用 NodeSource 安装(版本新、稳定)

适用于 Debian 10/11/12。

1. 更新系统

sudo apt update
sudo apt upgrade -y

2. 安装依赖

sudo apt install -y curl gnupg

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

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

想装 18:setup_18.x
想装 22:setup_22.x

4. 安装 Node.js

sudo apt install -y nodejs

5. 验证

node -v
npm -v

二、用 apt 直接装(最简单,但版本偏旧)

sudo apt install -y nodejs npm

⚠️ 缺点:Debian 官方源里的 Node 版本通常较老,不适合现代项目。


三、用 NVM 管理多版本(开发推荐 ✅)

适合需要切换 Node 版本的开发者。

1. 安装 NVM

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

2. 重新加载 shell

source ~/.bashrc

3. 安装 Node

nvm install 20
nvm use 20

4. 设为默认

nvm alias default 20

四、配置 npm(可选但推荐)

国内镜像(加速)

npm config set registry https://registry.npmmirror.com

查看配置

npm config get registry

五、常见问题

1. npm 命令不存在?

sudo apt install -y npm

2. 权限问题(不要 sudo npm)

mkdir ~/.npm-global
npm config set prefix ~/.npm-global
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

六、总结建议

使用场景 推荐方式
服务器部署 NodeSource
本地开发 NVM
快速测试 apt

如果你告诉我:

  • Debian 版本(cat /etc/os-release
  • 用途(服务器 / 开发 / 脚本)

我可以给你更精确的一步到位命令

0