Stok Footage

Continually experimenting with new ideas and techniques — Reconstructing, Developing, Modernising.

Building Ruby 1.8.7-p374 on OSX Mavericks (now homebrew has dropped apple-gcc42)

Recently I needed to build an older Ruby to use on a relatively new Mac running Mavericks. These days I use chruby (installed by homebrew) as my ruby switcher of choice, and ruby-install (also from homebrew) as my ruby building utility.

Mavericks comes with a Ruby 2.0.0p451 as its default Ruby, but there is a 1.8.7 build installed in /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby, and if that’s enough for you then you can use it.

I want to be able to switch rubies using chruby I did some digging about on the web, and here’s my current solution:

I only use Ruby 1.8.7 for old projects which don’t use Tk, so I can live without understanding how to make 1.8.7 and Tk work together on my Mac.

  • Make sure Xcode is installed.
  • Make sure I have done:
    1
    xcode-select --install
  • Make sure I have gcc-4.8 installed as explained in ruby-install’s Ruby section:
    1
    2
    brew tap homebrew/versions
    brew install gcc48
  • Run this command to build and install ruby:
    1
    2
    3
    4
    ruby-install ruby 1.8.7-p374 -- \
      CC=gcc-4.8\
      --with-openssl-dir=$(brew --prefix openssl)\
      --without-tcl --without-tk
  • Then build & install rubygems as it doesn’t come as part of Ruby 1.8.7:
    1
    2
    3
    4
    5
    6
    7
    chruby ruby-1.8.7-p374
    wget http://production.cf.rubygems.org/rubygems/rubygems-2.4.1.tgz
    tar xvzf rubygems-2.4.1.tgz
    cd rubygems-2.4.1
    ruby setup.rb
    gem install rubygems-update
    update_rubygems

I have this script in my bin as ~/bin/ruby-setup which I use to build various Ruby versions and install some of my favourite gems automatically. To build ruby-1.8.7p374 I would invoke it like this:

1
ruby-setup ruby 1.8.7-p374
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
#
# This installs ruby and my "favourite gems" using ruby-install
#
# Did you remember to:
#
#  * xcode-select --install
#  * install XQuartz

set -e

install_ruby () {
        if [[ "$WHAT" == "ruby" && "$VERSION" == 1.8.7* ]]; then
                ruby-install "$WHAT" "$VERSION" -- \
                        CC=gcc-4.8 \
                        --with-openssl-dir=$(brew --prefix openssl) \
                        --without-tcl --without-tk
        else
                ruby-install "$WHAT" "$VERSION" -- \
                        CPPFLAGS=-I/opt/X11/include/
        fi
}

use_installed_ruby () {
        set +x
        source /usr/local/opt/chruby/share/chruby/chruby.sh
        set -x
        chruby "$WHAT-$VERSION"
}

install_favourite_gems () {
        if [[ "$WHAT" == "ruby" && "$VERSION" =~ ^1.8.7 ]]; then
                GEM_BUILD_TMPDIR="$(mktemp -d -t ruby-setup)" || exit 1
                pushd "$GEM_BUILD_TMPDIR"
                rubygems=rubygems-2.4.1
                wget http://production.cf.rubygems.org/rubygems/${rubygems}.tgz
                tar xvzf ${rubygems}.tgz
                cd ${rubygems}
                ruby setup.rb
                gem install rubygems-update
                update_rubygems
                popd
        fi

        for gem in rake rdoc bundler rubygems-bundler awesome_print hoe; do
                gem install "$gem"
        done

        if [[ "$WHAT" == "ruby" && "$VERSION" =~ ^1.8.7|^1.9.1 ]]; then
                gem install rdoc-data; rdoc-data --install
        else
                gem install rubocop # rainbow needs ruby >=1.9.2
                gem install pry
                gem install pry-plus
        fi

        gem regenerate_binstubs
}

# could use ${1:?...} here, but argument count is easier.
if [ $# -ne 2 ]; then
        echo "Usage: $(basename $0) what version" 1>&2
        exit 1
fi

WHAT="$1"
VERSION="$2"

trap 'rm -rf "$GEM_BUILD_TMPDIR"' EXIT

set -x

install_ruby
use_installed_ruby
install_favourite_gems

Leave a Reply

Your email address will not be published. Required fields are marked *