How to Install Node.js and NPM on Ubuntu 16.04 | 18.04 via APT and SNAP

The steps below show you how to install the latest version of Node.js and NPM packages on Ubuntu 16.04 and 18.04 using its repository via PPA and Snap package management…

Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine…..  Chrome V8 engine is a Google’s open source high-performance JavaScript engine written in C++ and used in Google Chrome, the open source projects….

Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient…. Although Node.js comes in Ubuntu default repositories, if you want to get the latest version, you’ll have to add its official PPA…

For more about Node.js, please visit the project’s homepage

There are multiple ways to install Node.js on Ubuntu… below are two methods that show you how to install it via its PPA repository as well as using Snap package management… You can choose either method to install Node.js. You probably don’t want to use both methods on the same machine…

Method #1: Install Node.js via PPA

Before installing the latest version of Node.js, you must add its PPA to Ubuntu… This repository is provided by the official package maintainer… To continue, run the commands below to install required dependencies….

sudo apt install build-essential curl

There are two repositories you can install… one repository contains the latest Node.js packages and the other has the LTS or (Long Term Support) packages…. if you need the latest and greatest, then install the first repository…

On the other hand, if you need a more stable and tested Node.js packages, then install the LTS repository….

Then for the Latest release (version 11), add this PPA..

curl -sL | bash -

To install the LTS release (version 10), use this PPA

curl -sL | bash -

After that, you can now install latest version of Node.js from the particular repository you choose… If you add both repositories, the latest version of Node.js will be installed and not the LTS…

Install Node.js and NPM

To install, run the commands below

sudo apt install nodejs

After installing, both Node.js and NPM modules should be installed and ready to use….

You can use the commands below to view the version number installed….

node -v
npm -v

Method #2: Install Node.js via Snap

Another way to install Node.js is via Snap package management… this might be the easiest way to do it…

Snaps are containerised software packages that are simple to create and install…. They are applications packaged with all their dependencies to run on all popular Linux distributions from a single build which allow them to update automatically and roll back gracefully….

To install via Snap, run the commands below install Snap…

sudo apt update
sudo apt install snapd

Then run the commands below to get Node.js installed via Snap..

For the Latest release (version 11),

sudo snap install node --channel=11/stable --classic

For the LTS (version 10)

sudo snap install node --channel=10/stable --classic

To test whether the web server is properly installed, run the commands below to create a test file called http_server.js in your home folder…

cd ~/
nano http_server.js

Then copy and paste the content below into the file and save….

const http = require('http');

const hostname = '';
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}/`);

After that save the file and run the commands below to start the server….

node http_server.js

You should see an output that reads:

Server running at

Now open your browser and browse to the server hostname or IP address followed by port 3000… and you should see a default page with Hello World

Node.js ubuntu install


You may also like the post below:

1 Comment

  1. Great article! you have used this method for Ubuntu 16.04 LTS and Ubuntu 18.04 but what about Ubuntu 18.04.2 LTS can we use the same method for this version too?

Leave a Reply

Your email address will not be published. Required fields are marked *

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