How to install NodeJS on Linux (CentOS/Ubuntu)

Node.js or as commonly called NodeJS, is an open source , cross platform Java Run time environment based Chrome’s V8 JavaScript engine, meant for server-side programming. It allows us to run scripts on server-side to produce dynamic web page content before the page is sent to the user’s web browser. It allows to easily develop networked applications that require back-end functionality with Java-scripts being used on both front-end & back-end.

Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. In this tutorial, we will learn to install Node.JS on Ubuntu & CentOS machines.

Pre-requisites

We need to have some packages like ‘gcc, gcc-c++’ installed on our systems. Install them on CentOS using the following command,

$ sudo yum install gcc gcc-c++

For Ubuntu, install them using the following command,

$ sudo apt-get install build-essential

Install NodeJS on Ubuntu

We can install either of the two available Node.JS versions i.e. 6 (which has long term support) or 9 (which is the latest version available). For installing Node.JS, we will add the official repositories on our system, use the following command to add the repository,

For Node.JS 6,

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

For Node.JS 9,

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

Once added, install Node.JS using the following command,

$ sudo apt-get install nodejs

We can also install Node.JS using the source code, method has been discussed below.

Install NodeJS on CentOS

On CentOS, we can either install Node.JS from EPEL repository or from official Node.JS repositories. Both methods have been shared below.

Using EPEL repository

To use this method for Node.JS installation, we need to add the EPEL repositories on our system first. Execute one of the following mentioned command, based on the OS & architecture being used,