Jump to content

How to Install Node.js and NPM in Ubuntu


Linux Hint

Recommended Posts

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

word-image-174956-1.png

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.

word-image-174956-2.png

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.

word-image-174956-3.png

For NPM, use the following command to install it:

$ sudo apt-get install npm -y

word-image-174956-4.png

Once installed, check the NPM version.

word-image-174956-5.png

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

word-image-174956-6.png

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

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

word-image-174956-7.png

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

$ sudo chmod 500 nsolid_setup_deb.sh

word-image-174956-8.png

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

word-image-174956-9.png

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

$ sudo apt-get install nodejs -y

word-image-174956-10.png

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.

word-image-174956-11.png

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.

word-image-174956-12.png

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

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

word-image-174956-13.png

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

word-image-174956-14.png

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

word-image-174956-15.png

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

word-image-174956-16.png

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

$ sudo apt-get update

Lastly, install Node.js.

$ sudo apt-get install nodejs -y

word-image-174956-17.png

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

word-image-174956-18.png

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.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...