Installing Node.js on Ubuntu: Choose APT or nvm

Installing Node.js on Ubuntu: Choose APT or nvm

The right way to install Node.js on Ubuntu depends on whether you need a simple system package or control over Node versions per project. APT is straightforward, while nvm is useful when projects require different versions. Whichever route you choose, check what your shell actually runs before installing another copy.

在 Ubuntu 上安装 Node.js 的正确方式取决于你是需要一个简单的系统软件包,还是需要针对不同项目控制 Node 版本。APT 安装方式简单直接,而当项目需要不同版本时,nvm 则非常有用。无论选择哪种路径,在安装另一个副本之前,请先检查你的 shell 实际运行的是哪个版本。

Check what Ubuntu and your project need Ubuntu’s repositories can provide different Node.js versions depending on the release and configured package sources. Check your Ubuntu version, the available APT candidate, and any existing Node installation: lsb_release -a apt policy nodejs node —version npm —version If lsb_release is unavailable, /etc/os-release contains release details. The apt policy output shows the candidate version available from your current sources; compare that with the version your project requires rather than relying on instructions written for another Ubuntu release.

检查 Ubuntu 和你的项目需求。Ubuntu 的软件仓库根据发行版和配置的软件包源,可能会提供不同的 Node.js 版本。请检查你的 Ubuntu 版本、可用的 APT 候选版本以及现有的 Node 安装情况:lsb_release -a、apt policy nodejs、node --version、npm --version。如果 lsb_release 不可用,/etc/os-release 文件中包含发行版详细信息。apt policy 的输出显示了当前源中可用的候选版本;请将其与项目要求的版本进行比较,而不是依赖为其他 Ubuntu 发行版编写的说明。

Option 1: Install the Ubuntu packages For a basic system installation, refresh APT’s package index and install Node.js and npm: sudo apt update sudo apt install nodejs npm Then verify both commands are available: node —version npm —version APT manages these packages system-wide, and their versions are tied to Ubuntu’s repositories. An older version than the latest upstream release doesn’t necessarily indicate a failed install. If you’re setting up a new Ubuntu machine, this fits naturally into the broader Ubuntu server setup and secure access checklist. If the project’s required Node.js version is newer than APT’s candidate, don’t add a repository intended for a different Ubuntu release. Choose a method that provides the version you need.

选项 1:安装 Ubuntu 软件包。对于基础的系统安装,请刷新 APT 软件包索引并安装 Node.js 和 npm:sudo apt update、sudo apt install nodejs npm。然后验证这两个命令是否可用:node --version、npm --version。APT 在系统范围内管理这些软件包,其版本与 Ubuntu 的软件仓库绑定。版本比最新的上游发布版本旧并不一定意味着安装失败。如果你正在设置一台新的 Ubuntu 机器,这可以自然地融入更广泛的 Ubuntu 服务器设置和安全访问清单中。如果项目所需的 Node.js 版本比 APT 的候选版本新,请勿添加针对其他 Ubuntu 发行版的软件仓库。请选择一种能提供你所需版本的方法。

Option 2: Use nvm for project-specific versions nvm installs Node.js versions in your user environment and lets you switch between them. It’s a practical choice for development when different projects have different version requirements. Install and run it as your regular user—not with sudo. After installing nvm using its current project instructions, install a version and select it: nvm install 22 nvm use 22 node —version npm —version Here, 22 is only an example. Use the major version supported by your project. You can see versions managed by nvm and switch to another installed version like this: nvm ls nvm use 20 To select a default version for new shells, run nvm alias default 22, replacing 22 with the version you want. If a newly installed version isn’t available in the current terminal, open a new shell or reload the shell configuration that initializes nvm.

选项 2:使用 nvm 管理项目特定的版本。nvm 在你的用户环境中安装 Node.js 版本,并允许你在它们之间切换。对于不同项目有不同版本要求的开发场景,这是一个实用的选择。请以普通用户身份(而非使用 sudo)安装并运行它。按照 nvm 当前的项目说明安装完成后,安装一个版本并选择它:nvm install 22、nvm use 22、node --version、npm --version。这里的 22 仅为示例,请使用你项目支持的主版本号。你可以查看 nvm 管理的版本并切换到另一个已安装的版本,如下所示:nvm ls、nvm use 20。要为新 shell 选择默认版本,请运行 nvm alias default 22(将 22 替换为你想要的版本)。如果新安装的版本在当前终端中不可用,请打开一个新的 shell 或重新加载初始化 nvm 的 shell 配置文件。

Know which installation your shell is using APT and nvm manage Node.js differently. Installing both without a clear reason can leave you wondering why node —version doesn’t match the version you expected. Ask the shell which executable it finds: command -v node command -v npm With nvm, the paths commonly point inside your home directory. An APT-installed binary is generally in a system location. Check the path and version together before making changes; this helps identify whether you need to switch nvm versions or investigate your package sources. NodeSource is another option for administrators who want a selected Node.js release installed as a system-wide APT package. It adds a third-party package source, so confirm that the Ubuntu release and architecture are supported and follow the current instructions for the release you choose. Avoid mixing package sources without a specific reason.

了解你的 shell 正在使用哪个安装版本。APT 和 nvm 管理 Node.js 的方式不同。在没有明确理由的情况下同时安装两者,可能会让你困惑为什么 node --version 与你预期的版本不符。询问 shell 它找到了哪个可执行文件:command -v node、command -v npm。使用 nvm 时,路径通常指向你的主目录。APT 安装的二进制文件通常位于系统位置。在进行更改之前,请同时检查路径和版本;这有助于确定你是需要切换 nvm 版本还是需要调查你的软件包源。NodeSource 是管理员的另一种选择,他们希望将选定的 Node.js 发行版作为系统范围的 APT 软件包安装。它会添加第三方软件包源,因此请确认你的 Ubuntu 发行版和架构是否受支持,并遵循你所选版本的当前说明。除非有特定理由,否则请避免混合使用软件包源。

Quick troubleshooting node or npm is not found For an APT installation, check whether the Node.js package is installed: dpkg -s nodejs If it isn’t installed, install the needed packages with APT. If you used nvm, make sure the shell initialized nvm and that you selected an installed version with nvm use. Use command -v node or command -v npm to check whether the command is on your current shell’s path. The version doesn’t match the project Check the selected executable and installed nvm versions before installing anything else: command -v node node —version nvm ls APT’s candidate depends on your configured Ubuntu package sources. With nvm, install or select the project’s required major version. Installing a second copy blindly can make it harder to tell which one your shell is using. APT reports repository errors Run sudo apt update and read the errors it prints. Check network access and verify that any third-party repository is configured for your Ubuntu release. Don’t add a repository for another release just to make an installation command succeed. If you’re diagnosing a broader package or installer failure, this Ubuntu installation troubleshooting guide can help narrow down the cause.

快速故障排除:找不到 node 或 npm。对于 APT 安装,请检查是否安装了 Node.js 软件包:dpkg -s nodejs。如果未安装,请使用 APT 安装所需的软件包。如果你使用了 nvm,请确保 shell 已初始化 nvm,并且你已使用 nvm use 选择了一个已安装的版本。使用 command -v node 或 command -v npm 检查该命令是否在当前 shell 的路径中。版本与项目不匹配:在安装任何其他内容之前,请检查所选的可执行文件和已安装的 nvm 版本:command -v node、node --version、nvm ls。APT 的候选版本取决于你配置的 Ubuntu 软件包源。使用 nvm 时,请安装或选择项目所需的主版本。盲目安装第二个副本会让你更难判断 shell 正在使用哪一个。APT 报告仓库错误:运行 sudo apt update 并阅读其打印的错误信息。检查网络访问,并验证任何第三方仓库是否针对你的 Ubuntu 发行版进行了配置。不要仅仅为了让安装命令成功而添加针对其他发行版的仓库。如果你正在诊断更广泛的软件包或安装程序故障,这份 Ubuntu 安装故障排除指南可以帮助缩小原因范围。

Which method should you pick? Use Ubuntu APT when you want a simple package-managed installation and its available version meets your needs. Use nvm when you need to switch versions across projects or manage Node.js as your regular user. For a managed system-wide version through APT, consider a supported third-party repository and follow its current setup instructions. After installing, verify with node —version, npm —version, and, if anything looks unexpected, command -v node. That small check can save you from debugging a project with the wrong Node.js runtime.

你应该选择哪种方法?当你想要一个简单的软件包管理安装,且其可用版本满足你的需求时,请使用 Ubuntu APT。当你需要在不同项目间切换版本或以普通用户身份管理 Node.js 时,请使用 nvm。对于通过 APT 进行的系统范围版本管理,请考虑使用受支持的第三方仓库,并遵循其当前的设置说明。安装完成后,请使用 node --version、npm --version 进行验证;如果发现任何异常,请使用 command -v node。这个小小的检查可以让你免于调试使用错误 Node.js 运行时的项目。