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.

Introduction

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 14.10.1 is the current release.
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -

Latest Stable Release

In this article , We are installing Node.js 12.18.3 latest stable version.
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
Sample Output:
## Installing the NodeSource Node.js 12.x repo...

## Populating apt-get cache...

+ apt-get update
Ign:1 https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 InRelease
Hit:2 https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 Release
Hit:3 https://deb.nodesource.com/node_12.x bionic InRelease
Hit:5 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic InRelease
Get:6 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:7 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:8 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Fetched 252 kB in 1s (221 kB/s)
Reading package lists... Done

## Confirming "bionic" is supported...

+ curl -sLf -o /dev/null 'https://deb.nodesource.com/node_12.x/dists/bionic/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 12.x repo...

+ echo 'deb https://deb.nodesource.com/node_12.x bionic main' > /etc/apt

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

/sources.list.d/nodesource.list

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

+ apt-get update
Ign:1 https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 InRelease
Hit:2 https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 Release
Hit:4 https://deb.nodesource.com/node_12.x bionic InRelease
Hit:5 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic InRelease
Hit:6 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:7 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:8 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic-backports InRelease
Reading package lists... Done

## Run `sudo apt-get install -y nodejs` to install Node.js 12.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
Sample Output:
The following NEW packages will be installed:
nodejs
0 upgraded, 1 newly installed, 0 to remove and 55 not upgraded.
Need to get 18.2 MB of archives.
After this operation, 92.3 MB of additional disk space will be used.
Get:1 https://deb.nodesource.com/node_12.x bionic/main amd64 nodejs amd64 

12.18.3-1nodesource1 [18.2 MB]
Fetched 18.2 MB in 1s (31.2 MB/s)
Selecting previously unselected package nodejs.
(Reading database ... 230517 files and directories currently installed.)
Preparing to unpack .../nodejs_12.18.3-1nodesource1_amd64.deb ...
Unpacking nodejs (12.18.3-1nodesource1) ...
Setting up nodejs (12.18.3-1nodesource1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
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
v12.18.3
To Check NPM version.
npm --version
Output:
$ npm --version
6.14.6

Step #3: Testing Node.js Server

Create a javascript file with name nodeapp.js, add below code which prints ” Hello World”.
sudo nano nodeapp.js
Paste the below lines it it
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
We have covered How to Install Latest Node.js and NPM on Ubuntu 19.04,18.04/16.04 LTS.

Installing Node.js and NPM using NVM

Download the NVM installation script from NVM GitHUB Page using curl.
curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.35.3/install.sh -o 
install_nvm.sh
once downloaded, run the bash script
bash install_nvm.sh
It will install your system’s home directory ~/.nvm  and adds configuration in ~/.profile Load the profile
source ~/.profile
To check all available node.js versions
nvm ls-remote
Sample output:
.....
v12.14.0 (LTS: Erbium)
v12.14.1 (LTS: Erbium)
v12.15.0 (LTS: Erbium)
v12.16.0 (LTS: Erbium)
v12.16.1 (LTS: Erbium)
v12.16.2 (LTS: Erbium)
v12.16.3 (LTS: Erbium)
v12.17.0 (LTS: Erbium)
v12.18.0 (LTS: Erbium)
v12.18.1 (LTS: Erbium)
v12.18.2 (LTS: Erbium)
v12.18.3 (Latest LTS: Erbium)
v13.0.0
v13.0.1
v13.1.0
v13.2.0
v13.3.0
v13.4.0
v13.5.0
v13.6.0
v13.7.0
v13.8.0
v13.9.0
v13.10.0
v13.10.1
v13.11.0
v13.12.0
v13.13.0
v13.14.0
v14.0.0
v14.1.0
v14.2.0
v14.3.0
v14.4.0
v14.5.0
v14.6.0
v14.7.0
v14.8.0
v14.9.0
v14.10.0
v14.10.1
Error: npm update check failed try running with sudo or get access change the permission to install node.js
sudo chown -R $USER:$(id -gn $USER) /home/ubuntu/.config
Now you can install node.js version as you want.
nvm install 12.18.3
To set recently node.js version
nvm use 12.18.3
chaeck the node varsion
node -v
check the nvm version
nvm --version

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 (12.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 Node.js and NPM on Ubuntu 20.04 LTS How to Install Angular CLI on Ubuntu 18.04/16.04 LTS How to Install Node.js on mac OS How to Install Node.js on Windows 10
FOSS TechNix

FOSS TechNix (Free,Open Source Software's and Technology Nix*) founded in 2019 is a community platform where you can find How-to Guides, articles for DevOps Tools,Linux and Databases.

Leave a Comment

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

Share via
Copy link
Powered by Social Snap