Method 1 — Install Node.js from Ubuntu Repository
✔️ Easiest method ✔️ Stable ❌ Not always the latest version
bash
sudo apt update && sudo apt upgrade -y
sudo apt install -y nodejs npm
node -v
npm -v
This installs Node.js + npm directly from Ubuntu’s official repo.
Method 2 — Install Latest Node.js via NodeSource (Recommended)
This is the best method for production VPS because it gives you the latest LTS or current version.
Step 1: Install curl
bash
sudo apt install -y curl
Step 2: Add NodeSource repo (example: Node.js 20.x)
bash
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
Step 3: Install Node.js
bash
sudo apt install -y nodejs
Step 4: Verify
bash
node -v
npm -v
Method 3 — Install Node.js Using NVM (Node Version Manager)
Best for developers who need to switch between Node versions.
Step 1: Install NVM
bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
Step 2: Install Node.js (LTS)
bash
nvm install --lts
Step 3: Verify
bash
node -v
npm -v