Have a Question?

If you have any question you can ask below or enter what you are looking for!

How to install Node.js on CentOS 6

Introduction

Node.js is a popular open source platform for easily building fast scalable server side network applications. It is built on Google Chrome’s V8 JavaScript engine and applications are written in JavaScript. Its event-driven and non-blocking I/O model makes it lightweight, efficient and good for serving high volume of requests.

In this article, we will show you four different methods to install Node.js on your CentOS 6 Linux server.

Requirements

Commands described here require root user access to the CentOS 6 server.

Installation methods

Following are the different installation methods that we use:

  1. Install Node.js from source package
  2. Install Node.js from binary package
  3. Installing Node.js from binary RPM
  4. Install Node.js using NVM

Install Node.js from source package

To compile newer versions of Node.js, GCC 4.8 or higher is required. CentOS 6 has GCC version 4.4. So let us use devtoolset-3 Software Collection which provides GCC version 4.9.2 at this time. devtoolset-3 depends on another Software Collection rh-java-common and we install it as well.

At first, install rh-java-common Software Collection:

rpm -ivh https://www.softwarecollections.org/en/scls/rhscl/rh-java-common/epel-6-x86_64/download/rhscl-rh-java-common-epel-6-x86_64.noarch.rpm
yum -y install rh-java-common-runtime

Next, install devtoolset-3 Software Collection:

rpm -ivh https://www.softwarecollections.org/en/scls/rhscl/devtoolset-3/epel-6-x86_64/download/rhscl-devtoolset-3-epel-6-x86_64.noarch.rpm
yum -y install devtoolset-3

Download Node.js source package, uncompress it and change current directory to source directory:

wget https://nodejs.org/dist/v5.3.0/node-v5.3.0.tar.gz
tar xzf node-v5.3.0.tar.gz
cd node-v5.3.0

Use devtoolset-3 Software Collection:

scl enable devtoolset-3 bash

Configure Node.js source, build and install it:

./configure
make -j4
make install

NOTE: the -j switch of make build tool specifies the number of jobs to run simultaneously so as to speed up build process. Usually the selected number is equal to the number of CPU cores available in the server.

Quit devtoolset-3 environment by pressing Ctrl+D or entering exit command.

Nodejs is installed in /usr/local directory. Verify file system path of node and npm (Node Package Manager) programs with which command:

which node
/usr/local/bin/node
which npm
/usr/local/bin/npm

Using –version switch, verify version of node and npm programs:

node --version
v5.3.0
npm --version
3.3.12

Install Node.js from binary package

The binary package contains pre-compiled collection of programs and files. So there is no need for compilation. It is downloaded, uncompressed and copied to installation directory.

Binary package is available for 32-bit and 64-bit Linux versions. 32-bit package is identified with linux-x86 in package name and 64-bit package is identified with linux-x64 in package name. Note that CentOS 6 is available on 32-bit and 64-bit versions, whereas CentOS 7 is available only on 64-bit version. Here we use 64-bit version of Node.js 0.12.7 binary package. Other versions can be downloaded from https://nodejs.org/dist/.

Download the package, uncompress it and change to the package directory with following commands:

wget https://nodejs.org/dist/v5.3.0/node-v5.3.0-linux-x64.tar.gz
tar xzf node-v5.3.0-linux-x64.tar.gz
cd node-v5.3.0-linux-x64

We install Node.js in /usr/local directory. Use following command to copy the files to appropriate sub directories in /usr/local:

for dir in bin include lib share; do cp -par ${dir}/* /usr/local/${dir}/; done

To verify version of node and npm, type:

node --version
v5.3.0
npm --version
3.3.12

Installing Node.js from binary RPM

Node.js RPM can be installed from the EPEL (Extra Packages for Enterprise Linux) YUM repository. To enable EPEL repo, refer to previous article How to enable EPEL repo on CentOS.

After enabling EPEL repo, install Node.js and npm RPM packages using following YUM command:

yum -y install nodejs npm

To verify version of node and npm, type:

node --version
v0.10.36
npm --version
1.3.6

Install Node.js using NVM

NVM (Node Version Manager) is a BASH shell script to easily install, run and manage multiple versions of Node.js. Install nvm by using cURL or wget:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.30.1/install.sh | bash

or

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.30.1/install.sh | bash

Then source .bash_profile to set up environment for NVM:

source ~/.bash_profile

Now you can check which versions of Node.js are known to NVM:

nvm ls-remote
...
v4.2.4
v5.0.0
v5.1.0
v5.1.1
v5.2.0
v5.3.0

NVM can install any of these versions available in the list. For example, to install version 4.2.4, type:

nvm install v4.2.4
Downloading https://nodejs.org/dist/v4.2.4/node-v4.2.4-linux-x64.tar.xz...
######################################################################## 100.0%
WARNING: checksums are currently disabled for node.js v4.0 and later
Now using node v4.2.4 (npm v2.14.12)

To install the latest stable version, type:

nvm install stable
Downloading https://nodejs.org/dist/v5.3.0/node-v5.3.0-linux-x64.tar.xz...
######################################################################## 100.0%
WARNING: checksums are currently disabled for node.js v4.0 and later
Now using node v5.3.0 (npm v3.3.12)

To list versions installed by NVM:

nvm list
         v4.2.4
->       v5.3.0
         system
node -> stable (-> v5.3.0) (default)
stable -> 5.3 (-> v5.3.0) (default)
iojs -> N/A (default)

The version pointed by arrow is currently selected for use.

To switch between versions:

nvm use v4.2.4
Now using node v4.2.4 (npm v2.14.12)

For more information on NVM, check out https://github.com/creationix/nvm.

Conclusion

We showed you different methods to install Node.js in your CentOS 6 server. You may stick to any one of these methods which suits your deployment model and provides the required version of programs.

When you want complete control over building and installing Node.js, use source compilation. Since programs are natively built in the server by source compilation, Node.js may perform slightly better compared to other installation methods, though it is not guaranteed.

If you do not want to compile the source, but need some level of control such as choosing installation directory, then installing Node.js from binary package may suit your requirement.

Installing Node.js using binary RPM package from a YUM repository makes the installation and upgrade process easy.

When you want to try out different versions of Node.js, nvm may better suit your requirement. It makes installation and switching between different versions very easy.

Leave a Reply

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

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>