How To Install Node.js On CentOS 7

Lately, I have been interested in learning Node.js which is a Javascript platform for server-side programming that allows the developer to write javascript code on the server and since there are many CentOS users struggling to prepare a development environment for this language I thought to make this tutorial.

Before going any further make sure you have CentOS version 7 because it is the only version I have been playing with and there is no absolute guarantee this guide will work on another.

Install Node.js From Source

First we will try to install Node.js from source. I really love installing software from source. So open a new terminal tab on your CentOS 7 machine and run the following command to download the archive file which we need to use for the installation.

wget http://nodejs.org/dist/v0.12.0/node-v0.12.0.tar.gz

As you can see from the above command we make use of the wget utility.

Then extract the tar file as shown below.

tar xvf node-v0.12.0.tar.gz

 Then use the following command to change the working directory to node.

cd node-v*

Now before compiling our code we need to install some packages in our CentOS machine that will help us to do so. So in the opened tab of your terminal enter the followings.

sudo yum install gcc gcc-c++

Wait for these packages to install and run the following commands to configure and compile.

./configure
make

Don’t worry if the above takes some time to complete because the compilation will take awhile. Then use the following command to install Node.js on your system.

sudo make install

 Once the installation is finished you can start using Node.js and to make sure that the correct version is installed you can use the following command in order to check it.

node --version

I get the following when running the above command.

v0.12.0

How To Install Node.js From The EPEL Repository

Another efective but very easy way to install Node.js on your machine is to do it from the official repository. To this make sure you have access to to the EPEL repository, you can do this by running the following command.

sudo yum install epel-release

Now use the yum command to install Node.js

sudo yum install nodejs

And since I want to manage node packages during the development I also need to install the npm package manager for Node.js by using the following command.

sudo yum install npm

Write Our First Node.js Program

Writing a hello world app in Node.js is very easy! Easier than python. All you have to do is type the following piece of code on a file that needs to be saved as something.js.

console.log("Hello World");

I am saving mine as hello.js. Then to run the Node.js application we just created we need to use the following command.

node hello.js

Now open the hello.js with your text editor and use the following piece of code. You can copy and paste it but I highly recommend to type because this is a very good you can get familiar with Node.js.

var http = require('http');

http.createServer(function (request, response) {
 response.writeHead(200, {'Content-Type': 'text/plain'});
 response.end('Hello World\n');
 }).listen(8080);

console.log('Server started');

Use the following command to run the application.

node hello.js

What do you see now? This example is a bit complicated for a beginner but not really hard to understand.What the above code does is that it will display ‘Hello World’ on your browser if you visit http://www.localhost:8000/.

If you want to learn more about Node.js then visit nodejs.org.

Conclusion

Installing Node.js on CentOS operating system is not that hard if you find the right guide and follow the instructions very carefully. I hope this tutorial helps to install it on your machine.