The default package manager for Node.js is called npm; it is the world’s largest software registry.
In this guide, we will show you how to install Node.js and npm on CentOS 7 using various methods, which you can use to implement in any CentOS-based distribution.
Suppose you want to install Node.js as a runtime for deploying or hosting any Node.js application. In that case, you should use to install Node.js from the NodeSource repository, but if you are a developer and wanted to create a development environment for Node.js application development, prefer to go with NVM script to install Node.js.
You can choose the installation process as per your needs. The best idea is to go through the Node.js application documentation to find which version of Node.js is supported and need to install.
Install Node.js and npm from NodeSource
NodeSource is focused on enterprise support for Node.js and then maintains this repository with the latest version of Node.js.
We can use this repository to install any specific stable and the latest version of Node.js.
You need to follow these steps to install Node.js and npm from the NodeSource repository.
Step 01: To enable the NodeSource repository, run the following curl command as a sudo privileged user:
$ curl -sL https://rpm.nodesource.com/setup_10.x | sudo bash -
This command will add the NodeSource repository key to your system and create an apt source repository file to install all required packages and dependencies.
The above command will install Node.js 12. If you want to install another version of Node.js, replace “setup_12.x” in the above command with your version; for example, if you need version 14, then write “setup_14.x”.
Step 02: After enabling the NodeSource repository, you can install the Node.js and npm by executing the below command:
$ sudo yum install nodejs
Step 03: After installing Node.js and npm as both contain in the same package (node.js), verify the installation by checking the installed version using the following commands:
$ node --version
$ npm --version
Install Node.js and npm using NVM
As we know, the NVM (Node Version Manager) installation is used by the developer to install Node.js and npm it helps to install multiple active Node.js version in a system. It helps to install and uninstall any specific version of Node.js to use or test.
You can go through the following steps to install Node.js and npm using NVM script:
Step 01: Install NVM script
Use the following command to download and install the nvm script:
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
The above command will make a clone of the NVM repository from Github to th ~/.nvm directory:
output: => 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
Now NVM script is installed, but it not show in the same session, and it indicates in the output of the above command either you should close and reopen the terminal for a new session or add the path to nvm script to execute the command.
Once the script is in your PATH, verify the installed NVM by checking the NVM script version:
$ nvm --version
Step 02: Install Node.js and npm
Now you can use the command “nvm” to install the latest version of Node.js by using the following command:
$ nvm install node
output: Downloading and installing node v11.0.0... Downloading https://nodejs.org/dist/v11.0.0/node-v11.0.0-linux-x64.tar.xz... ######################################################################## 100.0% Computing checksum with sha256sum Checksums matched! Now using node v11.0.0 (npm v6.4.1) Creating default alias: default -> node (-> v11.0.0)
After completing the installation of Node.js, you can verify it by checking the version of Node.js:
$ node --version
Now, let us install another two version of Nod.js, the latest LTS and version 8.10.0 using the nvm command:
$ nvm install --lts $ nvm install 8.12.0
To list out the installed Node.js version, you can use the following command:
$ nvm ls
The output will look like below:
output: -> v8.12.0 # ACTIVE VERSION v10.13.0 v11.0.0 default -> node (-> v11.0.0) # DEFAULT VERSION node -> stable (-> v11.0.0) (default) stable -> 11.0 (-> v11.0.0) (default) iojs -> N/A (default) lts/* -> lts/dubnium (-> v10.13.0) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.14.4 (-> N/A) lts/carbon -> v8.12.0 lts/dubnium -> v10.13.0
To change the currently active version of Node.js using below command:
$ nvm use 10.13.0
output: Now using node v10.13.0 (npm v6.4.1)
If you want to change the version of default Node.js, use the following command:
$ nvm alias default 10.13.0
output: default -> 10.13.0 (-> v10.13.0)
Install development tools
To compile and install native addons from npm, you need to install development tools for the development environment.
To install development tools in the CentOS system, use the following command:
The above command will install all necessary packages for development, including GCC compilers:
$ sudo yum install gcc-c++ make
If you want to remove Node.js and npm package for some reasons, you can uninstall using the following command:
$ sudo yum remove nodejs npm
You have learned to install Node.js and npm in three different ways in your CentOS 7 machine. The methods of installation you can choose as per your requirements and preferences. Where the installation of Node.js and npm package from NodeServer and CentOS repository, CentOS repository is more comfortable, the NVM repository gives you more flexibility to use any version of Node.js in a single machine.
If you face any issue with installation or have doubt, question, or feedback, please leave a comment below.