How to install Node.js on CentOS 7
Introduction
Node.js is a very 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 show you different methods to install Node.js on your CentOS 7 Linux server.
Requirements
Many of the commands used here require root user privilege on the server.
Installation methods
Following are the different installation methods that we use:
- Installing Node.js from binary RPM
- Install Node.js from source package
- Install Node.js from binary package
- Install Node.js using NVM
Installing Node.js from binary RPM
The third party NodeSource repository contains binary RPM packages of Node.js for CentOS 7. NodeSource has separate repository for Node.js 5.x, 4.x, 0.12.x and 0.10.x. Following commands add YUM repository for the specified version branch:
Node.js 5.x,
curl -sL https://rpm.nodesource.com/setup_5.x | bash -
Node.js 4.x,
curl -sL https://rpm.nodesource.com/setup_4.x | bash -
Node.js 0.12.x,
curl -sL https://rpm.nodesource.com/setup_0.12 | bash -
Node.js 0.10.x,
curl -sL https://rpm.nodesource.com/setup | bash -
If you want to install the latest Node.js 5.x in the server, run above curl command for 5.x branch. Then install Node.js and npm packages using yum:
yum -y install nodejs npm
To verify version of node and npm, type:
node --version v5.3.0
npm --version 3.3.12
Install Node.js from source package
In this method, we download source package of Node.js, compile it and install the binary programs and other files.
At first, install the development packages required for compiling Node.js source. Use following YUM command:
yum -y install gcc gcc-c++ make
Source code of latest version of Node.js is available for download on https://nodejs.org/download/. Older versions can be downloaded from https://nodejs.org/dist/. Here, we download the source package of Node.js version 5.3.0, compile and install it using following commands:
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 ./configure make make install
Using –version switch, verify version of node and npm (Node Package Manager) 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.
We use 64-bit version of Node.js 5.3.0 binary package. Other versions can be downloaded from https://nodejs.org/dist/.
Download the package, uncompress it and change to the package directory using 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
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. To install NVM, use any of following curl and wget commands:
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
Using ls-remote sub command you can check which versions of Node.js are available on 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 four different methods to install Node.js in your CentOS 7 server. Use 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.