How to install Node.js on Linux Ubuntu?

How to install Node.js on Linux Ubuntu?

Hello Guys,

Today, we are going to learn how to install node.js on linux ubuntu 20.04. This tutorial goes into details on how to install node.js on Ubuntu 20.04. You will learn about node js installation on ubuntu in easy steps. I will show you three different methods of getting node.js installed on linux ubuntu.

  • Using apt command to install node.js from ubuntu's default software repository.
  • Using apt command with an alternate NodeSource PPA software repository to install the specific version.
  • Using Node Version Manager (NVM)

Note: Using apt ubuntu's default repository will be sufficient to install node.js. If you want a specific or legacy version of node.js, you can use PPA software repository. If you are already developing an application in node.js and wanna switch on different versions of node.js frequently, then you should use the NVM method.

So, let's get started.

Method 1: Installing Node.js with Apt from the Ubuntu's Default Repositories

By default, Ubuntu 20.04 has a version of Node.js in it's repositories. The version of Node.js in the repositories is 10.19.0. This is not the latest version of Node.js but it should be stable and sufficient for the quick experiment.

Warning: Node.js 10.19 version is no longer supported and maintainable. You should not use this version in production.

To install this version, you can simply use the apt package manager. First update your local package index with the following command

sudo apt update

Then install node.js with the following command

sudo apt install nodejs

You can check if nodejs is installed with the following command

nodejs -v

Output

v10.19.0

If the package in the repository is enough to get your desired result, then this is all to get set up with Node.js.

In most cases, you have to install node package manger (npm) also. You can install npm on ubuntu by using the following command.

sudo apt install npm

This command allows you to install modules and packages that will be used with Node.js.

At this point, you have successfully installed nodejs and npm using apt from the ubuntu default software repositories. Let's check now how to install different nodejs versions with PPA, an alternate repository.

 

Method 2: Installing Node.js with Apt from the NodeSource PPA

You can use Personal Package Archive (PPA) to install different versions of node.js. It has more versions of node.js than the ubuntu default software repository. 16 and 18 versions of nodejs are available as of the time of writing.

First, you have to install PPA to get access to its packages. Use curl request to get the installation script for your preferred version, making sure to replace 16.x with your required version if you need a different version. Run the following command from your home directory. Check here for available versions.  

curl -sL https://deb.nodesource.com/setup_16.x -o /tmp/nodesource_setup.sh

You can validate downloaded file scripts with nano or your preferred text editor in bash.

nano /tmp/nodesource_setup.sh

When you are satisfied that the downloaded script is safe to run. Then you can run the script with sudo command just like the mentioned below. 

sudo bash /tmp/nodesource_setup.sh

The Personal Package Archive (PPA) will be added to your configuration and your local package directory will be updated automatically. Now, you can install the NodeJs by the following command, same as you did in the first method.

node -v

Output

v10.19.0

The package contains both the node and npm packages, so you don't need to install npm separately.

At this point, you have successfully installed NodeJs and npm package from the NodeSource PPA using apt. In the next method, you will learn how to use the Node Version Manager to install and manage multiple versions.

 

Method 3: Using Node Version Manager (NVM)

You can install NodeJs pacakge with the Node Version Manager (NVM) that allows you to install and maintain many different independent versions of NodeJs and their associated packages.

To install the Node Version Manager, run the following command. You can copy this command from github repo also. 

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

This command will install the nvm script to your system. To use it, you must have to source your .bashrc file. Run the following command to do this. 

source ~/.bashrc

Now, you can check the available versions of NodeJs pacakge by running the following command:

nvm list-remote

Output

....
v18.0.0
v18.1.0
v18.2.0
v18.3.0
v18.4.0
v18.5.0
v18.6.0
v18.7.0
v18.8.0
v18.9.0
v18.9.1
v18.10.0
v18.11.0
v18.12.0   (LTS: Hydrogen)
v18.12.1   (LTS: Hydrogen)
v18.13.0   (Latest LTS: Hydrogen)
v19.0.0
v19.0.1
v19.1.0
v19.2.0
v19.3.0
v19.4.0

As you can see, there are many versions of NodeJs available. You can install any version of NodeJs by writing in any of the release versions listed.

nvm install 16.3.0

Or you can install the latest version with the following command.

nvm install node # "node" is an alias for the latest version

You can check the different versions that you have installed on your system with the following command.

nvm list

Output

->     v14.10.0
       v14.21.2
default -> v14.10.0
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v14.21.2) (default)
stable -> 14.21 (-> v14.21.2) (default)
. . .

It will show the currently active version , followed by some named aliases. 

You can switch between versions that you have installed with the following command.

nvm use v14.10.0

Output

Now using node v14.10.0 (npm v6.14.8)

 

Conclusion

In conclusion, installing Node.js package on Linux Ubuntu is a simple process that can be done by using a few commands. By following the three methods in this guide, you can easily install Node.js on your Ubuntu system and start developing applications using this powerful JavaScript runtime environment. Once installed, you can switch to other Nodejs versions if you have installed different versions of Node.js.


I hope it will help you to install NodeJs on your system.

Happy Coding :)

Leave a Comment

Your email address will not be published. Required fields are marked *

Go To Top
×