Read in 3 minutes

How to install Node.js on CentOS 7

This tutorial walks you through installing Node.js and npm on a CentOS 7 machine. Node.js is a cross-platform JavaScript run-time environment that allows server-side execution of JavaScript code. Node.js is mainly used on the back-end, but it is also popular as a full-stack and front-end solution. npm is is the default package manager for Node.js.

Install Node.js from NodeSource

There are several different ways of getting Node.js installed on a CentOS 7 machine. In the first part of this tutorial we will install Node.js from the NodeSource repository.

First we need to enable the NodeSource repository:

curl -sL https://rpm.nodesource.com/setup_8.x | sudo bash -

The current LTS version of Node.js is version 8.x. If you want to install version 10 just change setup_8.x with setup_10.x

Once the NodeSource repository is enabled install Node.js and npm by typing:

sudo yum install nodejs

To check that the installation was successful, we can run the following commands which will print Node.js and npm versions:

node --version
v8.11.1
npm --version
5.6.0

Install Node.js using NVM

NVM (Node Version Manager) is a bash script used to manage multiple active Node.js versions. NVM allows us to install and uninstall any specific Node.js version which means we can have any number of Node.js versions we want to use or test.

To download the nvm install script type:

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

The script will clone the nvm repository from Github to ~/.nvm and add the script Path to your Bash or ZSH profile.

=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

As the output above shows, you should either close and reopen your terminal or run the commands to add the path to nvm script to your current session.

To verify that nvm was properly installed type:

nvm --version
0.33.11

Now that we have nvm installed we can install the latest available version of Node.js, by typing:

nvm install node
Downloading and installing node v10.1.0...
Downloading https://nodejs.org/dist/v10.1.0/node-v10.1.0-linux-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v10.1.0 (npm v5.6.0)
Creating default alias: default -> node (-> v10.1.0)

We can verify the Node.js version, by typing:

node --version
v10.1.0
Advertisement

Let’t install two more versions, the latest LTS version and version 6.14.2

nvm install --lts
nvm install 6.14.2

Once LTS version and 6.14.2 are installed to list all installed Node.js instances type:

nvm ls
->      v6.14.2                 # ACTIVE VERSION
        v8.11.1
        v10.1.0
         system
default -> node (-> v10.1.0)    # DEFAULT VERSION
node -> stable (-> v10.1.0) (default)
stable -> 10.1 (-> v10.1.0) (default)
iojs -> N/A (default)
lts/* -> lts/carbon (-> v8.11.1)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.14.2
lts/carbon -> v8.11.1

The output tell us that the entry with an arrow on the left (-> v6.14.2), is the version used in the current shell session and the default version is set to v10.1.0. Default version is the version that will be active when opening new shells.

To change the current active version you can use the following command:

nvm use 8.11.1

and verify it with:

nvm current
v8.11.1

If you want to change the default version to 8.11.1 then type:

nvm alias default 8.11.1

Install development tools

Lastly we will also install the development tools so that we are able to build native addons from npm.

sudo yum install gcc-c++ make

That’s it! If you have any questions or feedback, feel free to comment below.