In this article, we want to teach you How To Install Node.js on Ubuntu 20.04.
How To Install Node.js on Ubuntu 20.04
To complete this article, you need to log in to your server as a non-root user with sudo privileges. To do this you can check our article about the Initial server setup with Ubuntu 20.04.
In this article, you learn to install Node.js on your server in three ways:
- Use Ubuntu’s default repository
- With an alternate PPA software repository
- Also, you can use NVM ( Node Version Manager)
For many users, installing Node.js with apt the Ubuntu default repository is sufficient. If you need newer versions of Node, it’s better to use the PPA repository. If you are developing Node applications and need to switch between node versions frequently, choose the NVM.
Install Node.js from the Ubuntu’s Default Repository
In Ubuntu’s default repository there is a 10.19 version of Node.js. this is not the latest version of Node.js, but it should be stable and sufficient for quick experimentation with the language.
Warning: the 10.19 version of Node.js included with Ubuntu 20.04, is now unsupported and unmaintained. It’s recommended to install a more recent version of Node with other ways in this article.
First, update the local package index with the following command:
sudo apt update
Then, install Node.js with the following command:
sudo apt install nodejs
Verify your installation by using this command:
Also, you may need to install modules and packages to use with Node.js. To do this, you need to install NPM (Node Package Manager) with the following command:
sudo apt install npm
At this point, you installed Node.js and NPM with the default Ubuntu repository. Let’s see how to use PPA to install different versions of Node.js on Ubuntu 20.04.
Install Node.js with PPA Software Repository
You can use PPA (Personal Package Archive) maintained by NodeSource to install a different version of Node.js. These PPAs have more versions of Node.js available than the official Ubuntu repositories.
Check the NodeSource Documentation for the latest version of Node.js.
From your home directory, use the curl command to get the installation script for your preferred version of Node.js:
cd ~ curl -sL https://deb.nodesource.com/setup_17.x -o nodesource_setup.sh
Then, run the bash script with the following command:
sudo bash nodesource_setup.sh
With this command, the PPA will be added to your configuration and your local package cache will be updated automatically.
Now install Node.js with the following command:
sudo apt install nodejs
You can verify your installation with the new version with the following command:
You don’t need to install NPM separately, the NodeSource has both Node.js and NPM.
Let’s see how to use NVM to install multiple versions of Node.js on Ubuntu 20.04.
Install Node.js with NVM
Another way to install Node.js is to use NVM (Node Version Manager). This software allows you to install and keep different independent versions of Node.js, and their related Node packages, at the same time.
Visit the Project’s GitHub Page to install NVM on Ubuntu 20.04. Then, copy the curl command from the README file that shows on the main page.
It’s recommended to audit the script to be sure it isn’t doing anything you don’t agree with.
To do this, remove the bash segment at the end of the curl command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh
Check and make sure you are ok with the changes it is making. Then, run the command again with the bash segment:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
This command will install the NVM script. Now you need to source the .bashrc file:
List the available version of Node with the following command:
Output v14.17.6 (LTS: Fermium) v14.18.0 (LTS: Fermium) v14.18.1 (Latest LTS: Fermium) v15.0.0 v15.0.1 v15.1.0 v15.2.0 v15.2.1 v15.3.0 v15.4.0 v15.5.0 v15.5.1 v15.6.0 v15.7.0 v15.8.0 v15.9.0 v15.10.0 v15.11.0 v15.12.0 v15.13.0 v15.14.0 v16.0.0 v16.1.0 v16.2.0 v16.3.0 v16.4.0 v16.4.1 v16.4.2 v16.5.0 v16.6.0 v16.6.1 v16.6.2 v16.7.0 v16.8.0 v16.9.0 v16.9.1 v16.10.0 v16.11.0 v16.11.1 v17.0.0
As you can see the latest LTS version is 14.18.1. To install it run the following command:
nvm install v14.18.1
Now you can check the different versions of Node.js that you have installed with the following command:
In your output you will see something similar to this:
Output -> v14.18.1 system default -> v14.18.1 iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v14.18.1) (default) stable -> 14.18 (-> v14.18.1) (default) ...
Also, you will see aliases for the different long-term support or LTS releases of Node.js:
Output ... lts/* -> lts/fermium (-> v14.18.1) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.17.0 (-> N/A) lts/dubnium -> v10.24.1 (-> N/A) lts/erbium -> v12.22.7 (-> N/A) lts/fermium -> v14.18.1
You can install a release based on these aliases too. for example, install the LTS erbium, with the following command:
nvm install lts/erbium
Output Now using node v12.22.7 (npm v6.14.15)
Now you can switch between installed Node.js versions with the following command:
nvm use v14.18.1
Output Now using node v14.18.1 (npm v6.14.15)
Then, verify the installation with the following command:
This means that the correct version of Node is installed on Ubuntu 20.04.
Let’s see how to remove Node.js on your server.
Remove Node.js on Ubuntu 20.04
If you installed Node.js from the default Ubuntu repository or PPA, you can remove the Node.js with the following command:
sudo apt remove nodejs
It will remove the Node package and the configuration files.
If you have enabled Node.js with NVM, first, check the currently active version with the following command:
If the version that you want to remove is the current active version, you need to deactivate it first with the following command:
Then, uninstall it with the following command:
nvm uninstall node_version
If the version you want to remove is not your current version, directly run the above command.
At this point, you learn How To Install Node.js on Ubuntu 20.04 in different ways. Also, you can easily remove it from your server.
Hope you enjoy it.