How to Install Latest Node.js and NPM on Ubuntu 19.04,18.04/16.04 LTS

In this article, We are going to perform How to Install Latest Node.js and NPM on Ubuntu 19.04,18.04/16.04 LTS and any other cloud platform like Azure, EC2, Compute Engine.

Introdution

Node.js is free and an open-source cross-platform JavaScript run-time environment that allows server-side execution of JavaScript code. It is used in developing web and networking applications.

NPM(Node Package Manager) is command line tool for Node.js packages that installs, updates and uninstall packages in your projects.We don’t have install npm separately it is includes with Node.js installation. 

Prerequisites

  • Ubuntu 19.04, 18.04/16.04 LTS
  • SSH access with sudo privileges
  • Firewall Port: 3000

Before installing,Lets update and upgrade System Packages

$ sudo apt-get update
$ sudo apt-get upgrade

Step 1: Add Node.js and npm from NodeSource Repository

Before installing, We have to install curl and add Node.js from Nodesource repository.

$ sudo apt-get install curl

Current Release

Node.js 12 is the current release.

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

Latest Release

In this article , We are installing Node.js 10.16.3 latest version.

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

Output:

+ curl -sLf -o /dev/null 'https://deb.nodesource.com/node_10.x/dists/xenial/Release'

## Adding the NodeSource signing key to your keyring...

+ curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add -
OK

## Creating apt sources list file for the NodeSource Node.js 10.x repo...

+ echo 'deb https://deb.nodesource.com/node_10.x xenial main' > /etc/apt/sources.list.d/nodesource.list
+ echo 'deb-src https://deb.nodesource.com/node_10.x xenial main' >> /etc/apt/sources.list.d/nodesource.list

## Running `apt-get update` for you...


## Run `sudo apt-get install -y nodejs` to install Node.js 10.x and npm
## You may also need development tools to build native addons:
sudo apt-get install gcc g++ make
## 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-get update && sudo apt-get install yarn

Step 2: Install Latest Node.js and NPM on Ubuntu

Once repository added in ubuntu,enter below command to install Node.js and NPM.

$ sudo apt-get install -y nodejs

Output:

$ sudo apt-get install nodejs
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
nodejs
0 upgraded, 1 newly installed, 0 to remove and 84 not upgraded.
Need to get 15.8 MB of archives.
After this operation, 76.5 MB of additional disk space will be used.
Get:1 https://deb.nodesource.com/node_10.x xenial/main amd64 nodejs amd64 10.16.3-1nodesource1 [15.8 MB]
Fetched 15.8 MB in 0s (38.8 MB/s)
Selecting previously unselected package nodejs.
(Reading database ... 139352 files and directories currently installed.)
Preparing to unpack .../nodejs_10.16.3-1nodesource1_amd64.deb ...
Unpacking nodejs (10.16.3-1nodesource1) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up nodejs (10.16.3-1nodesource1) ...

Step 3: Check Node.js and NPM Version on Ubuntu

After installation, check Node.js version by entering below command,

$ node --version

Output:

$ node --version
v10.16.3

To Check NPM version.

$ npm --version

Output:

$ npm --version
6.9.0

Step 4: Testing Node.js Server

Create a javascript file with name nodeapp.js, add below code which prints ” Hello World”.
const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Run your Node.js server with the below command,

$ node nodeapp.js

Output:

$ node nodeapp.js

Server running at http://127.0.0.1:3000/

Now open your browser and type server name or IP followed by port 3000, you will see Hello World message.

http://127.0.0.1:3000

Uninstall Node.js and NPM in Ubuntu

If we want to uninstall/remove node.js and npm from ubuntu, Enter Y to proceed uninstallation.

$ sudo apt remove nodejs npm

Output:

$ sudo apt remove nodejs npm
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package 'npm' is not installed, so not removed
The following packages will be REMOVED:
nodejs
0 upgraded, 0 newly installed, 1 to remove and 84 not upgraded.
After this operation, 76.5 MB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 143850 files and directories currently installed.)
Removing nodejs (10.16.3-1nodesource1) ...
Processing triggers for man-db (2.7.5-1) ...

Conclusion:

In this article, We have performed ,How to Install Latest Node.js and NPM on Ubuntu 19.04,18.04/16.04 LTS, Checked Node.js and NPM version and performed to uninstall node.js and NPM from ubuntu.

Related Articles:

How to Install MongoDB on Ubuntu 18.04/16.04 LTS

Shivdas Kanade

I am Shivdas Kanade working as DevOps Engineer. Believe in Sharing Knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Shares
Share via
Copy link