Updated on 5/1/20
Problem scenario
You have a Linux server with 1 GB of RAM. You want to install node and npm because you want to try out the latest JavaScript technologies. (You were persuaded to not use Gulp or Grunt to manage build scripts. You want to use npm because of this link.) You want to use the same copy of the bash script on CentOS/RedHat/Fedora, Debian/Ubuntu, and SUSE Linux to do this installation. What do you do?
Solution
Prerequisite
Do not attempt to run this on a server with less than 1 GB of RAM. We recommend you have 3 GB of memory, but 2 GB of that can be virtual memory. If you need assistance creating 2 GB of swap space memory, see this posting.
Warning
If you use a RedHat derivative (e.g., CentOS, RHEL, or Fedora), this script will install Perl (in case you had a special version installed you can comment out this line). This script will remove Perl if it is already installed. You can comment out the "yum -y remove perl" command if you so desire. If you want to try an older, alternative set of directions for installing a RedHat derivative, this link may be of value, but we recommend this set below. Also, this installation can take over one hour to complete.
Recommendation for Debian/Ubuntu users
If you are using Ubuntu/Debian Linux, this command will do what you want easily: sudo apt -y install nodejs npm
The command above will not work if you are behind a firewall or the repositories on your network do not have the dependencies involved. If this is your problem, then proceed with the script below instead.
n.b. If you must use Python 2, and cannot install Python 3, use the attached file "npm install with python2.txt." For normal Linux servers, with recommend Python 3 and the newer version of npm as shown below.
1. Create a file like this in /tmp/ called node.sh:
# Script written by www.continualintegration.com
# The apt-get, zypper, and yum commands will error out depending on the distribution.
# Running them on a Linux distro that does not support them would not normally harm anything.
# This script for NPM verison 14.x works with Python 3.x
version="14.1.0" # Change if desired. This will be the version installed for the non-SUSE distros (e.g. Debian & RedHat)
distro=$(cat /etc/*-release | grep NAME)
debflag=$(echo $distro | grep -i "ubuntu") # This will be empty if it is not Ubuntu Linux.
if [ -z "$debflag" ] #If it is not Ubuntu, check if it is Debian.
then
debflag=$(echo $distro | grep -i "debian") # This will be empty if it is not Debian Linux.
fi
if [ -z "$debflag" ]
then
echo "...still determining Linux distribution..." # At this point it could be SUSE or a RedHat distro
else
echo "You are using either Ubuntu Linux or Debian Linux."
apt-get -y update # This is needed for AWS Ubuntu servers (it won't hurt to run on a GCP Debian as of February 2018)
apt-get -y install gcc g++ make python3 # python is needed for AWS Ubuntu AMI (not GCP Debian as of February 2018)
cd /opt/
curl http://nodejs.org/dist/v$version/node-v$version.tar.gz > node-v$version.tar.gz
tar xzvf node-v$version.tar.gz && cd node-v$version
./configure
make
make install
fi
rhflag=$(echo $distro | grep -i "red*hat")
if [ -z "$rhflag" ]
then #If it is not RedHat, see if it is CentOS or Fedora.
rhflag=$(echo $distro | grep -i "centos")
if [ -z "$rhflag" ]
then #If it is neither RedHat nor CentOS, see if it is Fedora.
echo "It does not appear to be CentOS or RHEL..."
rhflag=$(echo $distro | grep -i "fedora")
fi
fi
if [ -z "$rhflag" ]
then
echo "...still determining Linux distribution..."
else
echo "You have a RedHat distribution (e.g., CentOS, RHEL, or Fedora)"
yum -y install gcc gcc-c++ make python3 # make is included as a "just in case"
yum -y groupinstall 'Development Tools'
ln -s /usr/bin/python3 /usr/bin/python
cd /opt/
curl http://nodejs.org/dist/v$version/node-v$version.tar.gz > node-v$version.tar.gz
tar xzvf node-v$version.tar.gz && cd node-v$version
./configure
make
make install
sudoversion=$(man sudo | grep Sudo | awk '{print $2}')
echo "If you see a number below that is lower than 1.7, you need to update sudo"
echo $sudoversion # Why it needs to be 1.7 or higher: CVE-2005-2959, CVE-2005-4158 & CVE-2006-0151
echo "The number above should be greater than 1.7. If not, update your sudo command to 1.7 or higher"
echo "Assuming that the version above was greater than 1.7 modify your /etc/sudoers file"
fi
suseflag=$(echo $distro | grep -i "suse")
if [ -z "$suseflag" ]
then
if [ -z "$debflag" ]
then
if [ -z "$rhflag" ]
then
echo "*******************************************"
echo "Could not determine the Linux distribution!"
echo "Installation aborted. Nothing was done."
echo "******************************************"
exit
fi
fi
else
echo "You have Linux SUSE."
zypper -n addrepo http://download.opensuse.org/tumbleweed/repo/oss/ oss
zypper --no-gpg-checks update
zypper -n install make gcc cpp-7-2.9.x86_64 gcc-c++-7-2.9.x86_64
cd /opt/
curl http://nodejs.org/dist/v$version/node-v$version.tar.gz > node-v$version.tar.gz
tar xzvf node-v$version.tar.gz && cd node-v$version
./configure
make
make install
fi
npm i -g npm
sleep 1
finvar=$(/usr/local/bin/node -v)
if [ -z "$rhflag" ]
then
echo "It looks like Node version " $finvar " was installed!"
echo "run these commands below to double check"
echo "npm -v"
echo "/usr/local/bin/node -v"
else
linenumber=$(cat /etc/sudoers | grep -n secure_path | sed 's/[^0-9]*//g')
if [ "$linenumber" -eq 80 ]
then
yum -y install perl
cp /etc/sudoers /etc/sudoers.bak
perl -i -pe 's/.*/Defaults secure_path = \/sbin:\/bin:\/usr\/sbin:\/usr\/bin:\/usr\/local\/bin/ if $.==80' /etc/sudoers
yum -y remove perl
echo "We removed Perl. Reinstall it if necessary."
else
echo "You will need to modify /etc/sudoers so that the secure_path setting includes /usr/local/bin"
echo "If you do not modify it, you will not be able to use 'sudo node' or 'sudo npm' commands"
fi
echo "Node and npm seem to have been installed. Use these commands below to check:"
echo "npm -v"
echo "node -v"
fi
2. Run this command: sudo bash /tmp/node.sh
3. The above script may take 30 minutes or more to complete. When it is done, test it by running these commands: npm -v
node -v