If you ever installed and upgraded iojs in CentOS or Ubuntu VPS, you will find that this jobs takes time to type commands into terminal. I have to install iojs new version frequently so I decide to write a Bash script to automate installing and upgrading iojs process. This script works well on fresh Ubuntu, CentOS 6.x, CentOS 7.x.

Just copy the bash script then save to file for example iojs.sh. Change mode of this file and sudo to execute it. Happy coding with iojs

chmod u+x iojs.sh
sudo ./iojs.sh
If you want to install specific version of iojs then provide version of iojs as argument
sudo ./iojs.sh 2.0.1
#!/bin/bash
## Check version of iojs

echo "Download and install iojs"
## Get version of iojs from argument
if [ -z "$1" ]
then
  echo "Assume iojs version = 2.0.0"
  iojsversion="2.0.0"
else
  iojsversion=$1
fi


## Make sure user runs this bash script as root
if [ $(id -u) -ne 0 ]; then
   echo -e  "\e[41mMust run as root user\e[49m"
   exit
fi

## return 0 if a command does not exist
function checkIfCommandExist {
  command -v $1 >/dev/null 2>&1 || {
	#not found 
	echo 0
        return
  }
  #found
  echo 1
}

## check if node is installed
if [ $(checkIfCommandExist node) -eq 1 ]; then
  nodeversion=$(node -v)
  ## Remove v and . from  node version string
  nodeversionnumber=${nodeversion//[v.]/}
  iojsversionnumber=${iojsversion//./}
  if [ $nodeversionnumber -lt $iojsversionnumber ]; then
     echo "Installed node version is older. Upgrade now"
  fi
fi
function getOSVersion {
	if [ -f "/etc/os-release" ]; then
		osname=`cat /etc/os-release | grep ID= | head -1`
		osname=${osname:3}
		osversion=`cat /etc/os-release | grep VERSION_ID= | head -1`
		osversion=${osversion:11}
	else
		if [ -f "/etc/system-release" ]; then
			sysrelease=`cat /etc/system-release`
			if [[ $sysrelease == CentOS* ]]; then
				osname="centos"
			else
				exit
			fi
		else
			echo "Sorry my script only support CentOS and Ubuntu"
			exit
		fi
	fi
}

function install {
  if [ -z "$1" ]; then
      echo "You must provide packname to be installed"
      exit
  fi
  case "$osname" in
  ubuntu)
    apt-get install $1
    ;;
  centos)
    yum install $1
    ;;
  esac
}
getOSVersion

## install curl if it does not exist
if [ $(checkIfCommandExist curl) -eq 0 ]; then
   install curl
fi

iojsfile="iojs-v${iojsversion}-linux-x64.tar.gz"

if [ -f "$iojsfile" ]
then
        echo "$iojsfile found."
else
        echo "$iojsfile not found."
        remote_file=https://iojs.org/dist/v${iojsversion}/${iojsfile}
        echo "start download: $remote_file"
        file_exist=$(curl  -s -o /dev/null -IL -w %{http_code} $remote_file)
        if [ $file_exist -eq 200 ]; then
          curl -O $remote_file
        else
	  echo -e  "\e[41m$remote_file does not exist\e[49m"
	  exit
        fi
fi

echo "Extract ${iojsfile}"

tar -xzf ${iojsfile}

if [ -d "iojs-v${iojsversion}-linux-x64" ]
then
	echo "Extract to folder iojs-v${iojsversion}-linux-x64 successfully"
else
	echo "Extract failed"
	exit
fi

echo "Move iojs-v${iojsversion} to /opt folder"
# if in /opt folder there is already iojs folder, then remove it
if [ -d "/opt/iojs-v${iojsversion}" ]
then
	rm -rf /opt/iojs-v${iojsversion}
fi

mv iojs-v${iojsversion}-linux-x64 /opt/iojs-v${iojsversion}

echo "Create symbolic links to iojs, node, npm"
rm -f /usr/bin/iojs
rm -f /usr/bin/node
rm -f /usr/bin/npm

ln -s /opt/iojs-v${iojsversion}/bin/iojs /usr/bin/iojs
ln -s /opt/iojs-v${iojsversion}/bin/node /usr/bin/node
ln -s /opt/iojs-v${iojsversion}/bin/npm /usr/bin/npm

#--------------------------------
echo "node version: `node -v`"
echo "iojs version: `iojs -v`"
echo "npm version: `npm -v`"
modulepath="`npm list -g | head -1`/node_modules"
#--------------------------------

## Install express-generator
npm install -g express-generator
rm -f /usr/bin/express
ln -s ${modulepath}/express-generator/bin/express /usr/bin/express