• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to install Node.js and npm on CentOS 7

Written by Admin, Updated On August 18, 2019
centos, nodejs, npm, nvm
How to Install Node.js and npm on CentOS 7

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.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install Composer on CentOS 7
Next Article   How to Setup SSH Keys on Debian 10 Buster

Related Posts

  • How to Install Php 8 on CentOS 8

    How to Install PHP 8 on CentOS 8

    January 27, 2021
  • How to Install Python 3.9 on CentOS 8

    How to Install Python 3.9 on CentOS 8

    December 31, 2020
  • How to Install GIMP 2.10 on CentOS 8

    How to Install GIMP 2.10 on CentOS 8

    December 30, 2020

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook