Installing nodejs and npm on Linux CentOS

You will need to have root access to your server or have access to an account that has sudo powers on your servers. Check this post to see how to give a linux user sudo powers.

I did some experimentation, as my usual, to get comfortable with all the commands I was using.

If you are just looking for the commands go to the Command Summary.

Experimentation

There are many ways to install node. I find this install instructions page to be very useful. Find your linux distribution and follow the instructions. I will be installing on CentOS.

[ahmed@amayem ~]$ yum repolist

If you don’t see EPEL in the list then you need to install it via RPM. EPEL stands for Fedora Extra Packages for Enterprise Linux (EPEL), which provides us with the packages we need for node. Even if you do have it the following command will update it because of the -U flag.

[ahmed@amayem ~]$ rpm -Uvh http://download-i2.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
Retrieving http://download-i2.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
warning: /var/tmp/rpm-tmp.0bhoBO: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
error: can't create transaction lock on /var/lib/rpm/.rpm.lock (Permission denied)

Ooops forgot to sudo.

[ahmed@amayem ~]$ sudo rpm -Uvh http://download-i2.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
Retrieving http://download-i2.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
warning: /var/tmp/rpm-tmp.fBVcz2: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                ########################################### [100%]
   1:epel-release           ########################################### [100%]

We have it now, though with a warning. A quick search about the warning tells us that it means that I don’t have the key to check the signature of the package. The signature basically ensures that the package is actually from who it’s supposed to be from and isn’t tampered with. Since it’s just a warning, technically, we could ignore it and continue at our own risk, but let’s do the responsible thing and verify the signature. After reading this page for a while I understood I have to import the keys using the rpm --import PUBKEY command. However, there weren’t any examples and I was a bit at a loss, so it was time for more experimentation again. I found a list of keys on the same page and found the EPEL package under the heading RPM-GPG-KEY-EPEL-6. Let’s try that:

[ahmed@amayem ~]$ rpm --import RPM-GPG-KEY-EPEL-6
error: RPM-GPG-KEY-EPEL-6: import read failed(2).

Nope, it didn’t like that. Next I found a link under the same header which took me a txt file. This looks more promising.

[ahmed@amayem ~]$ rpm --import https://fedoraproject.org/static/0608B895.txt
error: cannot get exclusive lock on /var/lib/rpm/Packages
error: cannot open Packages index using db3 - Operation not permitted (1)
error: cannot open Packages database in /var/lib/rpm
error: https://fedoraproject.org/static/0608B895.txt: key 1 import failed.

The numbers of erros have increased, thats a good sign. It means we got farther than last time. This time the errors have to do with permissions. It would seem we need sudo here.

[ahmed@amayem ~]$ sudo rpm --import https://fedoraproject.org/static/0608B895.txt

It worked! Now we need to verify the package. According to redhat here we should run the following command to verify:

[ahmed@amayem ~]$ rpm -K --nosignature http://download-i2.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
http://download-i2.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm: sha1 md5 OK

So we got the sha1 md5 OK. I’m wondering why we were told to use the --nosignature option. The man page give me this.

--nosignature
              Don’t verify package or header signatures when reading.

So I guess it checked some other signature. Anyways I will try the other option --checksig and see what happens:

[ahmed@amayem ~]$ rpm -K --checksig http://download-i2.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
http://download-i2.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm: rsa sha1 (md5) pgp md5 OK

This time I got a few more words: rsa sha1 (md5) pgp md5 OK. I’m not sure but I suspect that what that actually did was check that the linked package is official. However, I want to check if the package I downloaded was good. Let me try to update the package.

[ahmed@amayem ~]$ sudo rpm -U http://download-i2.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
    package epel-release-6-8.noarch is already installed

Nope. Let’s force it.

[ahmed@amayem ~]$ sudo rpm -U --force http://download-i2.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

Seems to have worked. But no I’m gonna be paranoid and insist I don’t get the warning message again. Let’s erase the package and install again.

[ahmed@amayem ~]$ sudo rpm -e http://download-i2.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
error: package http://download-i2.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm is not installed

Huh? Not installed? But it just told me it was installed. I guess this time it wants the local package name. It gave me the name when I tried to update earlier. Let’s try using that name.

[ahmed@amayem ~]$ sudo rpm -e epel-release-6-8.noarch

It worked! Now if you run yum repolist you won’t find epel anymore. Let’s reinstall with the same command as before:

[ahmed@amayem ~]$ sudo rpm -Uvh http://download-i2.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
Retrieving http://download-i2.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
Preparing...                ########################################### [100%]
   1:epel-release           ########################################### [100%]

No warning! Looks like we did it :D.

Let’s install nodejs and npm using our new EPEL repo:

[ahmed@amayem ~]$ sudo yum install nodejs npm --enablerepo=epel

You will be prompted in the middle to install. Simply press y then enter. There will be a huge output that should end with Complete!.

Congratulations you have installed nodejs and npm!

Command Summary

[ahmed@amayem ~]$ sudo rpm --import https://fedoraproject.org/static/0608B895.txt
[ahmed@amayem ~]$ sudo rpm -Uvh http://download-i2.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
Retrieving http://download-i2.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
Preparing...                ########################################### [100%]
   1:epel-release           ########################################### [100%]
[ahmed@amayem ~]$ sudo yum install nodejs npm --enablerepo=epel

Press y then enter when prompted. There will be a huge output that should end with Complete!.

Congratulations you have installed nodejs and npm!

Testing

Run these two commands to check you have them:

[ahmed@amayem ~]$ node --version
v0.10.26
[ahmed@amayem ~]$ npm --version
1.3.6

Updates

I updated the joyent install instructions page to include the key import above as well as the sudo for the install.

References

  1. Joyent install instructions
  2. Fedora Extra Packages for Enterprise Linux (EPEL)
  3. Fedora Project keys
  4. Redhat rpm instructions

Ahmed Amayem has written 90 articles

A Web Application Developer Entrepreneur.