Most Linux distributions ship an old Node.js in their default repositories. To run modern apps such as Next.js you need a current LTS release. This guide shows the two reliable ways to install it on a VPS: nvm (per user, easy to switch versions) and NodeSource (system-wide packages).
Before you start
- A Linux VPS running Ubuntu 22.04/24.04, Debian 12, AlmaLinux 8/9 or Rocky Linux 8/9.
- SSH access as root or a sudo user. See Connect to a Linux VPS over SSH.
- The Node.js major version your project needs (check
enginesinpackage.json).
Which method should you choose?
| Method | Best for | Notes |
|---|---|---|
| nvm | One app user, several projects, easy upgrades | Installed in the user's home folder; not visible to other users or system services by default |
| NodeSource | Servers where systemd or several users need /usr/bin/node | Updates with the normal package manager |
Option A: install Node.js with nvm
- Log in as the user that will run the app (not root, ideally).
- Download and run the nvm install script from the official nvm-sh repository:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash - Load nvm in the current shell (or log out and back in):
source ~/.bashrc - Install and use the latest LTS release:
nvm install --lts nvm alias default lts/* - To install a specific major version instead, for example 22:
nvm install 22
Note: nvm installs Node.js under ~/.nvm. Tools like PM2 must be installed with the same user so they find the right node binary.
Option B: install Node.js from NodeSource
Ubuntu and Debian
- Install the prerequisites and add the repository for your chosen major version (22 here):
sudo apt update sudo apt install -y curl ca-certificates curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash - - Install Node.js (npm is included):
sudo apt install -y nodejs
AlmaLinux and Rocky Linux
- Add the repository:
curl -fsSL https://rpm.nodesource.com/setup_22.x | sudo bash - - Install Node.js:
sudo dnf install -y nodejs
Install build tools for native modules
Some npm packages (for example bcrypt or sharp on older versions) compile native code. Install a compiler toolchain so npm install does not fail:
sudo apt install -y build-essentialOn AlmaLinux or Rocky Linux use sudo dnf groupinstall -y "Development Tools".
Check that it works
node -v
npm -v
which nodeYou should see the version you installed, for example v22.x.x. Then try a one-line server:
node -e "require('http').createServer((q,r)=>r.end('ok')).listen(3000)"In a second SSH session run curl http://127.0.0.1:3000; it should print ok. Press Ctrl+C to stop the test server.
Troubleshooting
nvm: command not found— open a new shell or runsource ~/.bashrc. Check that the installer added the nvm lines to~/.bashrcor~/.profile.- Old version still shows — another Node.js from the distro repository is earlier in
PATH. Remove it withsudo apt remove nodejsand reinstall. EACCESon global installs — with NodeSource, usesudo npm install -g, or switch to nvm so globals live in your home folder.
