Install and use Node.js on Ubuntu 20.04

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

This guide will show you how to install and use Node.js on Ubuntu 20.04.

Node.js on Ubuntu

Node.js is very popular for scaling backend functionalities. In the case of Ubuntu, there are several sources to grab Node.js. Different methods install different versions of Node.js. You can also manually choose which one to choose.

Use the method that best suits your needs.

Install Node.js from Ubuntu repositories

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

Installation is super easy. Update the APT cache and install Node.js along with npm (Node Package Manager).

$ sudo apt update && sudo suitable To install nodejs above sea level -and

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

$ nodejs execution

Install Node.js from NodeSource PPA

NodeSource is a dedicated PPA that offers multiple versions of Node.js. I recommend this method to others as it gives you more control. For advanced users, you can also specify the exact version of Node.js to install. At the time of this writing, NodeSource PPA is hosting Node.js v10, v12, v13, and v14.

Here I show how to configure NodeSource PPA for Node.js v14. If you’d like to install a different version of Node.js, see the NodeSource Readme for proper instruction.

First, make sure curl is installed on your system.

$ sudo apt update && sudo suitable To install Beckon -and

Now run the NodeSource installation script.

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

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

$ sudo suitable To install nodejs -and

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

$ node -v

Install Node.js with nvm

It’s an interesting way to install Node.js. The nvm (Node Version Manager) is a tool with which several versions of Node.js together with the associated node packages can be installed and managed independently of one another. Check out nvm on GitHub.

Run one of the following commands to install nvm. Each of them will download and run the nvm installer script.

$ Beckon -Ö- 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 the terminal and open it again. This will load nvm. Otherwise you can reload the bashrc file manually.

$ source ~/.bashrc

Run the following command to verify the installation.

$ command -v nvm

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

$ nvm list-remote

To install the version you want, use the command below. In this example, the command installs Node.js v14.9.0.

$ nvm To install v14.9.0

With nvm it is possible to install a version based on its aliases. For example, run this command to install the latest version of LTS Erbium.

$ nvm To install lts/Erbium

The following command lists all installed Node.js versions.

$ nvm list

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

$ node -v

Change the standard Node.js to a different version.

$ Node usage <execution>

The version alias can also be used instead of the version number.

$ Node usage <version_alias>

Test the change.

$ node -v

The following command sets the default version of Node.js.

$ nvm alias Originally <execution>

Install Node.js from the source

As mentioned earlier, Node.js is an open source project. We can get the source code and build and use Node.js manually. However, this approach is highly recommended if you plan to use Node.js for production purposes.

Before getting into the process, it’s important to note Python. Node.js supports both Python 2 and Python 3. Node.js uses the installed. If both Python 2 and Python 3 are installed, the latter is used. If only Python 2 is installed, then Python 2 is used.

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

$ sudo apt update && sudo suitable To install python python3-distutils g ++ do

Now download the source code. In this example I am compiling Node.js v12.18.3 (contains npm 6.14.6). Download the 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

It’s time to build Node.js. Run the configuration script.

$ ./configure

Start the compilation process. The “-j” should execute make in multithreaded mode. The “nproc” part indicates the number of available CPU cores.

$ do -j $(nproc)

Install Node.js.

$ sudo do To install

Let’s check the installation. Check the Node.js and npm versions.

$ Node -v
$ npm -v

Using Node.js

Node.js offers a lot of features and functionalities. It is a runtime for JavaScript. It’s up to you to leverage JavaScript to get the most out of Node. Here I show the very basic ways to use Node.js.

First, get a sample JavaScript. The following code was taken from W3 schools.

$ var http = require(‘http’);
$ http.createServer(function (req, res) {
$ res.Print head(200, {‘Content Type’: ‘text / html’});
$ res.end(‘Hello World!’);
}).hear(8080);

Run the JavaScript code using Node.js.

$ Node demo.js

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

Last thought

Node.js is a powerful and popular solution. There are several approaches to installing it on Ubuntu. Your situation will determine which method is best for you. While using the standard Node.js from the Ubuntu repo offers the simplest solution, NodeSource and nvm offer more flexibility.

As for how to use Node.js, there are tons of resources online to show you how to use the various Node functions in your JavaScript code. W3 schools is a good place to start your journey.

Have fun calculating!

Related Posts