How to Install Node.js on Ubuntu & Debian (with NVM)

Node.js is a fast JavaScript runtime for building APIs, web applications, and backend services. This guide covers installing it via NVM (Node Version Manager) — the recommended approach, since it lets you install and switch between multiple Node.js versions easily.

Prerequisites

  • Ubuntu 22.04/24.04 or Debian 11/12 VPS
  • Root or sudo access

Step 1 — Install NVM

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

Reload your shell configuration:

source ~/.bashrc

Step 2 — Verify NVM Installation

nvm --version

Step 3 — Install the Latest LTS Version of Node.js

nvm install --lts

Step 4 — Verify Node.js and npm

node -v
npm -v

Step 5 — Set the Default Version

nvm alias default --lts

Installing a Specific Node.js Version

nvm install 20
nvm install 22

Switching Between Installed Versions

nvm use 20
nvm use 22

Listing Installed Versions

nvm ls

Alternative: System-Wide Install via NodeSource (No Version Switching)

For a simpler single-version system install:

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

Step 6 — Create a Test Application

mkdir myapp && cd myapp
echo "console.log('Hello from Node.js');" > app.js
node app.js

Step 7 — Install PM2 for Production Process Management

npm install -g pm2

Run your application persistently:

pm2 start app.js --name myapp
pm2 startup
pm2 save

pm2 startup configures PM2 to restart your app automatically after a server reboot.

Common PM2 Commands

pm2 list
pm2 logs myapp
pm2 restart myapp
pm2 stop myapp

Common Errors

"nvm: command not found" after installation — the shell profile wasn't reloaded; run source ~/.bashrc or open a new terminal session.

Permission errors installing global npm packages — avoid using sudo with npm when using NVM (it manages its own user-writable directory); if permission errors occur, verify you're using an NVM-installed Node version, not a system one.

Best Practices

  • Use NVM for development flexibility across projects with different Node.js version requirements
  • Use PM2 (or a systemd service) to keep production applications running and auto-restart after crashes/reboots
  • Pin your project's Node.js version in a .nvmrc file for team consistency

Related Articles

  • Nginx as a Reverse Proxy for Node.js/Docker Apps
  • How to Deploy a Node.js Application with PM2 and Nginx
  • How to Install Docker Engine on Ubuntu & Debian
  • node.js, nvm, install node.js, pm2, ubuntu, debian
  • 0 Utenti hanno trovato utile questa risposta
Hai trovato utile questa risposta?

Articoli Correlati

How to Install PHP on Ubuntu & Debian (Complete Guide)

PHP powers a huge share of the web, including WordPress, Laravel, and Magento. This guide covers...

How to Install Python on Ubuntu & Debian

Python powers web frameworks like Django and Flask, automation scripts, and data applications....

How to Install Java (OpenJDK) on Ubuntu & Debian

Java powers enterprise applications, build tools like Maven and Gradle, and platforms like...

How to Install Composer for PHP Dependency Management

Composer is PHP's standard dependency manager, used by virtually every modern PHP framework...

How to Deploy a Node.js Application with PM2 and Nginx

This guide covers a complete, production-ready Node.js deployment pattern: PM2 keeping your app...