Install Node.js on Ubuntu
To install Node.js on Ubuntu, the most flexible and recommended method is using NVM (Node Version Manager). This lets you easily switch between Node.js versions.
Here’s a breakdown of the three main installation methods:
🛠️ Method 1: Using NVM (Node Version Manager) — Recommended
This method is ideal if you want to manage multiple Node.js versions or avoid system-wide changes.
- Install NVM:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bashThen restart your terminal or run:
source ~/.nvm/nvm.sh - Install Node.js via NVM:
nvm install node # Latest version nvm install 20 # Specific version (e.g., v20) nvm use 20 # Switch to version 20 - Verify installation:
node -v npm -v
📦 Method 2: Using Ubuntu’s APT Package Manager
This is the simplest method but may not give you the latest version.
- Update package index:
sudo apt update - Install Node.js and npm:
sudo apt install nodejs npm - Check versions:
node -v npm -v
📥 Method 3: Using NodeSource PPA (for specific versions)
This method is good for installing a specific version system-wide.
- Add NodeSource PPA:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - - Install Node.js:
sudo apt install -y nodejs - Verify installation:
node -v npm -v
🔍 Bonus Tips
- Use
nvm lsto list installed versions. - Use
nvm alias default <version>to set a default version. - For production environments, stick to LTS versions like Node.js 20.
Install Yarn
- If you already use NPM:
npm install --global yarn - Run this to find where NPM installed Yarn:
which yarnYou’ll get something like:
/home/dung-nguyen/.nvm/versions/node/v24.11.0/bin/yarn -
Edit your shell config file (depending on your shell):
For Bash:
echo 'export PATH="$PATH:/home/dung-nguyen/.nvm/versions/node/v24.11.0/bin/yarn"' >> ~/.bashrc source ~/.bashrcFor Zsh:
echo 'export PATH="$PATH:/home/dung-nguyen/.nvm/versions/node/v24.11.0/bin/yarn"' >> ~/.zshrc source ~/.zshrc - Verify Yarn is accessible:
yarn --version