In this article given two different ways to install Node.js and npm on CentOS 8 system. To install from NVM is the best option if you are using Node.js for the development purposes.
Installing Node.js and npm from the CentOS repositories
You can install Node.js and npm from the standard CentOS repositories. Currenly, the Node.js version in the repositories is v10.x.
Run the below command to list the modules that provides
yum module list nodejs
It should show output as below:
Here, Stream 10 represents the version of Node.js and
[d] mark represents to installs a common set of runtime packages.
By typing below command you can install default Node.js package on your CentOS system:
sudo yum module install nodejs
To build dynamically loadable modules it’s required additional libraries, run the below command to install additional libraries.
sudo yum module install nodejs/development
You can verify the installation by typing:
It will show Node.js version:
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:
Do not use sudo as it will enable the script for the root user.
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.0/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
nvmor 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 the nvm installation by typing:
Now you have nvm installed on your CentOS 8 system. You can install the latest available stable version of Node.js using below command:
nvm install node
It will show output as below:
... Computing checksum with sha256sum Checksums matched! Now using node v13.3.0 (npm v6.13.1) Creating default alias: default -> node (-> v13.3.0)
Once the installation is finishe you can check the version of Node.js by typing :
Next, We will install two more versions of Node.js. To do so type following:
nvm install --lts
nvm install 10.17.0
Once the both of above versions are installed we can view list using below command:
-> v10.17.0 v12.13.1 v13.3.0 default -> node (-> v13.3.0) node -> stable (-> v13.3.0) (default) stable -> 13.0 (-> v13.3.0) (default) iojs -> N/A (default) lts/* -> lts/dubnium (-> v12.13.1) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.16.2 (-> N/A) lts/dubnium -> v10.17.0 lts/dubnium -> v12.13.1
In output version with arrow is represent current shell session. Default version is set to
v10.17.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 12.13.1
Now verify it by typing:
You also can set set version 12.13.1 as the default Node.js version using below command:
nvm alias default 12.13.1
You can uninstall a Node.js version type following command:
nvm uninstall 10.17.1
Install development tools
To be able to build native modules from npm we will need to install the development tools and libraries:
sudo dnf groupinstall 'Development Tools'
If you want to uninstall Node.js and npm packages, use the following command:
sudo yum module install nodejs
Here, explained how to install Node.js and npm using different methods on your CentOS 8 system. You can choose best method as per your requirement.
If you have any questions or suggestion, please leave comment below.