This guide explains how to install Node.js (JavaScript runtime) and npm (package manager) on an Ubuntu or Debian VPS to run web apps (Express, NestJS, build tools, etc.).
Prerequisites
- Linux VPS with
sudoor root SSH access - Ubuntu 20.04+ or Debian 11+ (64-bit)
- Working SSH connection
Recommendation
For frequent builds (npm install, compilations), prefer a VPS with fast storage such as our NVMe VPS.
Recommended method: NodeSource (LTS)
1. Update the system
Bashsudo apt update && sudo apt upgrade -y
2. Install prerequisites
Bashsudo apt install -y ca-certificates curl gnupg
3. Add the NodeSource key and repository (example: Node.js 22.x LTS)
Replace 22.x with your desired LTS branch using the NodeSource distributions documentation.
Bashcurl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
4. Install Node.js
Bashsudo apt install -y nodejs
5. Verify
Bashnode -v npm -v
Quick alternative: distro packages
Bashsudo apt install -y nodejs npm
Versions may be older than with NodeSource. For production, prefer NodeSource or a version manager (nvm).
Update npm (optional)
Bashsudo npm install -g npm@latest
Best practices
- Avoid running project
npmcommands as root: use a dedicated user orsudo -u myuser. - For production, consider PM2 or a systemd unit to restart the app on boot.
- Open only required ports (firewall, Nginx reverse proxy in front of the app).
Troubleshooting
- Permission denied on global
npm install -g: configure a user-level global directory or usenvm. - Command not found after install: reconnect SSH or open a new shell.
Next steps: harden SSH and Nginx + Certbot for HTTPS.