Install NodeJS and NPM on Ubuntu 16.04 LTS

Hello. This is my first tutorial of the series and in this tutorial I will show you how to install NodeJS and NPM on Ubuntu 16.04 LTS. I will provide you with basic information about what NodeJS and NPM are as well as some basic examples on how to use them for your next projects.

What is NodeJS ?

First of all, let me copy the description from the official NodeJS website:

Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

Let’s start with Chrome’s V8. It is a an open-source JavaScript engine developed by The Chromium Project for the Google Chrome web browser which compiles and executes JavaScript source code.



It is designed to improve the speed of JavaScript code execution inside the web browser, but due to its efficiency it is also used in NodeJS as an engine. If you are interested about how V8 works under the hood you can read the answer on this question here. Although the answer isn’t official it is one of the most complete I found on the Internet and I hope you will find it helpful.

To better understand what event-driven means let’s talk a little bit about JavaScript. Those who write JavaScript code to build web applications should already been familiar with the event-driven programming paradigm. Generally, there is a main loop that listens on events such as user input (button click, mouse scroll, key down etc.) and as soon as one of those events is detected, it triggers a callback function. An example is the basic JavaScript onclick event that occurs when the user clicks on a button. NodeJS has the same event-driven architecture, but this time to execute server-side JavaScript code.

The next thing you need to understand is the non-blocking I/O model used by NodeJS. When the program accesses a file stored on the disk or makes a random system call, the program execution is interrupted until that process is completed. The model where the processes are waiting for the I/O operation to end is known as blocking I/O model, or synchronous programming. NodeJS is based on a non-blocking I/O model, or asynchronous programming and it doesn’t wait for I/O to complete. Instead it passes a callback function to the event loop which wait for the result returned from the I/O operation.

These features make NodeJS efficient when it comes to resource utilization while keeping the performance on a high-level and there is no wonder why corporations like Cisco, Microsoft, Yahoo!, PayPal, Netflix, LinkedIN, GoDaddy etc. use NodeJS for their projects.

Another thing that makes NodeJS special and very popular among the developers is NPM.

It is a package manager and it is automatically included when NodeJS is installed on the system. Specifically, NPM is an online repository where thousands of JavaScript developers upload their code so someone else can re-use it. NPM also provides a command-line utility which can be used to install specific packages from the online repository as well as to manage package dependencies.


Install NodeJS and NPM on Ubuntu 16.04 LTS

Node.js and NPM are available from the NodeSource binary distributions repository for Ubuntu, Debian, Linux Mint, elementaryOS and the other Debian-based distros. The following Ubuntu versions are supported:

  • Ubuntu 12.04 LTS (Precise Pangolin)
  • Ubuntu 14.04 LTS (Trusty Tahr)
  • Ubuntu 16.04 LTS (Xenial Xerus)

First, install build tools which will allow you to compile and install native addons from npm:

sudo apt-get update
sudo apt-get install -y build-essential

Add the the NodeSource Node.js v7.x repo:

curl -sL | sudo -E bash -

Once the repo is added to your system, install Node.js v7.x and NPM:

sudo apt-get install -y nodejs

There are other ways for installing NodeJS, but I am using this one because it is simple and allows me to update NodeJS easily when a newer version comes out. To print out the current NodeJS version installed on the system use the --version or shortly -v option:

node --version

It will return something like this:

~# nodejs -v

NPM is also installed automatically. Check the NPM version using the -v option:

npm -v


NodeJS Usage and Examples

To demonstrate how you can run scripts with NodeJS I will use the basic “Hello World” example. Create a file app.js and paste the following content:

console.log("Hello World!");

Save the file and run the script:

nodejs app.js

You will see “Hello World!” printed on your console.

~# nodejs app.js
Hello World!

The example of a web server written with NodeJS is another one I like to show you. Remove the content from the app.js file and paste the following one:

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}/`);

Save the file and run it again using the same command:

nodejs app.js

Since I use as hostname the web server will be accessible only on the local machine at or http://localhost:3000. To make it accessible from the outside, you can simply use your domain name or server IP address as a hostname.


I hope you enjoyed this tutorial. If you have any questions or thoughts post them in the comments below. Don’t forget to share it!

Related Posts