Table of Contents
To follow along with this tutorial, you need to have:
- Ubuntu 20.04 set up.
- A user account with sudo privileges.
Installing Node.js on Ubuntu 20.04
This tutorial covers three ways you can use to install Node.js on Ubuntu 20.04 and discusses why you should choose one method over the other. These ways are:
- Installing from the Ubuntu repository.
- Installing Node.js from NodeSource.
- Using Node Version Manager (nvm).
Method-1: Installing from the Ubuntu repository
Perhaps the easiest way you can install Node.js is by installing it from the official Ubuntu repository. This repository however does not have the latest version of Node.js. If you are looking to just get started with Node.js, you can use this method.
The first step is to update the package index. Open your terminal and write the following:
sudo apt update
Next, run the command to install Node.js.
sudo apt install nodejs
To verify whether the install was successful, run:
You should see the following:
To install npm, run:
sudo apt install npm
Method-2: Installing Node.js from NodeSource
If you want an up-to-date version of Node.js, NodeSource offers different versions and you get to choose which one to install. As of the time of writing this article, v12, v14, v16, and v17 are available. Find the full list of the available versions on Github.
Add and execute the NodeSource installation script of the Node.js version you want to install using the following command:
curl -fsSL https://deb.nodesource.com/setup_17.x | sudo -E bash
If you do not have curl installed, install it using:
apt-get install curl -y
Now to install Node.js v17 run:
sudo apt install nodejs
Confirm your installation
Your output should be:
This package comes with npm and you, therefore, do not need to run a different install.
Method-3: Using Node Version Manager (nvm)
You can also use nvm to install Node.js. nvm is useful when you want to switch between different Node versions depending on the project you are working on. It allows you to install all the versions you need and specify the default.
To install nvm, you need to run the install script by either downloading it then executing it or by using cURL. In this example, we will be using cURL.
Copy the following command to your terminal:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
The above command will download the script and run it and in turn, create a ~/.nvm folder containing a clone of the nvm repository.
Note that you can get the updated version of the script on Github
Now, you need to source the nvm common before using it using this code:
This command will make your OS pick up on nvm. Close and restart your terminal to start using nvm.
You can now check whether nvm was installed properly.
command -v nvm
To see the available options, run:
Finally, use nvm to install a certain version of Node say v16.0.0
nvm install v16.0.0
To install the current LTS version,
nvm install --lts
One of the benefits of using nvm to install Node.js is the ability to use specific versions of Node.js. To do this, you should know how to first set the default version. This is the version that a new shell will default to when the nvm script is sourced.
nvm alias default 16.0.0
You can confirm whether it was set by checking the node version.
To switch to other versions, you have to install those versions first. You only need to specify the version number. For instance, installing v10.0.0 looks like this:
nvm install 10.0.0
To list all the installed versions:
To switch from one version to another, provide the version number to the nvm use command.
nvm use 14.4.0
You should see the following output
Now using node v14.4.0 (npm v6.14.5)
Uninstalling Node.js is as simple as running this command
sudo apt remove nodejs
In this article, we have looked at the three ways you can use to install Node.js on Ubuntu 20.04. Each of the methods listed serves different people with different. Needs. Installing Node.js from the Ubuntu repository is straightforward and therefore great for beginners. However, the default version of the repository is not up-to-date. To solve this, you can use a PPA like NodeSource that allows you to choose the version you need. Going on further, if you work on different projects that run on different Node.js versions, installing it from nvm is the best option. It will give you the ability to switch to different versions as per your project’s needs.
Here are some important links you might find useful: