How to Install Node.js 10 / 11 / 12 on Ubuntu 16.04 / 18.04 via APT and SNAP

Node.js, a full-stack solution which is primarily used to build back-end server apps is a JavaScript runtime built on Chrome’s V8 JavaScript engine….. Chrome V8 engine on the other hand is a Google’s open source high-performance JavaScript engine written in C++ and used in Google Chrome, the open source projects….

If you’re going to be developing apps based on Node.js, then the steps below show you how to install version 10 (LTS), 11 and 12 (Current) on Ubuntu 16.04 / 18.04 LTS…

Updates to Node.js packages gets released very frequently… There are two primary versions: Current and the Long Term Support (LTS) version…

Unlike the current versions which get released about once a year, Node.js LTS versions get released about every 3 years..

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 update
sudo apt install build-essential apt-transport-https lsb-release ca-certificates 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 12), add this PPA..

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

For (version 11), run the commands below:

curl -sL https://rpm.nodesource.com/setup_11.x | bash -

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

curl -sL https://rpm.nodesource.com/setup_10.x | 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 containerized 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 12),

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

For version 11, run this:

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 = '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 Worldn');
});

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 http://127.0.0.1:3000/

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

http://localhost:3000

Node.js ubuntu install

Enjoy!

You may also like the post below:

How to Install Joomla with Apache2 and Cloudflare CDN / SSL on Ubuntu 16.04 / 18.04

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.