Skip to content

Instantly share code, notes, and snippets.

@jbboynton
Created November 21, 2020 10:58
Show Gist options
  • Save jbboynton/d12fad4de06f545aa3add82ea7ced0e7 to your computer and use it in GitHub Desktop.
Save jbboynton/d12fad4de06f545aa3add82ea7ced0e7 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Builds the specified version of Ruby from source.
set -eo pipefail
version="$1"
build_directory="/opt/ruby/$version"
source_url="https://cache.ruby-lang.org/pub/ruby/${version%??}/ruby-$version.tar.gz"
print_usage() {
cat <<EOM
Usage: build_ruby [VERSION]
EOM
}
get_options() {
while getopts '' flag; do
case "${flag}" in
*) print_usage; exit 1 ;;
esac
done
}
info() {
echo -e "$1"
}
error() {
echo -e >&2 "Error: $1"
exit 1
}
version_given() {
[ -n "$version" ]
}
create_build_directory() {
mkdir -p "$build_directory"
}
download_source() {
wget -P "$build_directory" "$source_url"
}
extract_source() {
tar xf "$build_directory/ruby-$version.tar.gz" -C "$build_directory"
}
build_ruby() {
cd "$build_directory/ruby-$version"
./configure
make
sudo make install
}
get_options "$@"
version_given || error 'no Ruby version specified'
create_build_directory
download_source
extract_source
build_ruby
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment