How to Install Node.js on Ubuntu/Linux Using NVM (Simple Guide for Beginners)
π‘ Tip: Don't install Node.js directly using
apt
or Snap. Use NVM β Node Version Manager β instead.
Itβs cleaner, safer, and perfect for developers!
π What is NVM?
NVM (Node Version Manager) is a tool that helps you install, manage, and switch between different versions of Node.js easily β without affecting system-wide settings.
β Why Use NVM Instead of Direct Installation?
Hereβs why using NVM is a better choice:
- Switch Node versions anytime β Perfect for different projects.
- No root access needed β Avoids system-level changes.
- Easily uninstall or upgrade β Clean and flexible.
- Avoid outdated versions β
apt install nodejs
often gives older versions. - No conflict with OS packages β Keeps things isolated and safer.
π οΈ Step-by-Step Guide to Install NVM & Node.js
Step 1: Install NVM (Latest Version)
Open your terminal and run:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
βοΈ This downloads and runs the official NVM installation script.
Step 2: Load NVM in Your Shell
After installation, activate NVM by running:
source ~/.bashrc
π This command reloads your shell configuration so NVM is available immediately.
Step 3: Confirm NVM Installation
Check if NVM was installed correctly:
nvm --version
If you see a version number like 0.39.7
, you're good to go!
Step 4: Install Latest Stable Node.js (v20)
nvm install 20
π― This installs Node.js version 20 (LTS) using NVM.
Step 5: Set Node v20 as the Default Version
nvm alias default 20
π This makes Node v20 the default every time you open a new terminal.
Step 6: Use Node v20 Right Now
nvm use 20
π’ Activates Node v20 in your current terminal session.
Step 7: Confirm Node and NPM Installation
node -v
npm -v
You should see the versions for Node (
v20.x.x
) and NPM.
π Youβre Done!
π Thatβs it! Youβve installed Node.js the right way using NVM.
Now you can:
- Easily switch Node versions:
nvm use 18
,nvm install 21
, etc. - Keep your system clean and organized.
- Stay up-to-date with latest LTS releases.