PROMO20% OFF with code QY3UJAEZ59 - Limited time offer!
Tutorials

Install Node.js on Your Linux VPS

Complete guide to install Node.js and npm on Ubuntu, Debian, CentOS, and Rocky Linux. Learn how to use nvm to manage multiple Node.js versions.

πŸ“–7 min read
⚑Beginner

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


1

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

β–Έ With curl
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
β–Έ With wget
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

Load nvm into your session

β–Έ For Bash
source ~/.bashrc
β–Έ For Zsh
source ~/.zshrc
β–Έ Verify installation
nvm --version

Install Node.js

β–Έ Install latest LTS version
nvm install --lts
β–Έ Install specific version
# Install Node.js 20
nvm install 20

# Install Node.js 18
nvm install 18

# Install Node.js 16
nvm install 16
β–Έ Set default version
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:

CommandDescription
nvm listList installed versions
nvm ls-remoteList all available versions
nvm use 20Use a specific version
nvm uninstall 16Uninstall a version
nvm currentShow current version

2

Install via package manager

Alternative to nvm: direct installation via NodeSource repositories.

Ubuntu and Debian

β–Έ Add NodeSource repository
# 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).

β–Έ Verify installation
node --version
npm --version

CentOS, Rocky Linux and AlmaLinux

β–Έ Add NodeSource repository
# 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
β–Έ Verify installation
node --version
npm --version

3

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.

β–Έ Install pnpm (faster)
npm install -g pnpm
β–Έ Install Yarn
npm install -g yarn

4

Install development tools

Build tools

β–Έ On Ubuntu/Debian
sudo apt install -y build-essential
β–Έ On CentOS/Rocky Linux
sudo dnf groupinstall "Development Tools" -y

Git

β–Έ On Ubuntu/Debian
sudo apt install -y git
β–Έ On CentOS/Rocky Linux
sudo dnf install -y git

5

Test your installation

Create a simple Node.js application

β–Έ Create the project
mkdir ~/test-nodejs
cd ~/test-nodejs
nano server.js
β–Έ Application code (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

β–Έ Start the application
node server.js
β–Έ Test from another terminal
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.

β–Έ With UFW
sudo ufw allow 3000/tcp
β–Έ With FirewallD
sudo firewall-cmd --permanent --add-port=3000/tcp
sudo firewall-cmd --reload

6

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:

CommandDescription
pm2 monitInteractive dashboard
pm2 statusSystem statistics
pm2 start server.js -i maxCluster mode (all cores)
pm2 reload allZero-downtime restart

7

Security best practices

Never run Node.js as root

β–Έ Create a dedicated user
sudo adduser nodejs
sudo su - nodejs

Use a reverse proxy (Nginx)

β–Έ Install Nginx
# Ubuntu/Debian
sudo apt install -y nginx

# CentOS/Rocky
sudo dnf install -y nginx
β–Έ Nginx configuration (/etc/nginx/sites-available/nodejs-app)
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;
    }
}
β–Έ Enable configuration
sudo ln -s /etc/nginx/sites-available/nodejs-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Secure environment variables

β–Έ Install dotenv
npm install dotenv
β–Έ Create a .env file
DATABASE_URL=your-database-url
API_KEY=your-secret-key
β–Έ Use in code
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

8

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


πŸ’¬ 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.

Questions? Let’s chat.
contact [at] atmosnode [dot] com
Live Chat
AtmosNode
Β© 2025 AtmosNode.com β€’ Company number: 899 139 182
Loading...
Company not subject to VAT

πŸ”’ Secure Payment Methods (Crypto accepted)

Visa
MasterCard
PayPal
Apple Pay
Google Pay