If you’re working with Node.js on Ubuntu, one of the easiest and most flexible ways to manage different versions is by using NVM (Node Version Manager). Instead of being stuck with the version of Node.js from Ubuntu’s default repositories, NVM lets you install and switch between multiple versions quickly.
In this guide, I’ll walk you through the steps to set up NVM and install Node.js on Ubuntu 22.04.
Step 1: Update Your System
Before we do anything, it’s a good idea to update your package list:
sudo apt update && sudo apt upgrade -y
Step 2: Install Required Packages
NVM needs a few basic tools to run properly. Install them with:
sudo apt install curl wget git -y
Step 3: Install NVM
You can install NVM using the official install script. Run the following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Once the installation is done, you’ll need to load NVM into your shell session. Run:
source ~/.bashrc
To confirm NVM is installed, check the version:
nvm --version
Step 4: Install Node.js with NVM
Now you can use NVM to install Node.js. For example:
nvm install --lts
This installs the latest LTS (Long Term Support) version of Node.js, which is recommended for most projects.
If you want a specific version, just run:
nvm install 20
Step 5: Verify the Installation
Check that Node.js and npm were installed correctly:
node -v
npm -v
Step 6: Switch Between Versions (Optional)
If you installed multiple versions, you can switch between them easily:
nvm use 18
To set a default version for every new session:
nvm alias default 18
Conclusion
That’s it! You’ve installed Node.js on Ubuntu 22.04 using NVM. The main benefit of this method is flexibility—you can install, remove, and switch between different Node.js versions without messing with your system’s default environment.
Whether you’re working on old projects or testing the latest features, NVM makes managing Node.js versions effortless.