Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active December 18, 2015 17:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/5820213 to your computer and use it in GitHub Desktop.
Save havenwood/5820213 to your computer and use it in GitHub Desktop.
A script for OS X with Homebrew for automatically setting up ruby-install, chruby and the latest stable Ruby and JRuby.

Installation

Install and configure ruby-install and chruby and build the latest Ruby and JRuby:

curl -fsSL https://gist.github.com/havenwood/5820213/raw/install.sh | sh

Or if you prefer, paste the install.sh script below in your Terminal.

Dependencies

- OS X
- Homebrew

If you don't have the brew command, install Homebrew:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

And make sure you have Xcode installed so you can build from source.

Usage

Install additional Rubies with ruby-install. Select the current Ruby with chruby. Edit ~/.ruby-version to change the default Ruby.

#!/usr/bin/env bash
set -e
#
# Prints a log message.
#
function log()
{
if [[ -t 1 ]]; then
echo -e "\x1b[1m\x1b[32m>>>\x1b[0m \x1b[1m\x1b[37m$1\x1b[0m"
else
echo ">>> $1"
fi
}
#
# Prints an error message.
#
function error()
{
if [[ -t 1 ]]; then
echo -e "\x1b[1m\x1b[31m!!!\x1b[0m \x1b[1m\x1b[37m$1\x1b[0m" >&2
else
echo "!!! $1" >&2
fi
}
#
# Prints an error message and exists with -1.
#
function fail()
{
error "$*"
exit -1
}
#
# Install ruby-install and chruby brew packages.
#
function install_ruby_install_and_chruby()
{
log "Installing ruby-install and chruby ..."
brew install ruby-install chruby
}
#
# Configure bash or zsh dotfiles for chruby.
#
function configure_chruby_dotfiles()
{
local source_chruby="source /usr/local/opt/chruby/share/chruby/chruby.sh"
local source_auto="source /usr/local/opt/chruby/share/chruby/auto.sh"
if [[ -n BASH_VERSION ]]; then
log "Configuring chruby in ~/.bashrc and ~/.bash_profile ..."
$source_chruby >> "$HOME/.bashrc"
$source_auto >> "$HOME/.bash_profile"
elif [[ -n ZSH_VERSION ]]; then
log "Configuring chruby in ~/.zshrc ..."
$source_chruby >> "$HOME/.zshrc"
$source_auto >> "$HOME/.zshrc"
fi
}
#
# Create a .ruby-version file that selects Ruby 2.0.
#
function create_ruby_version_file()
{
if [[ -f "$HOME/.ruby-version" ]]; then
log "A ~/.ruby-version file exists, skipping creation ..."
else
log "Creating a ~/.ruby-version file ..."
touch "$HOME/.ruby-version"
echo "ruby-2" >> "$HOME/.ruby-version"
fi
}
#
# Install latest stable Ruby.
#
function install_ruby()
{
log "Installing latest stable Ruby ..."
ruby-install ruby
}
#
# Install latest stable JRuby.
#
function install_jruby()
{
log "Installing latest stable JRuby ..."
ruby-install jruby
}
install_ruby_install_and_chruby || fail
configure_chruby_dotfiles || fail
create_ruby_version_file || fail
install_ruby || fail
install_jruby || fail
log "Successfully installed chruby, ruby-install and latest stable Ruby and JRuby."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment