There are three common ways to install Node.js on Ubuntu, and they suit different situations:
| Method | Version you get | Best for |
|---|---|---|
Ubuntu repository (apt install nodejs) |
Whatever shipped with the Ubuntu release — often an older LTS | Tools that just need some Node |
| NodeSource repository | Any current LTS line (e.g. 22.x, 24.x), updated via apt | Production servers |
| nvm (Node Version Manager) | Any version, per user, switchable | Development, multiple apps needing different versions |
For a server running one or two Node apps, NodeSource is usually the right answer: a current LTS release with security updates through apt upgrade.
Before choosing a version, check which lines are in Active LTS or Maintenance LTS on the Node.js release schedule. Odd-numbered releases (23, 25) are short-lived and not meant for production.
Method 1: Ubuntu’s own package
sudo apt update
sudo apt install -y nodejs npm
node -v
npm -v
Simple, but the version is fixed for the life of the Ubuntu release and may be too old for modern frameworks such as Next.js or Vite. Fine for running a small script; not ideal for applications.
Method 2: NodeSource (recommended for servers)
NodeSource provides a setup script that adds its apt repository for the major version you choose. Replace 22 with the LTS line you want:
sudo apt update
sudo apt install -y curl ca-certificates
curl -fsSL https://deb.nodesource.com/setup_22.x -o nodesource_setup.sh
less nodesource_setup.sh # read before running anything piped from the web
sudo bash nodesource_setup.sh
sudo apt install -y nodejs
The NodeSource nodejs package includes npm and npx — don’t install Ubuntu’s separate npm package alongside it.
node -v # v22.x.x
npm -v
Future patch and minor releases arrive through normal apt upgrade. To move to a new major version, run the setup script for the new line and apt install nodejs again.
If you previously installed Ubuntu’s nodejs, remove it first (sudo apt remove nodejs npm) to avoid package conflicts.
Method 3: nvm (per-user, many versions)
nvm installs Node into your home directory and lets you switch versions per project:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
source ~/.bashrc
nvm install --lts
nvm use --lts
node -v
(Check the nvm GitHub page for the current version number in that URL.) Details in our nvm guide.
nvm’s Node lives in ~/.nvm and is only on the PATH for that user’s interactive shells. That’s a downside on servers: systemd services, cron jobs and other users won’t find it unless you reference the full path. For production services, NodeSource is simpler.
Install build tools for native modules
Some npm packages (bcrypt, sharp in some cases, sqlite3) compile native code during installation:
sudo apt install -y build-essential python3
Global packages without sudo
With the NodeSource install, npm install -g writes to /usr/lib/node_modules and needs sudo. To avoid running npm as root, point the global prefix at your home directory:
mkdir -p ~/.npm-global
npm config set prefix ~/.npm-global
echo 'export PATH=$HOME/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
npm install -g pm2
Enable Corepack for pnpm and Yarn
Corepack manages package-manager versions per project (via the packageManager field in package.json). It’s bundled with Node 22 and earlier; newer Node releases may require installing it from npm first.
sudo corepack enable
pnpm -v
Run your app in production
Don’t leave node app.js running in an SSH session. Use a process manager that restarts the app on crashes and at boot:
- PM2 — popular, with log management and cluster mode: PM2 guide.
- systemd — built into Ubuntu, no extra software.
A minimal systemd unit, /etc/systemd/system/myapp.service:
[Unit]
Description=My Node app
After=network.target
[Service]
User=deploy
WorkingDirectory=/home/deploy/myapp
Environment=NODE_ENV=production PORT=3000
ExecStart=/usr/bin/node server.js
Restart=always
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
Then put Nginx in front for HTTPS and the domain name — see deploying a Node.js Express app with Nginx.
Uninstall
sudo apt remove --purge nodejs
sudo rm -f /etc/apt/sources.list.d/nodesource.list # NodeSource repo
rm -rf ~/.nvm # nvm install
Troubleshooting
node: command not foundafter nvm install — open a new shell or runsource ~/.bashrc.EACCES: permission deniedonnpm install -g— use the prefix trick above instead ofsudo npm.- dpkg error about
/usr/include/node/common.gypiwhile installing — Ubuntu’slibnode-devconflicts with NodeSource’s package;sudo apt remove libnode-devand retry. - Native module build fails — install
build-essentialandpython3, thennpm rebuild.
A Node.js app with a database runs comfortably on 2 GB of RAM. Our Ubuntu VPS plans include full root access to set up Node exactly the way you want.