Read in 5 minutes
How to install Node.js on CentOS 7
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
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:
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_10.x. Run the following command to add the yum repository:
curl -sL https://rpm.nodesource.com/setup_8.x | sudo bash -
Install Node.js and npm with yum
Once the NodeSource repository is enabled install Node.js and npm by typing:
sudo yum install nodejs
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:
Check npm version:
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:
Install NVM (Node Version Manager)
To download the
nvminstall 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
~/.nvmand 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:
Install Node.js using NVM
Now that we have
nvminstalled 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:
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:
-> 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.
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:
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
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:
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
Install Yarn on CentOS 7
Once the repository is added, install the Yarn package manager, with:
sudo yum install yarn
Verify the installation
Verify the installation by printing the version number:
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.