Install Node.js on Ubuntu 20.04 [3 Different Methods]


NodeJS

Introduction

Node.js is an open-source runtime environment that uses JavaScript on the server-side. It is built on Chrome’s V8 JavaScript environment. Node.js is a cross-platform environment and supports Windows, Mac Os, Linux,  Unix, and other platforms. While languages like PHP and ASP are synchronous, Node.js offers asynchronous programming. It uses callback functions that eliminate the need for a program to wait for requests to be handled before it is executed. Asynchronous programming is non-blocking which is advantageous as it is fast and memory efficient.

 

Prerequisites

To follow along with this tutorial, you need to have:

 

Installing Node.js on Ubuntu 20.04

This tutorial covers three ways you can use to install Node.js on Ubuntu 20.04 and discusses why you should choose one method over the other. These ways are:

  • Installing from the Ubuntu repository.
  • Installing Node.js from NodeSource.
  • Using Node Version Manager (nvm).

 

Method-1: Installing from the Ubuntu repository

Perhaps the easiest way you can install Node.js is by installing it from the official Ubuntu repository. This repository however does not have the latest version of Node.js. If you are looking to just get started with Node.js, you can use this method.

The first step is to update the package index. Open your terminal and write the following:

sudo apt update

Next, run the command to install Node.js.

sudo apt install nodejs

To verify whether the install was successful, run:

node -v

You should see the following:

v10.19.0

You may also need to install the Node Package Manager (npm). It is the largest package repository and is useful for downloading dependencies to your JavaScript program. Npm will also help you manage the versions of the dependency you are downloading from its repository and also supports commands that run specified tasks. It is therefore an essential tool in Node.js development.

To install npm, run:

sudo apt install npm

 

Method-2: Installing Node.js from NodeSource

If you want an up-to-date version of Node.js, NodeSource offers different versions and you get to choose which one to install. As of the time of writing this article, v12, v14, v16, and v17 are available. Find the full list of the available versions on Github.

Add and execute the NodeSource installation script of the Node.js version you want to install using the following command:

curl -fsSL https://deb.nodesource.com/setup_17.x | sudo -E bash

If you do not have curl installed, install it using:

apt-get install curl -y

Now to install Node.js v17 run:

sudo apt install nodejs

Confirm your installation

node -v

Your output should be:

v17.3.1

This package comes with npm and you, therefore, do not need to run a different install.

 

Method-3: Using Node Version Manager (nvm)

You can also use nvm to install Node.js. nvm is useful when you want to switch between different Node versions depending on the project you are working on. It allows you to install all the versions you need and specify the default.

To install nvm, you need to run the install script by either downloading it then executing it or by using cURL. In this example, we will be using cURL.

Copy the following command to your terminal:

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

The above command will download the script and run it and in turn, create a ~/.nvm folder containing a clone of the nvm repository.

Note that you can get the updated version of the script on Github

Now, you need to source the nvm common before using it using this code:

source ~/.bashrc

This command will make your OS pick up on nvm. Close and restart your terminal to start using nvm.

You can now check whether nvm was installed properly.

command -v nvm

To see the available options, run:

nvm list-remote

Finally, use nvm to install a certain version of Node say v16.0.0

nvm install v16.0.0

To install the current LTS version,

nvm install --lts

One of the benefits of using nvm to install Node.js is the ability to use specific versions of Node.js. To do this, you should know how to first set the default version. This is the version that a new shell will default to when the nvm script is sourced.

nvm alias default 16.0.0

You can confirm whether it was set by checking the node version.

To switch to other versions, you have to install those versions first. You only need to specify the version number. For instance, installing v10.0.0 looks like this:

nvm install 10.0.0

To list all the installed versions:

nvm ls

To switch from one version to another, provide the version number to the nvm use command.

nvm use 14.4.0

You should see the following output

Now using node v14.4.0 (npm v6.14.5)

 

Uninstalling Node.js

Uninstalling Node.js is as simple as running this command

sudo apt remove nodejs

 

Summary

In this article, we have looked at the three ways you can use to install Node.js on Ubuntu 20.04. Each of the methods listed serves different people with different. Needs. Installing Node.js from the Ubuntu repository is straightforward and therefore great for beginners. However, the default version of the repository is not up-to-date. To solve this, you can use a PPA like NodeSource that allows you to choose the version you need. Going on further, if you work on different projects that run on different Node.js versions, installing it from nvm is the best option. It will give you the ability to switch to different versions as per your project’s needs.

 

Learn More

Here are some important links you might find useful:

 

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment