Skip to content

Instantly share code, notes, and snippets.

@dayne
Last active September 25, 2024 19:03
Show Gist options
  • Save dayne/7f052537545e667872a3c6b4f2498696 to your computer and use it in GitHub Desktop.
Save dayne/7f052537545e667872a3c6b4f2498696 to your computer and use it in GitHub Desktop.
ruby installer methods

Chruby Ruby Fun

A script for Ubuntu/Debian systems to setup ruby-install and chruby to manage your Ruby environments in that user account.

This chruby.sh script is meant to be used in a .bash.d style setup where all .bash.d/*.sh scripts are run as part of the .bashrc run.

Warning: This has been build and tested in Ubuntu/Debian environments with apt.

If you don't have .bash.d setup you can add the following line to your .bashrc file:

source $HOME/.bash.d/chruby.sh

install methods

pull it down directly into .bash.d, make it executable, and run it

cd .bash.d
wget https://gist.githubusercontent.com/dayne/7f052537545e667872a3c6b4f2498696/raw/chruby.sh
chmod +x chruby.sh
./chruby

or clone the repo

git clone [email protected]:7f052537545e667872a3c6b4f2498696.git gist-dayne-chruby
ln gist-dayne-chruby/chruby.sh ~/.bash.d/chruby.sh

Tasks

Docker-updaterc-test

docker run -v "$(pwd)/chruby.sh:/chruby.sh" ubuntu bash -c \
       "apt update && apt install -y curl && mkdir .bash.d && ./chruby.sh update"

Docker-full-test

docker run -v "$(pwd)/chruby.sh:/chruby.sh" ubuntu bash -c \
       'echo "round 0: Inital sanity check" 
        ./chruby.sh sanity
        apt update && apt install -y curl 
        echo "round 1: Just run"
        ./chruby.sh
        echo "round 2: Install chruby & ruby-build"
        ./chruby.sh install
        echo "round 3: Install a ruby"
        ./chruby.sh ruby-install
        echo "round 3: See if it is available"
        source chruby.sh
        echo "round 5: Last sanity check"
        ./chruby.sh sanity
       '
#!/usr/bin/bash
# lives in $HOME/.bash.d/chruby.sh
#
# Hacking & want to test hacks - try this funny dance:
# CMD="chruby.sh" # be in the same dir as the script
# docker run -it --rm -v "$(pwd)/${CMD}:/${CMD}" ubuntu bash
# # now inside that docker instance test it out:
# . chruby.sh
function _ctrc-chruby-sanity() {
sanity=0
if command -v ruby-install > /dev/null; then
echo "good: ruby-install is available"
else
echo "FAIL: ruby-install is not in path"; sanity=1
fi
if [[ $(type -t chruby) == "function" ]]; then
echo "good: chruby is available as a function"
else
echo "FAIL: chruby not properly avilable in the shell"; sanity=1
fi
if [[ -f $HOME/.ruby-version ]]; then
echo "good: $HOME/.ruby-version exists"
else
echo "FAIL: Missing $HOME/.ruby-version"; sanity=1
fi
if command -v ruby > /dev/null; then
echo "good: Ruby is available: $(which ruby)"
else
echo "FAIL: No ruby is available in the environment"; sanity=1
fi
if [[ $sanity -eq 0 ]]; then
echo "good: Full sanity check passed -- SUCCESS"
return 0
else
echo "FAIL: Sanity failed."
return 1
fi
}
function _ctrc-update-chrubyrc() {
if [[ ! -d $HOME/.bash.d/ ]]; then
mkdir $HOME/.bash.d/
fi
cd $HOME/.bash.d
BACKUP=".chruby.sh.backup$(date +%Y%m%d)"
if [[ ! -f "${BACKUP}" ]]; then
if [[ -f chruby.sh ]]; then
echo "backing up chruby.sh to $(pwd)/${BACKUP}"
cp chruby.sh "${BACKUP}"
fi
else
echo "Skipping backup: File exists $(pwd)/$BACKUP"
fi
if ! command -v curl > /dev/null; then
echo "Warning: missing curl. Assuming docker & installing"
apt update && apt install -y curl
fi
curl -s -o chruby.sh https://gist.githubusercontent.com/dayne/7f052537545e667872a3c6b4f2498696/raw/chruby.sh
if [[ $? -eq 0 ]]; then
chmod +x chruby.sh
echo "download of chruby.sh into $HOME/.bash.d successful"
fi
}
function ct-install-ruby() {
echo "###############################################"
echo "# ruby-install install running #"
echo "# https://github.com/postmodern/ruby-install #"
echo "###############################################"
sleep 1
echo "Apt install for the library and build dependancies"
if ! command -v sudo > /dev/null; then
# needed for docker test environment
apt update && apt install -y sudo
fi
sudo apt update
if [ $? -eq 0 ]; then
echo "apt cache updated"
else
echo "Error: unable to run sudo apt update"
exit 1
fi
sudo apt-get install -y wget git curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev libffi-dev zlib1g libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison pkg-config
if [ $? -eq 0 ]; then
echo "Library and build dependancies are available"
else
echo "Error: unable to install needed build dependancies"
read -p "Do you want to continue? (Y/y/yes to proceed): " answer
case $answer in
[Yy][Ee][Ss]|[Yy])
echo "Continuing..."
;;
*)
echo "Exiting..."
exit 1
;;
esac
fi
pushd . > /dev/null
if [ ! -d $HOME/.local/share/ruby-install ]; then
echo "# mkdir -p $HOME/.local/share/ruby-install #"
sleep 0.5
mkdir -p $HOME/.local/share/ruby-install
fi
cd $HOME/.local/share/ruby-install
local RBI_VER="0.9.3"
echo "# wget ruby-install-$RBI_VER.tar.gz #"
wget -q "https://github.com/postmodern/ruby-install/releases/download/v${RBI_VER}/ruby-install-${RBI_VER}.tar.gz"
if [ $? -eq 0 ]; then
echo "# DOWNLOADED: ruby-install-$RBI_VER.tar.gz #"
else
echo "# Failed download #"
return 1
fi
tar -xzvf ruby-install-$RBI_VER.tar.gz
if [ $? -eq 0 ]; then
cd ruby-install-$RBI_VER/
else
echo "# Failed tar xvfz #"
return 1
fi
PREFIX="$HOME/.local" make install
if [ $? -eq 0 ]; then
echo "ruby-install-$RBI_VER installed"
else
echo "SAD: ruby-install-$RBI_VER failed to install"
return 1
fi
echo $PATH | grep ".local/bin" > /dev/null
if [ ! $? -eq 0 ]; then
echo "Warning: $HOME/.local/bin not in PATH. Not attempting chruby install"
exit 1
fi
hash -r #reload and find ruby-install hopefully
if [ ! -d $CT_CHRUBY_INSTALL_DIR ]; then
mkdir -p $CT_CHRUBY_INSTALL_DIR
fi
cd $CT_CHRUBY_INSTALL_DIR
CHRUBY_VERSION="0.3.9"
# wget or clone? how to pick latest version?
CHRUBY_URL="https://github.com/postmodern/chruby/releases/download/v${CHRUBY_VERSION}/chruby-${CHRUBY_VERSION}.tar.gz"
wget --quiet -O chruby-${CHRUBY_VERSION}.tar.gz $CHRUBY_URL
if [ ! $? -eq 0 ]; then
echo "Failed to download chruby: $CHRUBY_URL"
exit 1
fi
tar -xzvf chruby-${CHRUBY_VERSION}.tar.gz
if [ ! $? -eq 0 ]; then
echo "Failed to tar xvfz chruby-${CHRUBY_VERSION}"
exit 1
fi
cd chruby-${CHRUBY_VERSION}
if [ ! $? -eq 0 ]; then
echo "failed to enter chruby-${CHRUBY_VERSION}"
exit 1
fi
PREFIX="$HOME/.local" make install
if [ ! $? -eq 0 ]; then
echo "Failed to install chruby: PREFIX=$HOME/.local make install"
exit 1
fi
}
function ct-install-aruby() {
# just make sure that new bin dir is in path now
if [ -d $HOME/.local/bin ]; then
echo "bin check"
echo $PATH | grep "\.local/bin" > /dev/null
if [ $? -ne 0 ]; then
export PATH=$PATH:$HOME/.local/bin
fi
fi
echo "Starting ct-install-aruby()"
ruby-install -U
if [ -f $HOME/.ruby-version ]; then
ruby-install $(cat $HOME/.ruby-version)
else
echo "no $HOME/.ruby-version exists"
ruby-install ruby
if [[ $? -eq 0 ]]; then
echo "Successful install of a ruby"
fi
if [[ -d $HOME/.rubies ]]; then
RUBIES=$HOME/.rubies
elif [[ -d /opt/rubies ]]; then
RUBIES=/opt/rubies # probably root/docker context
else
echo "No rubies found in $HOME/.rubies or /opt/rubies"
exit 1
fi
aruby=$(ls -1 $RUBIES | grep "ruby-" | tail -n 1)
if [[ -f "$RUBIES/$aruby/bin/ruby" ]]; then
echo "Making $aruby your ruby in $HOME/.ruby-version"
echo "$aruby" >> $HOME/.ruby-version
else
echo "No ruby found in $RUBIES"
fi
fi
popd > /dev/null
return 0
}
CT_CHRUBY_INSTALL_DIR="$HOME/.local/share/chruby"
echo $PATH | grep "\.local/bin" > /dev/null
if [ $? -ne 0 ]; then
export PATH=$PATH:$HOME/.local/bin
hash -r
fi
if ! command -v ruby-install > /dev/null ; then
echo "Missing ruby-install: FIX: ct-install-ruby"
fi
if [ -f $CT_CHRUBY_INSTALL_DIR/chruby.sh ]; then
source $CT_CHRUBY_INSTALL_DIR/chruby.sh
unset CT_CHRUBY_INSTALL_DIR # clean up name space
fi
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
# The script is being sourced.
if [[ $(type -t chruby) == "function" ]]; then
unset -f ct-install-chruby
# chruby ready to go
if [ -f $HOME/.ruby-version ]; then
chruby `cat $HOME/.ruby-version`
if [ $? -eq 0 ]; then
#echo "chruby `cat $HOME/.ruby-version`"
unset -f ct-install-ruby
true
else
echo "FAILED: chruby `cat $HOME/.ruby-version`"
fi
else
echo "Warning: Missing $HOME/.ruby-version"
echo " Try running: ct-install-ruby"
fi
source $HOME/.local/share/chruby/auto.sh
else
echo "Warning: Missing chruby. Fix this with: ct-install-ruby"
fi
else
# The script is being executed. so just run it
case "$1" in
updaterc)
_ctrc-update-chrubyrc
;;
install)
ct-install-ruby
;;
ruby-install)
ct-install-aruby
;;
sanity)
_ctrc-chruby-sanity
;;
*)
echo "Options:"
echo " updaterc: downloads latest .bash.d/chruby.sh"
echo " install: installs a ruby-install and chruby logic"
echo " ruby-install: installs a ruby logic"
echo " sanity: sanity checks things"
;;
esac
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment