
Node.js is an opensource cross-platform JavaScript run-time environment for server-side execution of JavaScript code. Mostly, Node.js is used on the back-end, but it is also popular as a full-stack and front-end solution. npm is the very popular and default package manager for Node.js with large number of packages. In this tutorial, we described how to install Node.js and npm on CentOS 7 system.
You can install the Node.js packages using yum package manager from NodeSource repository, if your purpose is only to deploy Node.js applications. To install from NVM is the best option if you are using Node.js for the development purposes.
Prerequisites
Before start installation, you should logged in as non-root user with sudo privileges.
Install Node.js and npm on CentOS 7
You can install Node.js and npm on your CentOS 7 system by multiple ways. You can follow any of following to install:
Install Node.js and npm from the NodeSource repository
NodeSource is providing enterprise level Node support and they maintain consistently updated Node.js repository for Linux distributions. Perform the below steps to install Node.js and npm from the NodeSource repositories on your CentOS 7 machine:
First of all you need to add NodeSource yum repository to your system. Currently version 10.x. is the LTS version of Node.js. You can change setup_10.x with setup_8.x in the command below If you want to install version 8.
Execute the below curl command to add the NodeSource yum repository to your system:
curl -sL https://rpm.nodesource.com/setup_10.x | sudo bash -
Once the repository enabled, you can install Node.js and npm using following command:
sudo yum install nodejs
It will prompted to you to import the repository GPG key, you have to type y and press Enter.
At last, Verify the Node.js and npm Installation to check whether the installation is successful or not. Run below command which will show the versions of Node.js and npm.
It will show Node.js version:
node --version
v10.13.0
It will show npm version:
npm --version
6.4.1
Install Node.js and npm using nvm
NVM (Node Version Manager) script is used to manage multiple Node.js versions. You can install or uninstall specific Node.js versions using NVM.
At first, we need to download nvm install script on your system using below command:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
It will copy the nvm
repository from Github to the ~/.nvm directory and add the nvm path to your Bash 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 you can see in output, it is suggesting to either open new shell session or run the commands to add the path to the nvm script to your current session.
You can verify that nvm is installed properly by typing:
nvm --version
0.33.11
Now you have nvm installed on your Ubuntu system. You can install the latest available version of Node.js using below command:
nvm install node
It will show output as below:
Downloading and installing node v12.0.0... Downloading https://nodejs.org/dist/v12.0.0/node-v12.0.0-linux-x64.tar.xz… ################################################################## 100.0% Computing checksum with sha256sum Checksums matched! Now using node v12.0.0 (npm v6.9.0) Creating default alias: default -> node (-> v12.0.0)
Once the installation is finished you can check the version of Node.js by typing :
node --version
v12.0.0
Next, We will install two more versions of Node.js. To do so type following:
nvm install --lts
nvm install 9.10.1
Once the both of above versions are installed we can view list using below command:
nvm ls
-> v9.10.1
v10.15.3
v12.0.0
default -> node (-> v12.0.0)
node -> stable (-> v12.0.0) (default)
stable -> 12.0 (-> v12.0.0) (default)
iojs -> N/A (default)
lts/* -> lts/dubnium (-> v10.15.3)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.16.0 (-> N/A)
lts/dubnium -> v10.15.3
In output version with arrow is used for the current shell session. Default version is set to v12.0.0
and it will be used when you open new shell sessions.
You can change the current default version of Node.js by using the following command:
nvm use 10.15.3
Now verify it by typing:
nvm current
v10.15.3
You also can set set version 10.15.3 as the default Node.js version using below command:
nvm alias default 10.15.3
You can uninstall a Node.js version type following command:
nvm uninstall 9.10.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
Conclusion
You learned how to install Node.js and npm using different methods on your CentOS 7 machine. To install from the NodeSource repository is faster and easier way. You can also check this tutorial about How to install and use yarn on CentOS 7.
If you have any questions or suggestion, please leave comment below.
Leave a Reply