How to Install Node.js on Ubuntu 20.04 Server (Tutorial)

This article will show us how to Install Node.js on Ubuntu 20.04.

Node.js is a free and open source JavaScript runtime platform used for traditional websites and back end API services. It is built on Chrome’s V8 JavaScript engine and designed for non blocking, event driven servers. It is famous to build, back end, front end, full stack and networking applications. NPM is a default package manager for Node.js server applications and has the world’s largest software registry. It helps you to create a complete web app development project with the help of a single programming language.

Nodejs is gaining popularity due to its ease of development process and delivers efficient performance. Generally, Node.js is used in the I/O bound Applications, Data Streaming Applications, Data Intensive Real time Applications, JSON APIs based Applications and Single Page Applications.

Follow this post in how to install Node.js on Ubuntu 20.04.

Install Node.js via Ubuntu Repository

The simple and easiest way to install Node.js is to install it from the Ubuntu repository. However, the latest Node.js version is not available in the Ubuntu repository. The version included in the Ubuntu repository is 10.19.0.

You can check the information about the Node.js package with the following command:

				
					apt show nodejs
				
			

You will get the package information in the following output:

				
					Package: nodejs
Version: 10.19.0~dfsg-3ubuntu1
Priority: extra
Section: universe/web
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian Javascript Maintainers <pkg-javascript-devel@lists.alioth.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 158 kB
Depends: libc6 (>= 2.4), libnode64 (= 10.19.0~dfsg-3ubuntu1)
Recommends: ca-certificates, nodejs-doc
Suggests: npm
Conflicts: nodejs-legacy
Replaces: nodejs-legacy
Homepage: http://nodejs.org/
Download-Size: 61.1 kB

				
			

Next, update all the package index of the Ubuntu repository by running the following command:

				
					apt-get update -y
				
			

Once your repository is updated, install the Node.js and NPM package using the following command:

				
					apt-get install nodejs npm -y
				
			

Once the Node.js is installed, verify the Node.js version with the following command:

				
					node --version
				
			

You will get the following output:

				
					v10.19.0
				
			

If you want to remove the Node.js package, run the following commands:

				
					apt-get remove nodejs npm --purge
apt-get autoremove
apt-get clean

				
			

Once you are finished, you can move to the next step.

Install Node.js Using NodeSource Repository

In the next section of how to install Node.js on Ubuntu 20.04 is to install the NodeSource. NodeSource focuses on providing enterprise grade Node support and they maintain a repository containing the latest versions of Node.js. It allows you to install multiple Node.js versions in a single system. First, install the curl command line utility using the following command:

				
					apt-get install curl -y
				
			

Next, add the NodeSource repository for Node.js version 16, run the following command:

				
					curl -sL https://deb.nodesource.com/setup_16.x | bash -
				
			

Once the NodeSource repository is added, install the Node.js package with the following command:

				
					apt-get install nodejs -y
				
			

After the installation, verify the Node.js version with the following command:

				
					node --version
				
			

You will get the following output:

				
					v16.14.0
				
			

Install Node.js Using NVM

NVM stands for “Node Version Manager” is a Node package manager that helps you manage and switch between different Node versions with ease. It provides a command line interface to install different Node.js versions using a single command.

First, install the NVM by running the following script:

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

You will get the following output:

				
					% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 14926 100 14926 0 0 27951 0 --:--:-- --:--:-- --:--:-- 27951
=> Downloading nvm from git to 'https://net.cloudinfrastructureservices.co.uk/home/vyom/.nvm'
=> Cloning into 'https://net.cloudinfrastructureservices.co.uk/home/vyom/.nvm'...
remote: Enumerating objects: 354, done.
remote: Counting objects: 100% (354/354), done.
remote: Compressing objects: 100% (302/302), done.
remote: Total 354 (delta 40), reused 157 (delta 27), pack-reused 0
Receiving objects: 100% (354/354), 207.03 KiB | 999.00 KiB/s, done.
Resolving deltas: 100% (40/40), done.
* (HEAD detached at FETCH_HEAD)
master
=> Compressing and cleaning up git repository

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

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

				
			

Once the NVM is installed, close and reopen your terminal to start using nvm or run the following to use it now:

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

				
			

Next, verify the NVM version using the following command:

				
					nvm --version
				
			

You will get the following output:

				
					0.38.0
				
			

If you want to list all Node.js versions, run the following command:

				
					nvm list-remote
				
			

You will get a list of all Node.js versions in the following output:

				
					v16.9.1
v16.10.0
v16.11.0
v16.11.1
v16.12.0
v16.13.0 (LTS: Gallium)
v16.13.1 (LTS: Gallium)
v16.13.2 (LTS: Gallium)
v16.14.0 (Latest LTS: Gallium)
v17.0.0
v17.0.1
v17.1.0
v17.2.0
v17.3.0
v17.3.1
v17.4.0
v17.5.0

				
			

Now, install the Node.js version 17.0.0 using the following command:

				
					nvm install v17.0.0
				
			

You will get the following output:

				
					Downloading and installing node v17.0.0...
Downloading https://nodejs.org/dist/v17.0.0/node-v17.0.0-linux-x64.tar.xz...
####################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v17.0.0 (npm v8.1.0)
Creating default alias: default -> v17.0.0

				
			

Now, verify the Node.js version with the following output:

				
					node --version
				
			

You will get the following output:

				
					v17.0.0
				
			

To list all installed Node.js versions, run the following command:

				
					nvm ls
				
			

You will get the following output:

				
					-> v17.0.0
system
default -> v17.0.0
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v17.0.0) (default)
stable -> 17.0 (-> v17.0.0) (default)
lts/* -> lts/gallium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.10 (-> N/A)
lts/fermium -> v14.19.0 (-> N/A)
lts/gallium -> v16.14.0 (-> N/A)

				
			

To install the latest stable version, run the following command:

				
					nvm install --lts
				
			

You should see the following output:

				
					Installing latest LTS version.
Downloading and installing node v16.14.0...
Downloading https://nodejs.org/dist/v16.14.0/node-v16.14.0-linux-x64.tar.xz...
####################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v16.14.0 (npm v8.3.1)

				
			

To set the default Node.js version to LTS, run the following command:

				
					nvm use --lts
				
			

You should see the following output:

				
					Now using node v16.14.0 (npm v8.3.1)
				
			

To install the Node.js stable version, run the following command:

				
					nvm install stable
				
			

You will get the following output:

				
					Downloading and installing node v17.5.0...
Downloading https://nodejs.org/dist/v17.5.0/node-v17.5.0-linux-x64.tar.xz...
####################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v17.5.0 (npm v8.4.1)

				
			

If you want to remove Node.js version 17.0.0, run the following command:

				
					nvm uninstall v17.0.0
				
			

Once you are finished, you can move to the next step.

Create a Node.js Application

Next section of how to install Node.js on Ubuntu , we will show you how to create and run a simple Node.js application.  First, create a file named hello.js using the following command:

				
					nano hello.js
				
			

Add the following code:

				
					#!/usr/bin/env node
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');

				
			

Next, set the executable permission to the hello.js file using the following command:

				
					chmod +x ./hello.js
				
			

Next, run the Node.js application using the following command:

				
					./hello.js &
				
			

You will get the following output:

				
					Server running at http://localhost:8080/
				
			

Now, open another terminal and access your Node.js application using the following command:

				
					curl http://localhost:8080
				
			

You will get the following output:

				
					Hello World
				
			

How to install Node.js on Ubuntu 20.04 Conclusion

In the above post, we explained how to install Node.js using three different ways. You can now choose your preferred method to install Node.js on your server. For more information, visit the Node.js documentation page.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

5 1 vote
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x