-
-
Save DaveThw/7d9cf5c7922df0eaa4fc10c51b979b8d to your computer and use it in GitHub Desktop.
A Bash script to install Ruby, using rbenv/ruby-build, on Ubuntu/Debian and derivatives
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ------------------------------------------------------------------------------------------------ | |
# Installs Ruby using rbenv/ruby-build - can be used multiple times, to install different versions | |
# | |
# Download and run locally: | |
# wget git.io/inst_ruby -O install_ruby.sh && chmod a+x install_ruby.sh | |
# ./install_ruby.sh 2.6.6 | |
# | |
# Run from the web: | |
# bash <(curl -sL git.io/inst_ruby) 2.6.6 | |
# | |
# Based on blacktm's script, found here: | |
# https://gist.github.com/blacktm/8302741 | |
# Modified / updated by Dave Thwaites: | |
# https://gist.github.com/DaveThw/7d9cf5c7922df0eaa4fc10c51b979b8d | |
# ------------------------------------------------------------------------------------------------ | |
if [[ "x$1" == "x" ]]; then | |
if [[ "${0:0:8}" == "/dev/fd/" ]]; then | |
# URL=https://gist.github.com/DaveThw/7d9cf5c7922df0eaa4fc10c51b979b8d/raw/install_ruby.sh | |
URL=git.io/inst_ruby | |
echo "Usage: bash <(curl -sL $URL) <version>" | |
echo "eg:" | |
echo " bash <(curl -sL $URL) 2.6.6" | |
echo "or" | |
echo " bash <(curl -sL $URL) 2.7.1" | |
else | |
echo "Usage: $0 <version>" | |
echo "eg '$0 2.6.6' or '$0 2.7.1'" | |
fi | |
exit | |
else | |
VER=$1 | |
fi | |
# Welcome message | |
echo -e " | |
This will install Ruby $VER using rbenv/ruby-build. | |
It will likely take about 2 hours to compile on the original Raspberry Pi, | |
about 35 minutes on the second generation, and about 16 minutes on the third. | |
Or about 7 1/2 mins in GalliumOS on an HP Chromebook 14 ('Falco').\n" | |
# Prompt to continue | |
read -p " Continue? (y/n) " ans | |
if [[ $ans != "y" ]]; then | |
echo -e "\nQuitting...\n" | |
exit | |
fi | |
echo | |
# Time the install process | |
START_TIME=$SECONDS | |
# update the list of packages available to install | |
sudo apt update | |
# Ensure git is installed | |
sudo apt install -y git | |
if [[ -d ~/.rbenv ]]; then | |
echo "rbenv already installed - pull latest updates:" | |
git -C ~/.rbenv pull | |
else | |
# Check out rbenv into ~/.rbenv | |
git clone https://github.com/rbenv/rbenv.git ~/.rbenv | |
fi | |
# Add ~/.rbenv/bin to $PATH, enable shims and autocompletion | |
read -d '' String <<"EOF" | |
# rbenv | |
export PATH="$HOME/.rbenv/bin:$PATH" | |
eval "$(rbenv init -)" | |
EOF | |
grep -q rbenv ~/.bashrc; if [[ "$?" = "0" ]]; then | |
echo "rbenv already enabled in ~/.bashrc" | |
else | |
# Save to ~/.bashrc | |
echo -e "\n${String}" >> ~/.bashrc | |
fi | |
echo "$PATH" | grep -q "/.rbenv/bin:"; if [[ "$?" = "0" ]]; then | |
echo "rbenv already enabled in current shell" | |
RBENV_ENABLED=yes | |
else | |
RBENV_ENABLED=no | |
# Enable rbenv for current shell | |
eval "${String}" | |
fi; | |
if [[ -d ~/.rbenv/plugins/ruby-build ]]; then | |
echo "ruby-build already installed - pull latest updates:" | |
git -C ~/.rbenv/plugins/ruby-build pull | |
else | |
# Install ruby-build as an rbenv plugin, adds `rbenv install` command | |
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build | |
fi | |
# Install ruby-build dependencies | |
# See: https://github.com/rbenv/ruby-build/wiki#suggested-build-environment | |
sudo apt install -y autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm-dev | |
# try to install libgdbm5 (may not work on all systems, eg Raspbery Pi) | |
sudo apt install -y libgdbm5 | |
# if that fails, install libgdbm3 | |
if [[ $? != 0 ]]; then sudo apt install -y libgdbm3; fi | |
# Install Ruby ($VER), don't generate RDoc to save lots of time | |
CONFIGURE_OPTS="--disable-install-doc --enable-shared" rbenv install $VER --verbose | |
# Set Ruby $VER as the global default | |
rbenv global $VER | |
# List available versions, if more than one: | |
if [[ $(rbenv versions | wc -l) > 1 ]]; then | |
echo -e "\nAvailable versions of Ruby:" | |
rbenv versions | |
fi | |
# Don't install docs for gems (saves lots of time) | |
if [[ -e ~/.gemrc ]]; then | |
grep -q "gem: --no-document" ~/.gemrc; if [[ "$?" != "0" ]]; then | |
# ~/.gemrc exists, but doesn't contain this line: | |
echo "gem: --no-document" >> ~/.gemrc | |
fi | |
else | |
# ~/.gemrc doesn't exist: | |
echo "gem: --no-document" > ~/.gemrc | |
fi | |
if [[ "$RBENV_ENABLED" = "no" ]]; then | |
# Reminder to reload the shell | |
echo -e "\nQuit and reload the current shell to get access to Ruby and rbenv." | |
echo "Or, if you want to keep this shell open, re-load your .bashrc file, with:" | |
echo " source ~/.bashrc" | |
fi | |
# Print the time elapsed | |
ELAPSED_TIME=$(($SECONDS - $START_TIME)) | |
echo -e "\nFinished in $(($ELAPSED_TIME/60/60)) hr, $(($ELAPSED_TIME/60%60)) min, and $(($ELAPSED_TIME%60)) sec\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment