Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bash Script to install RVM and ruby, can be run from vagrant to provision a develop environment (run it with start param) or can be run standalone (without params).
#!/usr/bin/env bash
cd $HOME
function updateinstall(){
echo -e "Updating system and installing tools...\n"
sudo apt-get update
sudo apt-get install -y curl gnupg build-essential
sudo apt-get install -y gawk libreadline6-dev zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 autoconf libgmp-dev libgdbm-dev libncurses5-dev automake libtool bison pkg-config libffi-dev
}
function installnodeos() {
sudo apt-get install -y nodejs &&
sudo ln -sf /usr/bin/nodejs /usr/local/bin/node
if [[ $? -ne 0 ]]; then
echo "Error while installing Nodejs, please check...";
exit 1;
else
source $HOME/.bashrc
fi
}
function installrvm() {
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
curl -sSL https://get.rvm.io | bash -s stable
if [[ $? -ne 0 ]]; then
echo "Error while installing RVM, please check...";
exit 1;
fi
echo "source $HOME/.rvm/scripts/rvm" >> ~/.bash_profile
source $HOME/.rvm/scripts/rvm
}
function loadnvm() {
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
}
function installnvm() {
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
if [[ $? -ne 0 ]]; then
echo "Error while installing Nodejs, please check...";
exit 1;
else
source $HOME/.bashrc
loadnvm
fi
}
function listnodeversion() {
nvm ls-remote | less
echo -e "\n Choose a version from Node to install with nvm install x.x.x or nvm install --lts \n"
}
function listrubyversion() {
rvm list known
echo -e "\n Choose a version from Ruby to install with rvm install ruby-x.x.x or rvm install ruby --latest \n"
}
function installnode() {
lts=$1
loadnvm
if [[ -z $lts ]]; then
listnodeversion
echo -e "Enter node version to install or enter 'lts' to install lts version:"
read nodever
if [[ $nodever != "lts" ]]; then
nvm install $nodever
if [[ $? -ne 0 ]]; then
echo -e "Error installing node version, check version and try again";
return 1;
fi
else
nvm install --lts
if [[ $? -ne 0 ]]; then
echo -e "Error installing Node...";
return 1;
fi
fi
else
nvm install --lts
if [[ $? -ne 0 ]]; then
echo -e "Error installing Node...";
return 1;
fi
fi
}
function installruby() {
latest=$1
rvm > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo -e "\n RVM not installed or not found check and try again... \n";
if [[ -z $latest ]]; then
exit 1;
else
main
fi
fi
if [[ -z $latest ]]; then
listrubyversion
echo -e "Enter ruby version to install or enter 'latest' to install latest version:"
read rubyver
source $HOME/.rvm/scripts/rvm
if [[ $rubyver != "latest" ]]; then
rvm install $rubyver
if [[ $? -ne 0 ]]; then
echo -e "Error installing ruby version, check version and try again";
return 1;
else
rvm --default use ruby-$rubyver
fi
else
rvm install ruby
if [[ $? -ne 0 ]]; then
echo -e "Error installing Ruby...";
return 1;
else
rvm --default use ruby
fi
fi
else
rvm install ruby
if [[ $? -ne 0 ]]; then
echo -e "Error installing Ruby...";
return 1;
else
rvm --default use ruby
fi
fi
}
function installbundler(){
gem install bundler --no-rdoc --no-ri
if [[ $? -ne 0 ]]; then
echo -e "Error installing bundler";
return 1;
fi
}
function runall(){
latest=$1
lts=$2
updateinstall
installrvm
echo -e "Install node from Debian(D) repo or Install NVM (N) ? \n"
read option
case $option in
d|D)
echo -e "Installing node from Debian repo..."
installnodeos
;;
n|N)
echo -e "Installing node with NVM..."
installnvm
installnode $lts
;;
*)
echo -e "Node not selected, run again this script and select the node option..."
;;
esac
installruby $latest
installbundler
username=$(whoami)
if [[ $username == "vagrant" ]]; then
touch /home/vagrant/.provission
fi
}
function main() {
echo -e "\n"
echo -e " What do you want to install: \n 1) Install and update pre-requisites \n 2)Install node from Debian \n 3) RVM \n 4) NVM \n 5) NodeJs \n 6) Ruby \n 7) Bundler \n 8) All \n"
read option
case $option in
1)
echo -e "Installing pre-requisites...\n"
updateinstall
main
;;
2)
echo -e "Installing node from Debian..."
installnodeos
main;;
3)
echo -e "Installing RVM...\n"
installrvm
main
;;
4)
echo -e "Installing NVM...\n"
installnvm
main
;;
5)
echo -e "Installing NodeJS... \n"
installnode
main
;;
6)
echo -e "Installing Ruby... \n"
installruby
main
;;
7)
echo -e "Installing Bundler... \n"
installbundler
;;
8)
echo -e "Installing all... \n"
runall
;;
*)
esac
}
pathfile="/home/vagrant/.provission"
if [[ -f $pathfile ]]; then
if [[ -z $1 ]]; then
echo -e "Already installed...";
exit 0;
fi
elif [[ $1 = "start" ]]; then
runall latest lts
else
echo -e "Run this script with start parameter if you try to provission a box"
main
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.