How to Install Node.js and npm on Ubuntu 20.04

Choose a different version or distribution

Introduction

Before we begin talking about how to install Node.js and npm on Ubuntu 20.04, let's briefly understand – What is Node.js?

Node.js is a powerful, open-source JavaScript runtime environment that enables developers to build scalable and high-performance web applications. It uses an event-driven, non-blocking I/O model, making it efficient for handling multiple requests simultaneously.

Node.js is widely used for creating server-side applications, real-time applications, and microservices. Its lightweight nature and extensive library ecosystem make it a popular choice for developers seeking speed and flexibility in web development.

In this tutorial, We'll look at three possible ways to install Node.js and npm on Ubuntu 20.04.

Advantages of Node.js

  1. High Performance: Node.js uses a non-blocking, event-driven architecture, allowing it to handle numerous concurrent requests efficiently.
  2. Scalability: It enables the creation of scalable applications, thanks to its ability to handle a large number of connections with minimal resource consumption.
  3. Fast Development: Node.js employs JavaScript on both the client and server, enabling developers to use a single language throughout the entire development process.
  4. Extensive Library Ecosystem: Node.js has a vast collection of libraries and modules, known as the Node Package Manager (NPM), which aids in rapid application development.
  5. Real-time Applications: Node.js is particularly suited for real-time applications, such as chat applications or collaborative tools, where instant data updates are crucial.

We'll look at three possible ways to install Node.js and npm on Ubuntu 20.04 in this tutorial:

  • The regular Ubuntu repositories are used. This is the simplest approach to get Node.js and npm installed on Ubuntu, and it should suffice for most purposes. 10.19.0 is the version found in the Ubuntu repositories.
  • This is taken from the NodeSource repository. If you wish to install a Node.js version other than the one included in the Ubuntu repositories, use this repository. NodeSource currently supports Node.js versions 14.x, 13.x, 12.x, and 10.x.
  • Using nvm as a virtual machine (Node Version Manager). You can use this utility to install various Node.js versions on the same system. This is the preferred method of installing Node.js if you are a Node.js developer.

Select the installation technique that is best for your situation. Consult the documentation for the application you're intending to deploy if you're not sure which Node.js version to install.

Install Node.js and npm from the Ubuntu Repository

The Node.js version supplied in the Ubuntu 20.04 repositories at the time of writing is v10.19.0, which is the previous TLS version.

The process of installation is rather simple. To update the package index and install Node.js and npm, run the following commands:

sudo apt update
sudo apt install nodejs npm

The above command will install a variety of packages, including the tools needed to compile and install native npm addons.

After that, execute the following command to check the installation:

nodejs --version
v10.19.0

Installing Node.js and npm from NodeSource

NodeSource is a company that specializes in enterprise-level Node support. It keeps track of multiple Node.js versions in an APT repository. If your application requires a certain version of Node.js, use this repository.

The following versions are available in the NodeSource repository at the time of writing:

  • v14.x - Latest stable version.
  • v13.x
  • v12.x - The latest LTS version.
  • v10.x - The previous LTS version.

We will install version 14.x of NodeJS:

curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -

The script will install all essential packages, generate an apt repository file, and refresh the apt-cache after adding the NodeSource signing key to your machine.

If you require a different Node.js version, such as 12.x, replace setup 14.x with setup 12.x.

  • Install Node.js and npm once the NodeSource repository is enabled:
sudo apt install nodejs

Both the node and npm binaries are included in the nodejs package.

  • Print the versions of Node.js and npm to ensure that they were successfully installed:
node --version
Output

v14.21.3
npm --version
6.14.18

You'll need to install the following development tools in order to compile native addons from npm:

sudo apt install build-essential

Installing Node.js and npm using NVM

NVM (Node Version Manager) is a bash script that lets you manage different Node.js versions per user. You can use NVM to install and remove any version of Node.js that you want to utilize or test.

To download and install the nvm script, go to the nvm GitHub repository website and copy the curl or wget command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

Make sure not to use sudo, otherwise nvm will be enabled for the root user.

The script will clone the project's Github repository to the ~/.nvm directory as follows:

=> Close and reopen your terminal to start using nvm or run the following to use it now:

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

You should either close and reopen the terminal or use the instructions to add the path to the nvm script to the current shell session, as indicated by the output above. You are free to do whatever is most convenient for you.

Verify that nvm was properly installed after the script is on your PATH by typing:

nvm --version
Output

0.35.3

Run the following command to receive a list of all Node.js versions that can be installed with nvm:

nvm list-remote

The command will output a long list of all Node.js versions that are currently available.

Run the following command to install the most recent version of Node.js:

nvm install node

You will get an output like below:

Output

...
Checksums matched!
Now using node v19.8.1 (npm v9.5.1)
Creating default alias: default -> node (-> v19.8.1)

Verify that the installation was successful by printing the Node.js version:

node --version
v19.8.1

Let's install the most recent LTS version as well as version 10.9.0:

nvm install --lts
nvm install 10.9.0

You may get a list of the Node.js versions installed by typing:

nvm ls

You will get an output like below:

Output

>      v10.9.0
       v12.16.3
        v14.2.0
default -> node (-> v14.2.0)
node -> stable (-> v14.2.0) (default)
stable -> 14.2 (-> v14.2.0) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/erbium (-> v12.16.3)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.20.1 (-> N/A)
lts/erbium -> v12.16.3

The Node.js version used in the current shell session is shown by an arrow on the right (> v10.9.0), and the default version is set to v14.2.0. The default version is the one that is used when creating new shells.

If you wish to modify the version that is presently active, type:

nvm use 18.15.0
Output

Now using node v18.15.0 (npm v9.5.0)

Run the following command to update the default Node.js version:

nvm alias default 18.15.0

Visit the project's GitHub page for more details on how to utilize the nvm script.

FAQs to Install Node.js and npm on Ubuntu 20.04

How do I update Node.js to the latest version?

To update Node.js on Ubuntu 20.04, use the package manager. Run sudo apt update to update the package list, followed by sudo apt upgrade nodejs to upgrade Node.js to the latest version.

How do I install a specific version of Node.js?

Use the Node Version Manager (NVM) to install and manage multiple versions of Node.js on Ubuntu 20.04. You can easily switch between versions using the nvm use command.

How do I uninstall Node.js from Ubuntu 20.04?

To uninstall Node.js from Ubuntu 20.04, you can use the package manager. Run sudo apt remove nodejs to remove Node.js from your system.

What is npm?

npm is the default package manager for Node.js. It allows you to install, manage, and publish JavaScript packages/modules that can be used in your Node.js projects.

How do I update npm to the latest version?

To update npm to the latest version, you can use the following command: sudo npm install -g npm@latest. This will update npm globally on your system.

Conclusion

On your Ubuntu 20.04 server, we've shown you three different ways to install Node.js and npm. The approach you select is determined by your needs and preferences. Although the bundled version from the Ubuntu or NodeSource repositories is easier to install, the nvm technique allows you to add and remove different Node.js versions on a per-user basis.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.