Skip to content

Instantly share code, notes, and snippets.

@isakb
Created August 29, 2012 17:32
Show Gist options
  • Select an option

  • Save isakb/3515925 to your computer and use it in GitHub Desktop.

Select an option

Save isakb/3515925 to your computer and use it in GitHub Desktop.
Dependency checker and installer for node.js and npm
#!/bin/bash -e
#
# This script checks if useful versions of node.js and npm are installed
# on the system, and if not it attempts to install them.
#
# Should work on Debian based systems and OS X.
# So far tested on Kubuntu 12.04.
#
_die(){
echo "ERROR: $*"
echo
echo "You need to manually install the dependencies in order to contine."
exit 1
}
_is_git_installed(){
_v="$( git --version )"
if [[ "$_v" =~ \ 1\.[7-9] ]] || [[ "$_v" =~ \ 2\. ]] ; then
return 0
else
return 1
fi
}
_is_node_installed(){
_v="$( node -v )"
if [[ "$_v" =~ v0\.8 ]] || [[ "$_v" =~ v1 ]] ; then
return 0
else
return 1
fi
}
_is_npm_installed(){
_v="$( npm -v )"
if [[ "$_v" =~ ^2 ]] ; then
return 0
else
return 1
fi
}
_install_node(){
case $OSTYPE in
linux*) _install_node_on_linux_with_apt_get
;;
darwin*) _install_node_on_osx_with_brew
;;
*) _die "Unknown OS"
;;
esac
}
_install_node_on_osx_with_brew(){
if ! which brew ; then
_die "I need brew, I think. Feel free to improve the install script."
fi
# See https://gist.github.com/579814
PREFIX="$( brew --prefix )"
sudo mkdir -p $PREFIX/{share/man,bin,lib/node,include/node}
sudo chown -R $USER $PREFIX/{share/man,bin,lib/node,include/node}
brew install node
node -v
}
# See http://ariejan.net/2011/10/24/installing-node-js-and-npm-on-ubuntu-debian
_install_node_on_linux_with_apt_get(){
if ! which apt-get >/dev/null ; then
_die "apt-get not found. Feel free to improve the install script."
fi
if [ -d ./node ] ; then
_die "Will not overwrite existing folder: `pwd`/node"
fi
sudo apt-get update
sudo apt-get install git-core curl build-essential openssl libssl-dev
_owns_node_folder=yes
git clone https://github.com/joyent/node.git
cd node
git checkout v0.8.8
./configure
make
sudo make install
cd -
node -v
}
_install_npm(){
if [ -d ./node ] ; then
_die "Will not overwrite existing folder: `pwd`/npm"
fi
_owns_npm_folder=yes
git clone http://github.com/isaacs/npm.git
cd npm
# assume that master is stable
./configure
make
sudo make install
cd -
npm -v
}
_main(){
if ! _is_git_installed ; then
# Should 'never' happen actually (unless a wizard is at the keyboard).
_die "Seems like you checked out a git repo without having git...(?)"
fi
if ! _is_node_installed ; then
echo "Installing node.js ..."
_install_node || _die "It seems like installation of node.js failed"
else
echo "node OK"
fi
if ! _is_npm_installed ; then
echo "Installing npm ..."
_install_npm || _die "It seems like installation of npm failed"
else
echo "npm OK"
fi
}
_cleanup(){
if [ -d ./node ] && [ $_owns_node_folder ] ; then
rm -rf ./node
fi
if [ -d ./npm ] && [ $_owns_npm_folder] ; then
rm -rf ./npm
fi
}
# MAIN:
# Cleanup on exit and allow smooth ^C.
trap exit INT
trap _cleanup EXIT
_main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment