How to Install Node.js on Ubuntu 20.04 – Made Easy

Introduction

Node.js is an open-source, cross-platform, JavaScript runtime environment for general-purpose programming. It is lightweight and built based on Chrome’s V8 JavaScript engine. It is used to build fast and scale-able web applications on the server-side. It is one of the most widely used web technologies.

NPM (Node.js Package Manager) is an open-source library of Node.js packages. It is used to install and manage package dependencies.

In this article, you’ll download and install Node.js on Ubuntu 20.04 using Ubuntu repository(APT), Node source repository and NVM, remove Node.js and test it by running a Node.js web-server.

Methods to Install Node.js on Ubuntu

  • Official Ubuntu repositories – It will not provide you the LTS (Latest Stable Version). Instead, you get the version that is tested and verified by Ubuntu. (Currently v10.23.0)
  • Node Source repository You can choose Node Source if you want a different version than the one offered by the Ubuntu repository. Currently, Node Source supports Node.js v15.x, v14.x, v13.x, v12.x, and v10.x.
  • Node Version Manager – You can install multiple Node.js versions on the same machine. If you are a Node.js developer, then this is the preferred way to install Node.js on Ubuntu.

Prerequisites

Before you start, you’ll need the following.

  • If you do not have servers in the cloud, Create an AWS EC2 Ubuntu server instance by following the guide How to launch an EC2 Instance.
  • [Important] – Update the packages list in the server which is upgrade-able using sudo apt update
  • [Important] – Upgrade the packages in the server to the latest versions using sudo apt upgrade

Using Ubuntu’s Official Repository

Ubuntu 20.04 contains an earlier stable version of Node.js in its repositories. At the time of writing, the version in the repositories is v10.23.0. You’ll find an early version because the Ubuntu team will test the versions before hosting them in their repository.

To install Node.js, execute the following command.

sudo apt install nodejs

You’ll get a stable version of Node.js installed from the Ubuntu repository. This is enough for new developers to work on their skills. Still, you might need NPM to install other packages to work with Node.js.

To install npm, execute the following command.

sudo apt install npm

You’ve successfully installed Node.js and NPM.

To verify the installation of Node.js, execute the following command.

nodejs -v

You’ll see the below output.

Output

v10.23.0

To verify the installation of NPM, execute the following command.

npm -v

You’ll see the below output.

Output

6.14.8

You’re all set to work on the Node.js stable version of Ubuntu.

But in most cases, you might need the latest version to get the support for the newest packages. In the next section, you’ll install the Latest Stable Version of Node.js.

Using Node Source Repository

Node source repository contains the latest version always. To learn more about the Node Source, visit their official page.

In order to install the latest stable version of Node.js, you can use the PPA (Personal Package Archive) which is maintained by NodeSource. This will hold more up-to-date versions of Node.js. You can choose between Node.js v10.x (LTS version), Node.js v12.x (Active LTS version), Node.js v15.x (Current version), and also any other version from the official Github page.

First, install curl (if you already don’t have) using the below command.

sudo apt install curl

Next, install the PPA to get access to its contents. From your home directory, use curl to retrieve the installation script for your preferred version. If you prefer another version, replace 15.x with your preferred version string.

curl -sL https://deb.nodesource.com/setup_15.x -o nodesource_file.sh

You can read the content of the file using an editor of your choice, you will use nano in this tutorial.

If you would like to view the contents, use the below command. ( nano is used to view the content, Press CTRL + X to exit the nano).

nano nodesource_setup.sh

Now run the script using the below command. It’ll start the node source setup.

sudo bash nodesource_setup.sh

The PPA will be added to your configuration and your local package cache will be updated automatically.

Next, install Node.js and NPM using the below command.

sudo apt install nodejs

NodeJS is installed.

To check the installation, use the below command.

node -v

You’ll see the below output.

Output

10.23.0

You’ve successfully setup Node.js and NPM using Nodesource repository.

In the next step, you’ll tackle how to manage multiple versions and multiple instances.

Using NVM

In this step, you’ll find yet another way (preferred in most cases) to install Node.js on Ubuntu 20.04.

You’ll use NVM which stands for Node Version Manager. NVM is a practical tool for managing multiple Node.js versions.

This helps in creating multiple versions of the node running in the same system which is made possible due to the fact that nvm works at the level of the independent directory.

You can access the newest versions of Node.js at the same time manage previous releases with the help of NVM.

The versions of Node.js that you manage with apt are distinct from the versions you manage with NVM.

First, install curl, if you have not installed it already, using the below command. (Hopefully, You should have this installed, if you have not skipped the previous section).

sudo apt install curl

Next, download the nvm installation script from the official GitHub page using the curl.

The version number may differ from the one highlighted here

curl -sL https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh -o nvm.sh

You can view the content in the script file using the following command. You can prefer any editor of your choice.

nano nvm.sh

Next, run the command below to execute the script file.

bash nvm.sh

You’ll see the below output.

Output

=> Compressing and cleaning up git repository

=> Appending nvm source string to /home/ubuntu/.bashrc
=> Appending bash_completion source string to /home/ubuntu/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:

You can either log out and log in again, reopen the terminal (as mentioned in the above output), or you can use the below command to save the changes to the session.

source ~/.profile

Now that you have successfully installed NVM, you can install multiple versions of Node.js using NVM.

To look into the versions of Node.js that NVM provides, execute the below command.

nvm ls-remote

You’ll see the below output.

Output

       v14.9.0
       v14.10.0
       v14.10.1
       v14.11.0
       v14.12.0
       v14.13.0
       v14.13.1
       v14.14.0
       v14.15.0   (LTS: Fermium)
       v14.15.1   (LTS: Fermium)
       v14.15.2   (LTS: Fermium)
       v14.15.3   (Latest LTS: Fermium)
        v15.0.0
        v15.0.1
        v15.1.0
        v15.2.0
        v15.2.1
        v15.3.0
        v15.4.0
        v15.5.0

You can install Node.js by specifying the version number from the above list using the below command.

nvm install 14.15.3

Now, you have installed a version using NVM, you can go ahead and use the version using the below command.

nvm use 14.15.3

You’ll see the below output.

Output

Now using node v14.15.3 (npm v6.14.9)

Since there are so many versions available, it is normal that you may forget which version is the current version.

You can find the current version using the below command.

node -v

You’ll see the below output.

Output

v14.15.3

You can identify the versions that are installed in your system using the below command.

nvm ls

You can define a particular version as default version using the alias command as shown below.

nvm alias default 14.15.3

You’ll see the below output.

Output

default -> 14.15.3 (-> v14.15.3)

Each and every version of Node.js in NVM will have a separate npm to track the packages installed for the particular version.

To learn more about the nvm, you can use the below command.

nvm help

It is advisable to use the LTS for professional purpose.

You’ve successfully completed the installation of Node.js using nvm. In the next step, you’ll remove a particular version or all versions of Node.js.

Removing Node.js [Optional]

In this step, you’ll remove Node.js version using the apt command. Removing unnecessary files are as important as installing it.

The following command is used to remove the stable version installed from Ubuntu repository. This command saves the configuration files that are downloaded during installation.

sudo apt remove nodejs

In order to delete Node.js along with configuration files execute the below command.

sudo apt purge nodejs

In order to delete the additional files that are installed along with node.js installation, execute the below command.

sudo apt autoremove

To remove the current active version of Node.js.

First, use the below command to check the current active version.

nvm current

You’ll see the below output.

Output

v14.15.3

Next, remove the version based on its version number, if it is not the current active version, using the below command.

nvm uninstall node_version

If the version you have to remove is the current version. Then, deactivate the version using the below command.

nvm deactivate

Now you can remove the version using the remove command followed by the version number. This command removes the files associated with this version. Note that the cache files are still not removed as these can be used when the version is reinstalled.

Creating Web Server [Optional]

This is an optional step. If you want to test your node.js installation, you’ll create a web server with “Ask Vikram!” text.

First, create a file server.js using the below command.

nano server.js

You will see nano editor has opened. Type the below code into the file.

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Ask Vikram!\n');
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');

After typing, Press CTRL + X to exit. Press Y to save the file.

Now type the following command into the terminal to run the web-server.

node server.js

You will see the following output which confirms that the server is running and Node.js is installed successfully. You can open the following link in your browser to see the output.

Output

Server running at http://127.0.0.1:3000/

Conclusion

In this article, you have installed Node.js using the Ubuntu repository, NodeSource repository, and NVM. You have also removed Node.js versions and run a simple server using Node.js.

What Next?

You’ve installed Node JS. If you would like to install a database, you shall use the guide, How to install PostgreSQL on Ubuntu.

<Watch this space for more updates on Blog>

Leave a Comment

Share via
Copy link
Powered by Social Snap