Install Node.js on Your Linux VPS
This guide explains how to install Node.js and npm on your Linux VPS, whether you're using Ubuntu, Debian, CentOS, or Rocky Linux. We'll cover multiple installation methods, including using nvm (Node Version Manager) to easily manage multiple Node.js versions.
π Prerequisites
β A Linux VPS (Ubuntu, Debian, CentOS, Rocky Linux) β SSH access with sudo privileges β A stable Internet connection β At least 1 GB of available RAM
Install Node.js with nvm (Recommended)
nvm (Node Version Manager) is the recommended method because it makes managing multiple Node.js versions easy.
Benefits of nvm:
- Install multiple versions of Node.js
- Easily switch between versions
- Install Node.js without root privileges
- Manage versions per project
Download and install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Load nvm into your session
source ~/.bashrc
source ~/.zshrc
nvm --version
Install Node.js
nvm install --lts
# Install Node.js 20
nvm install 20
# Install Node.js 18
nvm install 18
# Install Node.js 16
nvm install 16
nvm alias default 20
Verify installation
Node.js version
node --version
npm version
npm --version
β Node.js and npm are now installed via nvm!
Useful nvm commands:
| Command | Description |
|---|---|
nvm list | List installed versions |
nvm ls-remote | List all available versions |
nvm use 20 | Use a specific version |
nvm uninstall 16 | Uninstall a version |
nvm current | Show current version |
Install via package manager
Alternative to nvm: direct installation via NodeSource repositories.
Ubuntu and Debian
# Update packages
sudo apt update
# Install curl if needed
sudo apt install -y curl
# Add NodeSource repository for Node.js 20
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
# Install Node.js and npm
sudo apt install -y nodejs
π‘ Tip: For other versions, replace setup_20.x with setup_18.x (Node.js 18) or setup_21.x (Node.js 21).
node --version
npm --version
CentOS, Rocky Linux and AlmaLinux
# Update packages
sudo dnf update -y # or yum update -y
# Add NodeSource repository for Node.js 20
curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -
# Install Node.js and npm
sudo dnf install -y nodejs # or yum install -y nodejs
node --version
npm --version
Post-installation configuration
Configure npm without sudo
By default, npm requires sudo for global installations. Let's configure npm to use a local directory.
# Create a directory for global packages
mkdir ~/.npm-global
# Configure npm to use this directory
npm config set prefix '~/.npm-global'
# Add the directory to PATH
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
Update npm
npm install -g npm@latest
Alternative package managers
π‘ Tip: For faster installations, you can use alternative package managers.
npm install -g pnpm
npm install -g yarn
Install development tools
Build tools
sudo apt install -y build-essential
sudo dnf groupinstall "Development Tools" -y
Git
sudo apt install -y git
sudo dnf install -y git
Test your installation
Create a simple Node.js application
mkdir ~/test-nodejs
cd ~/test-nodejs
nano server.js
const http = require('http');
const hostname = '0.0.0.0';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello from Node.js on AtmosNode VPS!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
βΉοΈ Save and exit with Ctrl+X, then Y, then Enter.
Start the server
node server.js
curl http://localhost:3000
β
You should see: Hello from Node.js on AtmosNode VPS!
Access from outside
β οΈ Access from the Internet
To access your application from the Internet, you need to open the port in your firewall.
sudo ufw allow 3000/tcp
sudo firewall-cmd --permanent --add-port=3000/tcp
sudo firewall-cmd --reload
Manage processes with PM2
PM2 is a process manager to keep your Node.js applications online 24/7.
PM2 features:
- Keep your applications online 24/7
- Automatically restart on crash
- Manage logs
- Start applications on server boot
- Cluster mode to use all CPU cores
Install PM2
npm install -g pm2
Use PM2
Start an application
pm2 start server.js
List applications
pm2 list
View logs
pm2 logs
Stop an application
pm2 stop server
Restart an application
pm2 restart server
Delete an application
pm2 delete server
Start PM2 on boot
pm2 startup
pm2 save
Advanced PM2 commands:
| Command | Description |
|---|---|
pm2 monit | Interactive dashboard |
pm2 status | System statistics |
pm2 start server.js -i max | Cluster mode (all cores) |
pm2 reload all | Zero-downtime restart |
Security best practices
Never run Node.js as root
sudo adduser nodejs
sudo su - nodejs
Use a reverse proxy (Nginx)
# Ubuntu/Debian
sudo apt install -y nginx
# CentOS/Rocky
sudo dnf install -y nginx
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
sudo ln -s /etc/nginx/sites-available/nodejs-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Secure environment variables
npm install dotenv
DATABASE_URL=your-database-url
API_KEY=your-secret-key
require('dotenv').config();
const apiKey = process.env.API_KEY;
Update regularly
Check for vulnerabilities
npm audit
Fix automatically
npm audit fix
Update Node.js with nvm
nvm install node --reinstall-packages-from=current
Verification checklist
Verify that you have:
β
Node.js is installed and working (node --version)
β
npm is installed and working (npm --version)
β
nvm is configured to manage versions (if used)
β
Build tools are installed
β
PM2 is installed for process management
β
An Nginx reverse proxy is configured
β
Node.js applications don't run as root
β
npm packages are up to date without vulnerabilities
β
Sensitive environment variables are secured
β
PM2 starts automatically on system boot
π Useful resources
Official documentation:
- Node.js: https://nodejs.org/docs
- npm: https://docs.npmjs.com
- nvm: https://github.com/nvm-sh/nvm
- PM2: https://pm2.keymetrics.io/docs
- Node.js Best Practices: https://github.com/goldbergyoni/nodebestpractices
π¬ Need Help?
If you encounter any difficulties:
π Congratulations!
Node.js is now installed and configured on your VPS! You're ready to develop and deploy your server-side JavaScript applications.

