温馨提示×

debian怎么配置nodejs

小樊
35
2026-09-02 07:55:53
栏目: 编程语言

在 Debian 上配置 Node.js 常见有 三种方式,推荐优先使用官方源或 NodeSource(版本新、可控)。下面按常用程度给你说明。


一、使用 NodeSource 安装(推荐)

适合需要指定 Node 版本(如 18 / 20 / 22)

1. 更新系统

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

2. 添加 NodeSource 源(示例:Node 20)

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

其他版本把 20.x 改成 18.x / 22.x 即可

3. 安装 Node.js

sudo apt install -y nodejs

4. 验证

node -v
npm -v

二、使用 Debian 自带仓库(简单但版本旧)

sudo apt update
sudo apt install -y nodejs npm

查看版本:

node -v

⚠️ Debian 稳定版中的 Node 通常较旧(如 12 / 18)


三、使用 NVM 安装(适合开发环境)

适合多版本切换

1. 安装 NVM

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

2. 重新登录或执行

source ~/.bashrc

3. 安装 Node

nvm install 20
nvm use 20

四、常见配置

1. 设置 npm 镜像(国内加速)

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

2. 全局目录权限(避免 sudo)

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

五、选择建议

场景 推荐方式
生产服务器 NodeSource
快速测试 apt
本地开发 NVM

如果你是 服务器部署 / Docker / 某个具体项目,可以告诉我,我给你更具体的配置方案。

0