How To Install Node.js on Ubuntu 20.04

THIS DOCUMENT IS ALSO AVAILABLE FOR

Node.js is an open-source, cross-platform JavaScript runtime environment mainly used for developing web applications and network tools.

It is built on Google’s V8 JavaScript engine, developed Node.js by Ryan Dahl in 2009.

Here, we will see how to install Node.js on Ubuntu 20.04.

There are two versions of Node.js available for the users.

  • v12.x (Long Term Supported)
  • v14.x (Latest Version)

Install Node.js On Ubuntu 20.04

Node.js can be installed from three ways:

  1. Using NodeSource Repository
  2. Using NVM (Recommended)
  3. Using Ubuntu Repository (Not Recommended)

1. Install Node.js using NodeSource

Add NodeSource Repository

NodeSource maintains Node.js packages for the Ubuntu operating system. So, set up the NodeSource repository on your system with the below commands.

Node.js 12.x (LTS)
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
Node.js 14.x (Current Version)
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -

Install Node.js

Once after adding the NodeSource repository on your system, install the Node.js using the apt command. NPM (Node Package Manager), a package manager for the Javascript programming language will also be installed along with Node.js.

sudo apt install -y nodejs

Check the version of Node.js and NPM using the following command.

node -v

Output:

v12.16.3

NPM version.

npm -v

Output:

6.14.4

2. Install Node.js using NVM

nvm (Node Version Manager) is a version manager for Node.js. It helps us to manage the installation Node.js and switch between different node versions.

Install NVM

Use the bash script to install nvm on your system.

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

Close and reopen your terminal to start using nvm or run the following commands to load the nvm to your current session.

export NVM_DIR="$HOME/.nvm" && [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" && [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

Install Node.js

Node.js 12.x (LTS)
nvm install --lts
Node.js 14.x (Current Version)
nvm install node

Check the version of Node.js and NPM using the following command.

node -v

Output:

v12.16.3

NPM version.

npm -v

Output:

6.14.4

NVM Commands

List the available Node.js version for installation. The command would take a minute or two to get the available Node.js versions.

nvm ls-remote

Use the below command to install a specific version of Node.js.

nvm install 12.16.2

List all Node.js versions installed on your system with the below command.

nvm ls

Output:

->     v12.16.3
default -> lts/* (-> v12.16.3)
node -> stable (-> v12.16.3) (default)
stable -> 12.16 (-> v12.16.3) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/erbium (-> v12.16.3)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.20.1 (-> N/A)
lts/erbium -> v12.16.3

You can switch between Node.js versions any time with the below command.

nvm use 14.2.0

You can remove the Node.js version using the below command.

nvm uninstall 12.16.2

3. Install Node.js using Ubuntu Repository

Installing Node.js from the Ubuntu repository is a straight forward one. Use the apt commands to install it.

This method of Node.js installation is not recommended as the version available in the Ubuntu repository is far old. However, v10.x is supported till April 30, 2021.
sudo apt update

sudo apt install -y nodejs

Check the version of Node.js using the following command.

node -v

Output:

v10.19.0

Install Yarm Package Manager (Optional)

To install the Yarn package manager, run:

curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -

echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

sudo apt update

sudo apt install -y yarn

Install Build Tools (Optional)

Install build tools if you plan to compile and install native addons from npm.

sudo apt install -y build-essential curl

Test Node.js Installation

To test the Node.js installation, we will create a web server and run it with Node.js.

Let’s create a file called web_server.js.

nano web_server.js

Place the below content into the web_server.js file.

const http = require('http');
const port = 8080;
const server = http.createServer((req, res) => {
   res.writeHead(200, {'Content-Type': 'text/plain'});
   res.end('Hello World\n');
});
server.listen(port, () => {
  console.log(`Node.js server listening on port ${port}`);
});

Now, start the webserver with Node.js.

node --inspect web_server.js

You should get the following message on your terminal.

Debugger listening on ws://127.0.0.1:9229/4d0e6aba-d7cb-46c1-997c-e2b105ec40e7
For help, see: https://nodejs.org/en/docs/inspector
Node.js server listening on port 8080

The above message confirms that the webserver has been up and listening on port 8080.

Open a browser and go to the below address.

http://your-ip-add-ress:8080

You should get the below web page.

Web Page Served by Node.js
Web Page Served by Node.js

Conclusion

That’s All. You have successfully installed Node.js on Ubuntu 20.04.

Prev Post
Next Post
comments powered by Disqus