LochStudios  /  Help Centre  /  VPS & Linux  /  Install Node.js (via NodeSource or NVM)

Install Node.js (via NodeSource or NVM)

Install Node.js and npm on your VPS using NodeSource repositories or NVM (Node Version Manager) for easy version switching.

Updated

Node.js is a JavaScript runtime for building server-side applications. This guide covers two installation methods: NodeSource (recommended for production) and NVM (better for development and version switching).

Option A: Install via NodeSource Repository (Recommended for Production)

NodeSource maintains up-to-date Node.js packages and is the standard choice for production servers.

Step 1: Install Node.js from NodeSource

For Ubuntu/Debian (LTS version 20 as example):

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install nodejs -y

For the latest LTS version, replace setup_20.x with setup_22.x (or check https://github.com/nodesource/distributions for current versions).

For RHEL/CentOS:

curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -
sudo dnf install nodejs -y

Step 2: Verify installation

node -v
npm -v

Step 3: Update npm to the latest version

sudo npm install -g npm@latest

Step 4: (Optional) Install global packages

Install common tools globally:

sudo npm install -g pm2 yarn ts-node
  • pm2: Process manager for Node.js applications
  • yarn: Alternative package manager
  • ts-node: Run TypeScript files directly

Option B: Install via NVM (Better for Development)

NVM allows you to install multiple Node.js versions and switch between them easily. This is ideal for development or testing different versions.

Step 1: Install NVM

Download and run the NVM installer:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

Reload your shell configuration:

source ~/.bashrc

Or close and reopen your terminal.

Step 2: Verify NVM installation

nvm --version

Step 3: Install Node.js

List available versions:

nvm list-remote

Install the latest LTS version:

nvm install node

Or install a specific version:

nvm install 20.10.0
nvm install 18.18.0

Step 4: Switch between versions

Set a default version:

nvm alias default 20.10.0

Switch to a specific version in the current shell:

nvm use 20.10.0

View installed versions:

nvm list

Step 5: Verify installation

node -v
npm -v

Step 5 (Both Methods): Create a simple Node.js application

Create a test directory:

mkdir -p ~/my-app
cd ~/my-app

Initialize a Node.js project:

npm init -y

Create a simple server file (app.js):

cat > app.js << 'EOF'
const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello, Node.js!\n');
});

server.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});
EOF

Step 6: Run your application

node app.js

You should see:

Server running on http://localhost:3000

Test it from another terminal:

curl http://localhost:3000

You'll see: Hello, Node.js!

Stop the server with Ctrl+C.

Step 7: Run Node.js in the background with PM2

PM2 keeps your Node.js app running and restarts it if it crashes.

Install PM2:

npm install -g pm2

Start your app with PM2:

pm2 start app.js --name "my-app"

View running processes:

pm2 list

View logs:

pm2 logs my-app

Enable PM2 to start on system reboot:

pm2 startup
pm2 save

Stop your app:

pm2 stop my-app

Restart:

pm2 restart my-app

Step 8: Open firewall port (if needed)

If you want external access to your Node.js app on port 3000, open the firewall:

sudo ufw allow 3000/tcp

For production, use a reverse proxy (Nginx or Apache) to forward port 80/443 to your Node.js app running on a higher port.

Tips

  • NVM for multiple projects: If you maintain projects on different Node.js versions, NVM makes it easy to switch per-project.
  • Production with NodeSource: NodeSource is the standard for production servers because it integrates with package managers and includes security updates.
  • Check Node.js version support: Node.js has LTS (Long Term Support) versions. Check https://nodejs.org/ for current LTS releases.
  • Update Node.js: With NodeSource, run sudo apt upgrade nodejs. With NVM, run nvm install node && nvm alias default node.
  • NPM packages location: Global packages with NVM are installed in ~/.nvm/versions/node/vX.X.X/lib/node_modules/. With NodeSource, they're in /usr/lib/node_modules/.
  • Remove a Node.js version (NVM): nvm uninstall 18.18.0

Was this article helpful?

← Back to VPS & Linux