Read in 5 minutes

last updated 

How to install Node.js on CentOS 7

This tutorial walks you through the steps to install 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, short for Node Package Manager is the default package manager for Node.js and the world’s largest software repository for the publishing of open-source Node.js packkages.

We will show you two different ways of installing Node.js and npm. In the first part of this tuturial we will install Node.js and npm using the yum package manager from the NodeSource repository and in the second part we will teach you how to install Node.js and npm using the nvm script.

How to install Node.js and npm from NodeSource

NodeSource is a company dedicated in providing enterprise-grade Node support and they maintain a consistently-updated Node.js repositories for Linux distributions.

To install Node.js and npm from the NodeSource repositories on your CentOS system, follow these steps:

  1. Add NodeSource yum repository

    First we need to add the NodeSource repository to our system. 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. Run the following command to add the yum repository:

    curl -sL https://rpm.nodesource.com/setup_8.x | sudo bash -
  2. Install Node.js and npm with yum

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

    sudo yum install nodejs
  3. Verify the Node.js and npm Installation

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

    Check Node.js version:

    node --version
    v8.11.1

    Check npm version:

    npm --version
    5.6.0

How to install Node.js and npm 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 install Node.js and npm using NVM on your CentOS system, follow these steps:

  1. Install NVM (Node Version Manager)

    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
  2. Install Node.js using NVM

    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
  3. Install multiple Node.js versions using NVM

    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.

  4. Change the active Node.js version using NVM

    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
  5. Change the default Node.js version using NVM

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

    nvm alias default 8.11.1

Install development tools

To be able to build native modules from npm we will need to install the development tools and libraries:

sudo yum install gcc-c++ make

Install Yarn package manager

Yarn is a JavaScript package manager compatible with npm. It was created to solve a set of problems with the npm such as speeding up the packages building process and reducing errors related to network connectivity.

The official Yarn repository provides the most up-to-date version and is the recommended way of installing Yarn for CentOS.

To install Yarn through yum on your CentOS system, follow these steps:

  1. Add Yarn yum repository

    To add the the GPG key and the repository to your system run the following commands:

    curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
    sudo rpm --import https://dl.yarnpkg.com/rpm/pubkey.gpg
  2. Install Yarn on CentOS 7

    Once the repository is added, install the Yarn package manager, with:

    sudo yum install yarn
  3. Verify the installation

    Verify the installation by printing the version number:

    yarn --version
    1.7.0

Conclusion

We have shown you two different ways to install Node.js and npm on your CentOS 7 server. The method you choose depends on your requirements and preferences. While installing the packaged version from the NodeSource repository is easier, the nvm method gives you more flexibility for adding and removing different Node.js versions on a per user basis.

If you have any questions or feedback, feel free to comment below.