温馨提示×

怎样在debian跑nodejs

小樊
38
2026-10-17 00:33:01
栏目: 编程语言

在 Debian 上运行 Node.js 一般有几种常见方式,下面按推荐程度给你说明。


一、推荐方式:用 NodeSource 安装官方新版本(最常用)

Debian 自带仓库里的 Node 通常比较旧,建议用 NodeSource。

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

二、简单方式:用 Debian 自带仓库(版本较旧)

sudo apt update
sudo apt install -y nodejs npm

适合:

  • 只是跑简单脚本
  • 不追求新特性

三、开发推荐:用 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

优点:

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

四、运行一个简单的 Node 程序

1. 写文件

nano hello.js

内容:

console.log("Hello Debian Node.js");

2. 运行

node hello.js

五、后台运行(生产环境常用)

node app.js &

或用:

  • pm2
  • systemd
  • docker

如果你愿意,我可以帮你:

  • 写 systemd 服务
  • 配置 Nginx + Node
  • 用 Docker 跑 Node
  • 选 服务器版本(18/20/22)

你现在是 桌面 Debian 还是服务器?用来做什么?

0 踩