How to upgrade Node.js from v6 to v12 on CentOS Linux 7

In this blog post I will explain how you can upgrade Node.js to the most recent version using the “yum” package manager utility on CentOS Linux 7.

On my machine, I had previously installed Node.js using the following command:

$ sudo yum install nodejs

Let’s check the current version and location of Node.js and npm (the Node.js package manager):

$ node --version
v6.17.1
$ npm --version
3.10.10

$ which node
/bin/node
$ which npm
/bin/npm

I can see from https://nodejs.org/en/about/releases/ that the last Active LTS version is v12, so I would like to upgrade to that version.

The upgrade steps for CentOS are explained here: https://github.com/nodesource/distributions#rpminstall

First, you need to add the NodeSource yum repository to your system. This can be done by using the following command as user root:

$ curl -sL https://rpm.nodesource.com/setup_12.x | bash -

This command will first use the curl tool to download the “setup_12.x” script and then execute this script using bash.

This is the output from the script:

## Installing the NodeSource Node.js 12.x repo...


## Inspecting system...

+ rpm -q --whatprovides redhat-release || rpm -q --whatprovides centos-release || rpm -q --whatprovides cloudlinux-release || rpm -q --whatprovides sl-release
+ uname -m

## Confirming "el7-x86_64" is supported...

+ curl -sLf -o /dev/null 'https://rpm.nodesource.com/pub_12.x/el/7/x86_64/nodesource-release-el7-1.noarch.rpm'

## Downloading release setup RPM...

+ mktemp
+ curl -sL -o '/tmp/tmp.kNjfOcFMcS' 'https://rpm.nodesource.com/pub_12.x/el/7/x86_64/nodesource-release-el7-1.noarch.rpm'

## Installing release setup RPM...

+ rpm -i --nosignature --force '/tmp/tmp.kNjfOcFMcS'

## Cleaning up...

+ rm -f '/tmp/tmp.kNjfOcFMcS'

## Checking for existing installations...

+ rpm -qa 'node|npm' | grep -v nodesource

## Your system appears to already have Node.js installed from an alternative source.
Run `sudo yum remove -y nodejs npm` to remove these first.


## Run `sudo yum install -y nodejs` to install Node.js 12.x and npm.
## You may also need development tools to build native addons:
sudo yum install gcc-c++ make
## To install the Yarn package manager, run:
curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
sudo yum install yarn

Let’s check if the new yum repository has been added correctly:

$ ls -la /etc/yum.repos.d/|grep nodesource
-rw-r--r--. 1 root root 474 Apr 23 2019 nodesource-el7.repo

Based on the output from the “setup_12.x” script, we first need to remove the existing version of Node.js:

$ sudo yum remove -y nodejs npm
...
Removed:
  nodejs.x86_64 1:6.17.1-1.el7                                   npm.x86_64 1:3.10.10-1.6.17.1.1.el7

Complete!

Now let’s install the new version of Node.js:

$ sudo yum list available nodejs

Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Available Packages
nodejs.x86_64 2:12.14.1-1nodesource nodesource

$ sudo yum install nodejs

Total download size: 22 M
Installed size: 65 M
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/nodesource/packages/nodejs-12.14.1-1nodesource.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 34fa74dd: NOKEY
Public key for nodejs-12.14.1-1nodesource.x86_64.rpm is not installed
nodejs-12.14.1-1nodesource.x86_64.rpm                                                                           |  22 MB  00:00:02
Retrieving key from file:///etc/pki/rpm-gpg/NODESOURCE-GPG-SIGNING-KEY-EL
Importing GPG key 0x34FA74DD:
 Userid     : "NodeSource <gpg-rpm@nodesource.com>"
 Fingerprint: 2e55 207a 95d9 944b 0cc9 3261 5ddb e8d4 34fa 74dd
 Package    : nodesource-release-el7-1.noarch (installed)
 From       : /etc/pki/rpm-gpg/NODESOURCE-GPG-SIGNING-KEY-EL
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 2:nodejs-12.14.1-1nodesource.x86_64                                                                                 1/1
  Verifying  : 2:nodejs-12.14.1-1nodesource.x86_64                                                                                 1/1

Installed:
  nodejs.x86_64 2:12.14.1-1nodesource

Complete!

Let’s check the versions again:

$ node --version
v12.14.1
$ npm --version
6.13.4

All done!

HTH

Matthias

 

 

 

 

 

 

 

 

19 thoughts on “How to upgrade Node.js from v6 to v12 on CentOS Linux 7

  1. Pingback: Using APEX as front-end for MySQL databases REST-enabled with Node.js – Matthias Hoys

  2. Pingback: “update nodejs centos” Code Answer – My Blog

  3. I got an error when i tried to run this command- “sudo yum install nodejs” .
    So i found a solution for this error. I run “yum clean all” command to solve the error. Than again run the “sudo yum install nodejs” command.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.