Ubuntu

How to Install Node.js and NPM in Ubuntu

As a developer, knowing how to install Nodejs and NPM is essential. Node.js is among the leading web technologies. When you want to create a local development environment, you must have installed Node.js and NPM on your system.

Node.js and NPM go hand in hand. While Node.js is the runtime environment, NPM is its package manager which you utilize when you want to install the dependencies and packages. So, how do you install Node.js and NPM in Ubuntu? We will cover three methods that you can use.

Three Methods to Install Node.js and NPM in Ubuntu

There are three options to install Node.js and NPM in Ubuntu. First, you can choose to use the default Ubuntu repository to install the available NPM and Node.js versions. Alternatively, you can utilize an installation script that is available from the Nodesource GitHub repository. Lastly, you can add the Nodesource GPG key, create a deb repository, and then install the latest or any other supported Node.js version.

Method 1: Using the Ubuntu Repository

Both Node.js and NPM packages are available on the official Ubuntu repository. This option works with Ubuntu 18 to the newest versions.

First, update the apt cache.

$ sudo apt update

Next, let’s start by installing Node.js with the following command:

$ sudo apt-get install nodejs

You will be required to enter your password and then press “y” to confirm the installation.

Run the “nodejs –version” command to check what Node.js version is installed. For this case, we have v12.22.9. Of course, this is not the latest version and that’s because we opted to source it from the Ubuntu repository.

For NPM, use the following command to install it:

$ sudo apt-get install npm -y

Once installed, check the NPM version.

That’s the first and simplest way of installing Node.js and NPM in Ubuntu.

Method 2: Using an Installation Script

If you access the Nodesource Github repository, there is a script that is created to ease the installation on DEB and RPM systems. To use this script, you must have a root access or use the sudo command.

First, ensure that you have curl on your system. You can use the following command to install it:

$ sudo apt-get install curl

Next, run the “curl” command to download the script.

$ sudo curl -SLO https://deb.nodesource.com/nsolid_setup_deb.sh

Find the downloaded script and give it the 500 file permission using the “chmod” command.

$ sudo chmod 500 nsolid_setup_deb.sh

To run the script, you must specify what Node.js version you want to install. For instance, to install Node.js 20, we run our script as follows:

$ sudo ./nsolid_setup_deb.sh 20

Lastly, use the “apt” command to install Node.js from the configured repository.

$ sudo apt-get install nodejs -y

When we check the installed Node.js and NPM versions, you will notice that we have Node.js v20 which is what we specified earlier. Its respective NPM is installed as well.

If you find this method ideal for installing a specific Node.js version, you can use the same steps, and you will have Node.js installed. As for NPM, you can’t specify its version. It automatically gets installed alongside Node.js.

Method 3: Using the Nodesource GPG Key

The last method that we will discuss to install Node.js and NPM in Ubuntu is using the Nodesource GPG key.

Start by refreshing the apt cache.

Next, install the required packages and dependencies by running the following command:

$ sudo apt-get install -y ca-certificates curl gnupg

Once installed, download the Nodesource GPG key and create a directory to host it in the “/etc/apt”. Still, use curl to create a deb repository for Nodesource. Run the following two commands:

$ sudo mkdir -p /etc/apt/keyrings

$ curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg

You must then specify the NODE_MAJOR which is the Node version that you want to install. For instance, let’s start by specifying v18.

$ NODE_MAJOR=18

Next, create the deb repository using the following “echo” command:

$ sudo apt-get install -y ca-certificates curl gnupg

If you want to have different Node.js versions that you can switch between when building your project, add them as follows:

Again, update your repository to accommodate the newly added repository.

$ sudo apt-get update

Lastly, install Node.js.

$ sudo apt-get install nodejs -y

When we check the Node.js version, it should match v18 which is the first NODE_MAJOR that we specified earlier.

You now successfully installed Node.js and NPM in Ubuntu.

Conclusion

The approach that you take to install Node.js and NPM depends on your goal and comfort. There are three methods that you can use. You can install Node.js and NPM from the Ubuntu repository. Alternatively, you can use an installation script. The last option is to rely on the Nodesource GPG key. All methods are covered in this post.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.