Contents
Option 1 — Installing Node.js with Apt from the Default Repositories
Ubuntu 20.04 contains a version of Node.js in its default repositories that can be used to provide a consistent experience across multiple systems. At the time of writing, the version in the repositories is 10.19. This will not be the latest version, but it should be stable and sufficient for quick experiments with the language.
Warning: The version of Node.js in Ubuntu 20.04, version 10.19, is no longer supported and is unmaintained. You should not use this version in production, and you should refer to one of the other sections in this tutorial to install a more recent version of Node.js.
To get this version, you can use the apt package manager. First refresh your local package index by typing:
sudo apt update
Then install Node.js:
sudo apt install nodejs
Check that the installation was successful by querying nodefor its version number:
node -v
Outputv10.19.0
If the package in the repositories suits your needs, that is all you need to do to install Node.js. In most cases, you will also want to install npm, the Node.js package manager. You can do this by installing the npm package with apt:
sudo apt install npm
This allows you to install modules and packages to use with Node.js.
At this point, you have successfully installed Node.js and npm using apt and the default Ubuntu software repositories. The next section will show how to use an alternative repository to install different versions of Node.js.
Option 2 — Installing Node.js with Apt Using a NodeSource PPA
To install a different version of Node.js, you can use a PPA (personal package archive) provided by NodeSource. These PPAs have more Node.js versions than the official Ubuntu repositories. Node.js v12, v14 and v16 are available at the time of writing.
First, we will install the PPA in order to access its packages. From your home directory, use curl to retrieve the installation script for your preferred version, making sure to replace 16.x with your preferred version string (if different):
cd ~
curl -sL https://deb.nodesource.com/setup_16.x -o /tmp/nodesource_setup.sh
See the NodeSource documentation for more information on the available versions.
Inspect the contents of the downloaded script with nano (or your preferred text editor):
nano /tmp/nodesource_setup.sh
When you are satisfied that the script is safe to run, exit your editor and run the script with sudo:
sudo bash /tmp/nodesource_setup.sh
The PPA will be added to your configuration and your local package cache will be updated automatically. You can now install the Node.js package as you did in the previous section:
sudo apt install nodejs
Verify that you have installed the new version by running node with the -v version flag:
node -v
Outputv16.6.1
The NodeSource nodejs package contains both the nodebinary and npm, so you do not need to install npm separately.
At this point you have successfully installed Node.js and npm using apt and the NodeSource PPA. The next section will show how to use the Node Version Manager to install and manage multiple versions of Node.js.
Option 3 — Installing Node Using the Node Version Manager
Another particularly flexible way to install Node.js is to use nvm, the Node Version Manager. This piece of software allows you to install and maintain many different independent versions of Node.js, along with their associated Node packages, at the same time.
To install NVM on your Ubuntu 20.04 machine, visit the project's GitHub page . Copy the curl command from the README file displayed on the main page. This will give you the latest version of the installation script.
Before piping the command to bash, it is always a good idea to audit the script to make sure it does not do anything you do not agree with. You can do that by removing the | bashsegment at the end of the curl command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh
Take a look and make sure you are comfortable with the changes it makes. When you are satisfied, run the command again with | bash appended at the end. The URL you use will change depending on the latest version of nvm, but as of right now, the script can be downloaded and executed by typing:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
This will install the nvm script to your user account. To use it, you must first source your .bashrc file:
source ~/.bashrc
If you are using the interactive terminal to test installing Node.js with nvm, you need to source your ~/.bash_profile file instead. Use the following command to do so:
source ~/.bash_profile
Now, you can ask NVM which versions of Node are available:
nvm list-remote
Output. . .
v14.16.0 (LTS: Fermium)
v14.16.1 (LTS: Fermium)
v14.17.0 (LTS: Fermium)
v14.17.1 (LTS: Fermium)
v14.17.2 (LTS: Fermium)
v14.17.3 (LTS: Fermium)
v14.17.4 (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
That is a very long list! You can install a version of Node by typing any of the release versions you see. For instance, to get version v14.10.0, you can type:
nvm install v14.10.0
You can see the different versions you have installed by typing:
nvm list
Output-> v14.10.0
system
default -> v14.17.4 (-> N/A)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v14.10.0) (default)
stable -> 14.10 (-> v14.10.0) (default))
. . .
This shows the currently active version on the first line ( -> v14.10.0), followed by some named aliases and the versions those aliases point to.
Note: If you also have a version of Node.js installed through apt, you may see a system entry here. You can always activate the system-installed version of Node using nvm use system.
Additionally, you will see aliases for the various long-term support (or LTS) releases of Node :
Output. . .
lts/* -> lts/fermium (-> N/A)
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.4 (-> N/A)
lts/fermium -> v14.17.4 (-> N/A)
We can also install a release based on these aliases. For instance, to install the latest long-term support version, fermium, run the following:
nvm install lts/fermium
OutputDownloading and installing node v14.17.4...
. . .
Now using node v14.17.4 (npm v6.14.14))
You can switch between installed versions with nvm use:
nvm use v14.10.0
OutputNow using node v14.10.0 (npm v6.14.8)
```
You can verify that the install was successful using the same technique from the other sections, by typing:
```command
node -v
Outputv14.10.0
The correct version of Node was installed on our machine as we expected. A compatible version of npm is also available.