Install and Use Node.js on Ubuntu 20.04

Node.js is a powerful JavaScript runtime. It’s a free and open-source cross-platform solution that’s primarily for server-side programming. It allows developers to have scalable backend functionality using JavaScript. Most of the time, it’s used to create back-end apps. However, it’s also popular for full-stack and front-end solutions.

In this guide, check out how to install and use Node.js on Ubuntu 20.04.

Node.js on Ubuntu

Node.js is quite popular for scaling backend functionalities. In the case of Ubuntu, there are multiple sources to grab Node.js. Various methods will install different versions of Node.js. You can also manually select which one to go for.

Use the method that suits your needs the best.

Install Node.js from Ubuntu repos

This is the default method of installing Node.js on Ubuntu. For most of the users, this will be more than enough. The only downside is, you may not get the latest version of Node.js.

The installation is super simple. Update the APT cache and install Node.js along with npm (Node Package Manager).

$ sudo apt update && sudo apt install nodejs npm -y

Let’s run a quick test to verify the installation.

$ nodejs --version

Install Node.js from NodeSource PPA

NodeSource is a dedicated PPA that offers multiple versions of Node.js. I recommend this method over other ones as it offers more control. For advanced users, it also allows deciding the exact version of Node.js to install. At the time of writing this article, NodeSource PPA hosts Node.js v10, v12, v13, and v14.

Here, I’ll be showcasing how to configure NodeSource PPA for Node.js v14. If you want to install a different version of Node.js, check out the NodeSource readme for proper instruction.

First, make sure that your system has curl installed.

$ sudo apt update && sudo apt install curl -y

Now, run the NodeSource installation script.

$ curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -

Voila! NodeSource PPA for Node.js v14 is successfully configured! Install Node.js.

$ sudo apt install nodejs -y

Verify the installation by checking the version of Node.js.

$ node -v

Install Node.js using nvm

It’s an interesting way of installing Node.js. The nvm (Node Version Manager) is a tool that allows installing and maintaining multiple versions of Node.js along with associated Node packages independently. Check out nvm at GitHub.

To install nvm, run either of the following commands. Either of them will download the nvm install script and run it.

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

$ wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

Close and re-open the terminal. This will load nvm. Otherwise, you can manually reload the bashrc file.

$ source ~/.bashrc

To verify the installation, run the following command.

$ command -v nvm

It’s time to use nvm. First, check out the available versions of Node.js. This will print out a long list.

$ nvm list-remote

To install the desired version, use the following command. In this example, the command will install Node.js v14.9.0.

$ nvm install v14.9.0

Using nvm, it’s possible to install a release based on its aliases. For example, run this command to install the latest LTS version erbium.

$ nvm install lts/erbium

The following command will list all the installed Node.js versions.

$ nvm list

If there are multiple versions installed, nvm allows switching to a different one. First, check the current Node.js version.

$ node -v

Change the default Node.js to a different version.

$ node use <version>

Instead of using the version number, using the version alias also works.

$ node use <version_alias>

Test the change.

$ node -v

The following command will set the default version of Node.js.

$ nvm alias default <version>

Install Node.js from source

As mentioned earlier, Node.js is an open-source project. We can grab the source code and manually build and use Node.js. However, this approach is strongly recommended to follow if you intend to use Node.js for production purposes.

Before jumping into the process, it’s important to note about Python. Node.js supports both Python 2 and Python 3. Node.js will use whichever one is installed. If both Python 2 and Python 3 are installed, the later will be used. If only Python 2 is installed, Python 2 will be used.

First, install the build dependencies. Run the following command. For Python 3 users, the python3-distutils package is necessary.

$ sudo apt update && sudo apt install python python3-distutils g++ make

Now, download the source code. In this example, I’ll be compiling the Node.js v12.18.3 (includes npm 6.14.6). Download Node.js source code.

$ wget https://nodejs.org/dist/v12.18.3/node-v12.18.3.tar.gz

Extract the source code.

$ tar -xvf node-v12.18.3.tar.gz

The time has come to build Node.js. Run the configuration script.

$ ./configure

Start the compilation process. The “-j” is to run make in multithread mode. The “nproc” part is to tell the number of available CPU cores.

$ make -j$(nproc)

Install Node.js.

$ sudo make install

Let’s verify the installation. Check the Node.js and npm version.

$ node -v
$ npm -v

Using Node.js

Node.js comes with a ton of features and functionalities. It’s a runtime for JavaScript. It’s up to you to leverage JavaScript to get the most out of Node. Here, I’ll be showcasing the very basic ways of using Node.js.

First, grab a sample JavaScript. The following code was grabbed from W3Schools.

$ var http = require('http');
$ http.createServer(function (req, res) {
$ res.writeHead(200, {'Content-Type': 'text/html'});
$ res.end('Hello World!');

Run the JavaScript code using Node.js.

$ node demo.js

To get the output, access your computer from port 8080.

Final thought

Node.js is a powerful and popular solution. There are multiple approaches to install it on Ubuntu. Your circumstance will dictate which method suits you the best. While using the default Node.js from Ubuntu repo offers the simplest solution, NodeSource and nvm offers more flexibility.

As for using Node.js, there are tons of materials online that teaches how to take advantage of various Node features in your JavaScript codes. W3Schools is a good place to start your journey.

Happy computing!

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.