Ubuntu 安装 Node.js 指定版本指南
在没有安装 NVM 的服务器环境中(如 Docker、CI/CD、虚拟机等),建议使用 Node.js 官方的二进制包源(PPA)来快速安装特定版本的 Node.js。本指南将展示如何在 Ubuntu/Debian 系统中通过 NodeSource 官方 PPA 安装 Node.js 的 LTS 与最新版本。
安装 Node.js 20(当前活跃 LTS)
适合最新项目开发和维护:
# 1. 添加 NodeSource 源
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
# 2. 安装 Node.js 20
sudo apt-get install -y nodejs
# 3. 验证安装
node -v
npm -v
安装 Node.js 18(长期支持版本 LTS)
适合已有系统兼容性较好的 LTS 项目:
# 1. 添加 NodeSource 源
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
# 2. 安装 Node.js 18
sudo apt-get install -y nodejs
# 3. 验证安装
node -v
npm -v
安装 Node.js 16(维护阶段的旧 LTS)
适合旧系统或历史项目兼容需求:
# 1. 添加 NodeSource 源
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
# 2. 安装 Node.js 16
sudo apt-get install -y nodejs
# 3. 验证安装
node -v
npm -v
安装 Node.js 22(当前最新版,非 LTS)
适合测试新特性、前沿技术栈:
# 1. 添加 NodeSource 源
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
# 2. 安装 Node.js 22
sudo apt-get install -y nodejs
# 3. 验证安装
node -v
npm -v
安装前准备(可选但推荐)
安装 curl 工具(如未安装):
sudo apt-get update
sudo apt-get install -y curl


