Installing Node.js 21 on Ubuntu is a pretty simple task regardless if you’re targeting an LTS or a non-LTS release. Personally, when working with Ubuntu in production environments, I stick to LTS releases like Ubuntu 20.04 LTS and Ubuntu 22.04 LTS. This guide should work well on Ubuntu 22.10 and Ubuntu 23.10 as well.
Installing Node.js 21 via Ubuntu’s APT Package Manager
My preferred method of getting Node.js 21 on Ubuntu is with the APT Package Manager. APT stands for “Advanced Package Tool” and is part of the Debian Linux distribution which Ubuntu is based on.
The reason that I prefer this method is that it allows us to easily upgrade Node.js alongside the rest of our packages. No additional tools to use or commands to remember.
Updating and adding dependencies
To get started, I like to make sure that my system is completely up to date:
Depending on how long it’s been since your last upgrade, or which packages needed to be updated, it may be a good time to reboot your system as well.
With our Ubuntu system up to date, we can proceed with making sure our dependencies are available. Fortunately, we only need to install a few packages, some of which will more than likely already be available:
Adding the APT source for and installing Node.js 21
Now that our system is up to date and the necessary dependencies are added, we can proceed with adding the new APT source to our Ubuntu system for Node.js 21.
First, we’ll want to download and import the GPG key for Nodesource, the ones that provide the binary version of Node.js for Ubuntu:
With the GPG keys installed, we can add the APT source for Node.js 21:
Adding the APT source allows for that source to be included when we run
apt commands. Next up, we’ll update our system again, and then install the
Assuming the installation went through as expected, we should now have an Ubuntu system that has Node.js 21 available:
Installing Node.js 21 via NVM on Ubuntu
While not my preferred method of installing Node.js on a server, my preferred method for my local environment is via NVM. NVM is the Node Version Manager, which allows you install multiple versions of Node.js and bounce between them depending on the project.
One caveat, is that NVM is not directly tied to your package management system, so you will have to remember to update Node.js from time to time.
Despite this, using NVM tends to be an easier approach to adding Node.js on Ubuntu.
Installing NVM on Ubuntu
Installing NVM on Ubuntu requires you to download the installation file from GitHub and running it. If you have
curl installed (directions above) you can simply run:
After the installation is complete, you should have a working copy of
nvm installed. The script also attempts to add the relevant shell commands to your shell profile,
~/.zshrc, or whichever file it determines is the right one for your shell.
Before continuing, you will need to close and reopen your command prompt to source the new commands, or you could run the commands yourself for the first time:
Installing Node.js 21 via NVM
The first thing to do once the NVM is script is done running, is to install a version of Node.js to use. To install Node.js 21, run:
Additionally, if this is the very first version of Node.js you’re adding with NVM, it should set it as the default version for you automatically, and you should be good to go.
Otherwise, if this is a new version of Node.js being added, or if for some reason your version of NVM doesn’t automatically create the alias, you can run:
Of course, it’s always good to double check that things worked out as we expected:
Explore More Articles
- Speed up your shell prompt when using Node Version Manager
- How to install Node.js 20 on Ubuntu 20.04 LTS
- How to install Node.js 18 on Ubuntu 20.04 LTS
- How to install Node.js 16 on Ubuntu 20.04 LTS
Looking for an Ubuntu host?
Check out DigitalOcean, with $200 of free credit to spend over 2 months, you can explore Ubuntu on their Droplets and all of their other managed services!