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:
% sudo apt update # Update our package list
% sudo apt upgrade # Upgrade our installed packages
ZshDepending 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:
% sudo apt install -y ca-certificates curl gnupg
ZshAdding 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:
% sudo mkdir -p /etc/apt/keyrings
% curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
ZshWith the GPG keys installed, we can add the APT source for Node.js 21:
% echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_21.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
ZshAdding 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 nodejs
package:
% sudo apt update
% sudo apt install nodejs -y
ZshAssuming the installation went through as expected, we should now have an Ubuntu system that has Node.js 21 available:
% node --version
v21.1.0
ZshInstalling 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:
% curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
ZshAfter 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, ~/.bashrc
, ~/.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:
% export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
ZshInstalling 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:
% nvm install 21
Downloading and installing node v21.1.0...
Downloading https://nodejs.org/dist/v21.1.0/node-v21.1.0-linux-arm64.tar.gz...
############################################################################ 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v21.1.0 (npm v10.2.0)
ZshAdditionally, 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:
% nvm alias default 21
default -> 21 (-> v21.1.0)
ZshOf course, it’s always good to double check that things worked out as we expected:
% node --version
v21.1.0
ZshExplore 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!