Installing or upgrading Node.js on a VPS: distro package, NodeSource or nvm

You installed Node.js on a VPS and the version is too old for your project, or you need to move up. The short answer: there are three routes, and the choice depends on how many projects you have and who runs them. For a server with one application, the NodeSource repository is the most direct. For several projects on different versions, nvm. The distribution’s package is the simplest, but it tends to be behind.

The three routes

Method Good for The catch
Distribution package (apt, dnf) Installing quickly, with the system’s security updates. The version is often older than the one the project asks for. On AlmaLinux and Rocky the distribution offers module streams (dnf module list nodejs).
NodeSource repository A recent, fixed version for the whole system; updates come through apt or dnf. Follow the command on their page, which changes with versions; do not copy it from an old tutorial.
nvm (Node Version Manager) Several projects on different versions, or trying a new version without touching the old one. It installs per user, not for the system. systemd and PM2 do not load nvm: use the full path to node.

Choosing the version and confirming

1 See what the project asks for: the engines field in package.json, or the README. Prefer a long-term support (LTS) version: under Node.js’s policy, the even-numbered releases are the ones that become LTS; end-of-support dates are on nodejs.org.
2 Install by the chosen method, following the project’s current documentation (NodeSource or nvm). The instructions change over time, so this article does not copy them. With nvm, after installing: nvm install 22
nvm use 22
nvm alias default 22
The 22 is an example: use the version you need.
3 Confirm: node -v
npm -v
which node
which node shows which one is being used. If the version is not what you expected, another installation is ahead in the path.
4 After moving up a version, reinstall each project’s dependencies (npm ci) and rebuild the ones with compiled parts (npm rebuild), then restart the applications (pm2 restart, or the service).
Is the service still on the old Node? It is the commonest slip with nvm: your terminal uses the new version, but systemd (or PM2 started by another user) does not load nvm and runs whatever it finds. Put in ExecStart the full path that which node gave you with nvm active. See the service example with variables.
Do not skip versions blindly. Go up one step at a time, test the application and only then move on. Before touching a server that hosts sites, make a backup: backing up a VPS. On an unmanaged VPS, Node and whatever runs on it are yours to maintain: how far our support goes.

No VPS for your application yet? See the plans and pick the system when you order.

See the VPS servers

SEE ALSO

Keeping a Node.js app running on a VPS with PM2

Node.js in cPanel: choosing the version and using npm

Which operating system to choose for your VPS or server

RECOMMENDED PRODUCT

VPS server with root access

Resources of your own, the OS you choose, reinstall whenever you like. from $8.39/mo (3-year plan, with coupon)

See plans
  • 0 Users Found This Useful
Was this answer helpful?