Five ways to install Node.js on Ubuntu 20.04
What is Node.js?
Node.js is an open-source, cross-platform JavaScript runtime based on the V8 JavaScript engine. Its server-side execution and asynchronous event-driven nature are its defining features, allowing web devs to create dynamic, performant web content and applications.
Processing tasks asynchronously lets Node.js handle many times more requests than scripting languages such as PHP and traditional JavaScript. At the same time, its server-side execution leaves it less affected by slow end-user hardware. All this makes it an excellent choice for websites and web apps.
How do I install Node.js on Ubuntu?
There are several ways to install Node.js on Ubuntu 20.04:
- Via the default software repository using
apt
. - Using an alternate PPA software repository with
apt
to install a specific version. - Installing Node Version Manager (NVM) and using that to install and manage your Node.js versions.
- Installing Fast Node Manager (FNM) and using that to install and manage your Node.js versions.
- Installing Node.js via a Docker image.
We'll cover all of these methods and their use cases so that you can install Nodejs in a way that best suits your needs. Before we start, you will naturally need a machine running Ubuntu 20.04 and a non-root user with sudo privileges.
How to install Node.js on Ubuntu 20.04 using the default repositories
If you don't have any particular requirements and want to get started as quickly as possible, the default repository is the way to go. The version of node.js included with Ubuntu is outdated and only has security support at the time of writing. You should ideally update it before using it in production.
All you need to do to install it is:
- Refresh your package index with
sudo apt update
. - Run
sudo apt install nodejs -y
and follow any additional instructions. - Type
node -v
after the installation completes to ensure the installation was successful.
You can optionally install node package manager or Yarn for easier updating and package management in the future.
Uninstalling Node.js from the default repositories
If you need to uninstall node.js after installing it using this method, just run:
sudo apt remove nodejs
If you want to purge config files, too, add purge
:
sudo apt purge nodejs
How to install Nodejs using Node Version Manager
Node Version Manager (nvm) represents a convenient way to install and manage several different node.js versions. It's instrumental if you're using node in production, since it lets you test your app on and maintain multiple versions.
Notably, for our purposes, nvm will let you install any version alongside npm, relatively easily. Here's how you use it:
- Download and install nvm using curl and bash:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
- Source your .bashrc file using
source ~/.bashrc
. - Close and re-open your terminal or run the following commands so that you can start using nvm:
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
- Type
nvm list-remote
to return a very long list of node versions that you can install. Note down the version you want to install (the last entry should be the latest version). - Install node by typing
nvm install
followed by your chosen version number:
nvm install v22.6.0
It is also possible to use the alias for an lts version to install it. For example, nvm install lts/hydrogen
. You can check the version(s) you have installed at any point with nvm list
. Or, to switch to a different version, run nvm use [version number]
.
Uninstalling Node.js with nvm
To uninstall a node.js version with nvm, check your versions with nvm list
and run:
nvm uninstall [version_number]
Note that if you are trying to uninstall a node version that is currently active, you'll need to deactivate it first using nvm deactivate
.
Installing the latest Node.js via the Nodesource PPA
Nodesource maintains its own repository of Node.js versions. It is more "complete" than Ubuntu's official one, offering all Node versions that are still in support and some that aren't.
We'll show you how to use it to install the latest version of Node.js. You can check your latest version here. At the time of writing, it is 22.
- In your home directory, download the relevant Nodesource script using:
curl -sL https://deb.nodesource.com/setup_22.x -o nodesource_setup.sh
2. You can now run your downloaded setup script with bash.
sudo -E bash nodesource_setup.sh
3. Finally, install Node.js with sudo apt-get install -y nodejs
. You can check that it's the right version with node -v
after the setup completes.
Note that Nodesource automatically installs npm, so you do not need to install it manually.
How to uninstall Node.js from Nodesource
Uninstalling node.js installed via Nodesource is the same as the default repository:
sudo apt remove nodejs
However, if you want to reverse the changes made by the script, you will need to additionally run:
rm -fv /etc/apt/sources.list.d/nodesource*
apt-key del 1655A0AB68576280
Using Fast Node Manager to install Node
Fast node version manager offers a simplified and fast alternative to NVM, built in Rust. It's quick to install and allows you to install and manage various Node.js versions, from most recent to outdated.
Here are the steps to use it:
- Install FNM by running
curl -fsSL https://fnm.vercel.app/install | bash
. - Activate FNM using
source ~/.bashrc
. - Type
fnm list-remote
to see all available node versions. - Install a node version with
fnm install [version.number]
. You should enter just the version number, notv
or any other characters.
You can check your versions at any point using fnm list
and swtich between versions with fmn use
.
How to uninstall Node.js with FNM
You can remove your Node.js versions installed via NVM with the following command:
fnm uninstall [node.version]
Unlike Node Version Manager, this will work even if the version is set as your default.
How to install Node.js using Docker
If you use Docker, you can pull the official Node.js image to install it. After you install Docker, run the following command to grab the Docker image, substituting 22
for your chosen version number:
docker pull node:22-alpine
You can check the exact version of Node.js and nvm with:
docker run node:22-alpine node -v
docker run node:22-alipine npm -v
Closing words
The methods presented above are not the only ways to install Node.js. You could, for example, manually download the pre-built binaries from the official website or even build it from source. This guide should, however, give you a wide enough range of options that you don't need to resort to these more time-consuming methods.
As with any package you install, maintaining and updating nodejs is critical but easy to neglect. For this reason, we recommend you add the nodejs releases blog to your RSS feed or subscribe to a newsletter. Of course, if you are using Node.js in production, you should always read the changelog carefully, test, and deploy slowly.