Cyberithub

Install Node.js in 6 Easy Steps on Ubuntu 18.04

In this tutorial, I will take you through the steps to Install Node.js on Ubuntu 18.04. Node.js is ideal for building highly scalable, data intensive and real time backend services that power our client applications. Node provides a runtime environment for executing Javascript code which basically means it contains a Javascript engine which can execute a Javascript code. It is using Chrome V8 Engine in its core.

Why do we need Node.js ?

As you know, JS Engine lies in the core of the Web browser which takes JS Code and converts it into a language that computer understands. Every browser has its own JS Engine due to which same JS code behaves differently in different browser. This became a headache for the JS Developers. In 2009, Ryan Dahl thought to run JS outside of browser instead of using it from inside. So he took the fastest JS Chrome v8 engine and embedded it inside a C++ Program and called it Node Program. As a result, Node.js became the common runtime environment for JS code from outside the browser.

What are the other few Javascript Engines used by the Browser ?

  • Internet Explorer uses Chakra JS Engine
  • Firefox used SpiderMonkey JS Engine
  • Google Chrome used v8 JS Engine

How Node.js is different from other Programming Languages ?

Node.js is very easy to work on creating backend services as compared to others programming languages such as ASP.NET, Rails, Django etc. Below are the few more features of Node.js:-

  • It is great for prototyping and agile development.
  • It can also be used for building superfast and highly scalable services
  • It is used in productions by Large Organizations like Paypal, Netflix, Uber, Walmart etc.
  • Node app is usually being written with 33% fewer lines of code as compared to Java.
  • Node backend services has the capacity to serving double the number of requests/sec while decreasing the average response time by 35%.

Install Node.js in 6 Easy Steps on Ubuntu 18.04 1

Install Node.js on Ubuntu

Also Read: How to Install NVM for Node.js on Ubuntu 18.04

Step 1: Prerequisites

a)You need to have a running Ubuntu 18.04 System.

b)You need to login as root or with user having sudo access to run all the commands. You can check How to add User into Sudoers to provide sudo access to Users.

Step 2: Update your System

Before installing Node.js in your system, you need to fully update your system with latest updates using sudo apt-get update command.

test@localhost:~$ sudo apt-get update
Hit:1 http://in.archive.ubuntu.com/ubuntu bionic InRelease
Get:2 http://in.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Hit:3 https://artifacts.elastic.co/packages/7.x/apt stable InRelease
Get:4 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:5 http://in.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:6 http://in.archive.ubuntu.com/ubuntu bionic-updates/main amd64 DEP-11 Metadata [295 kB]
Get:7 http://in.archive.ubuntu.com/ubuntu bionic-updates/main DEP-11 48x48 Icons [73.8 kB]
Get:8 http://in.archive.ubuntu.com/ubuntu bionic-updates/main DEP-11 64x64 Icons [143 kB]
Get:9 http://in.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 DEP-11 Metadata [264 kB]
Get:10 http://in.archive.ubuntu.com/ubuntu bionic-updates/universe DEP-11 48x48 Icons [206 kB]
Get:11 http://in.archive.ubuntu.com/ubuntu bionic-updates/universe DEP-11 64x64 Icons [432 kB]
Get:12 http://in.archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 DEP-11 Metadata [2,464 B]
Get:13 http://in.archive.ubuntu.com/ubuntu bionic-backports/universe amd64 DEP-11 Metadata [7,976 B]
Fetched 1,676 kB in 1s (1,230 kB/s)
Reading package lists... Done

Step 3: Download and Install Node.js

Once System is updated, you need to download and install node.js by running sudo apt-get install nodejs command.

test@localhost:~$ sudo apt-get install nodejs
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
linux-headers-5.0.0-23 linux-headers-5.0.0-23-generic linux-image-5.0.0-23-generic linux-modules-5.0.0-23-generic linux-modules-extra-5.0.0-23-generic
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
libc-ares2 libhttp-parser2.7.1 libuv1 nodejs-doc
The following NEW packages will be installed:
libc-ares2 libhttp-parser2.7.1 libuv1 nodejs nodejs-doc
0 upgraded, 5 newly installed, 0 to remove and 126 not upgraded.
Need to get 5,670 kB of archives.
After this operation, 24.8 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

..............................................................................................................................................................

Step 4: Check Node.js and NPM Version

You can check node.js version by running node -v command.

test@localhost:~$ node -v
v13.6.0

If you want to check npm version, you can do that by running npm -v command.

test@localhost:~$ npm -v
6.13.6

Step 5: Run Your First Node.js Program

You can use below code to test your node.js functionality where we are creating a http Server which listens on port 8080. It sends hello world text when you try accessing server on port 8080.

test@localhost:~$ vi example.js
var http = require('http');

http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}).listen(8080);

Run above code in a terminal.

test@localhost:~$ node example.js

Check the response in another terminal using curl tool.

test@localhost:~$ sudo curl http://localhost:8080
Hello World!

You can also check the response in your web browser.

Install Node.js in 6 Easy Steps on Ubuntu 18.04 2

 

Step 6: Update Node.js on Ubuntu

If you want to update Node.js to the latest version, you either need to go to Node.js Official Website and download the latest one or use apt-get update nodejs to update from Repo.

test@localhost:~$wget https://nodejs.org/dist/vxxx/node-xxx.tar-gz

Upgrade using apt-get.

test@localhost:~$ sudo apt-get upgrade nodejs

 

Also Read: Node.js Example

Leave a Comment