How to Install Node.js and NPM on Ubuntu 20.04/18.04

Nodejs is a free, open-source, cross-platform JavaScript runtime environment built on Google Chrome V8 JavaScript Engine. The main advantage of Node is an asynchronous event-driven architecture that helps it to handle many concurrent connections. 

It is becoming more and more popular among web developers around the world. Nodejs is available for the Linux platform and here, we will see how to install Node.js and NPM on Ubuntu 20.04/18.04.

Install NodeJS on Ubuntu 20.04/18.04

There are 3 different ways to install NodeJS on Ubuntu. You can use the default Ubuntu repository, Binary provided by Nodesoure or using NVM script. Each has its own advantages and convenience.

We are covering all the installation methods below but, you should choose either. It is not a good idea to mix them up.

Method 1 – Install using default Ubuntu Repositories

Node is available in the official Ubuntu repositories. The main advantage here is the ease and stability. However, Ubuntu does not update the major version of its packages. That means the version of Node that is in the Ubuntu repositories may be outdated. That should be fine for most Linux Users. 

This is the way you can install it using the apt command.

Open the terminal and refresh the APT cache. Then, install Nodejs along with the NPM package manager as shown below. Enter ‘Y’ when the system prompts you to continue.

:~$ sudo apt update
:~$ sudo apt install nodejs npm

Now, let’s check the version of Node installed using the command given below.

:~$ nodejs -v
v10.19.0

Here, the version is 10.19.0 which is not the latest one. So, use the below alternate methods to get the latest version of Node installed.

Method 2 – Using an external repository(Node binary)

Node.JS gives us some external repositories to facilitate the installation. The interesting thing about this method is that there is one repository for each major version of Node. That is, we can choose between the 10.x, 12.x, 13.x, 15.x, and 16.x branches.

So you have the option to choose the old as well as the latest version of Node depending on your preference and requirement.

For Node.js v16.x use the below commands

:~$ curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
:~$ sudo apt install -y nodejs

For Node.js v15.x use the below command

:~$ curl -fsSL https://deb.nodesource.com/setup_15.x | sudo -E bash -
:~$ sudo apt install -y nodejs

For other versions, please visit the official binary distribution on GitHub where you will find installation instructions for the older versions. This page even maintains installation instructions for Debian, Redhat, CentOS Linux distribution.

Use the below command to check the version when installation is complete.

:~$ nodejs -V

With this, you will have the version of Node.JS that you prefer.

This method takes an extra step, but it is more flexible than the first one. It allows you to choose a branch of specific versions. While in the first one, you can only install the version of Node.JS that is in the Ubuntu repositories.

However, there is a third method available. An even more flexible one.

Method 3 – Node Using NVM

NVM is a script that allows handling different versions of NodeJS. However, it also allows you to install the version of Node.JS you want. That is why I said it is an even more flexible method.

First, download and install the NVM script using the command given below.

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

When the installation is complete, restart the terminal. Alternatively, you can refresh the bash profile of the active user. This is to make NVM command available.

:~$ source ~/.profile

That’s it and now you are ready to use NVM.

With this script, you can view all available versions of Node.JS. To do this, run this command with the ls-remote option as shown below.

:~$ nvm ls-remote

This lists down all the versions available for the installation. The list is huge starting from version 0.x to 16.x. Now, it is up to you which version you want.

This is the way you can use nvm. Just follow the install with the version number.

nvm install <version>

Let see the command in action for the latest version on Node.

First, change the permission using chmod command and then complete the installation as shown below.

:~$ chmod 777 -R ~/.nvm/
:~$ nvm install 16.1.0

Check the installed version, to make sure everything is ok.

:~$ node -v
v16.1.0

Note that in this case, the command is the node and not nodejs.

This method is perhaps the most cumbersome of all. However, it brings us an invaluable advantage: we can choose specifically which version of Node.JS to install.

Also, having NVM installed in the system is a great help to facilitate working with different versions of Node.JS.

Conclusion

Node.JS is a quite useful tool in many projects nowadays. Much of the value has to do with the size of the project in question. But it is becoming more and more popular in all sorts of projects

The reason for this is that the development of Node.JS is quite active and brings us versions frequently. All to improve it and increase the possibilities of development.

Today in this post, you have learned several methods to install Node.JS in Ubuntu. Each of them has a degree of difficulty, as well as its advantages. Perhaps the one that combines ease and flexibility is the second. However, it is up to you which method to choose and which one suits your needs.

I hope you liked this post. Please share.

Scroll to Top