Mac(Mavericks)にRuby1.8.7を入れようとしたら大変だった話

今更Ruby1.8.7かよ的な空気もありつつちょっと試したいことがあったのでインストールしようとしたら割りと大変だったので記録を残しておく。

rbenvはインストール済み、という前提。

素直に1.8.7系をインストールしようとすると、こうなった

```
$ rbenv install 1.8.7-p374
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1

ERROR: This package must be compiled with GCC, but ruby-build couldn't
find a suitable `gcc` executable on your system. Please install GCC
and try again.

DETAILS: Apple no longer includes the official GCC compiler with Xcode
as of version 4.2. Instead, the `gcc` executable is a symlink to
`llvm-gcc`, a modified version of GCC which outputs LLVM bytecode.

For most programs the `llvm-gcc` compiler works fine. However,
versions of Ruby older than 1.9.3-p125 are incompatible with
`llvm-gcc`. To build older versions of Ruby you must have the official
GCC compiler installed on your system.

TO FIX THE PROBLEM: Install Homebrew's apple-gcc42 package with this
command: brew tap homebrew/dupes ; brew install apple-gcc42

You will need to install the official GCC compiler to build older
versions of Ruby even if you have installed Apple's Command Line Tools
for Xcode package. The Command Line Tools for Xcode package only
includes `llvm-gcc`.

BUILD FAILED

```

なんかgccが必要らしいからgccをインストールする
※ `brew tap homebrew/dupes ; brew install apple-gcc42`しろって書いてあるけど、この時気付かなかった。

```
$ brew install gcc
Error: No available formula for gcc
GCC is now maintained in homebrew-versions, with major version
number in formula name as suffix. Please tap using:

    brew tap homebrew/versions

and then install GCC based on its version, e.g., 'brew install gcc47'.
```

` brew tap homebrew/versions`してから`brew install gcc47`しないといけないらしい。言われたとおりにやってみる

```
$ brew tap homebrew/versions
Cloning into '/usr/local/Library/Taps/homebrew-versions'...
remote: Reusing existing pack: 1578, done.
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 1581 (delta 0), reused 2 (delta 0)
Receiving objects: 100% (1581/1581), 515.07 KiB | 310.00 KiB/s, done.
Resolving deltas: 100% (814/814), done.
Checking connectivity... done
Tapped 127 formula
```

```
$ brew install gcc47
==> Installing dependencies for gcc47: gmp4, mpfr2, libmpc08, ppl011, cloog-ppl015
==> Installing gcc47 dependency: gmp4
==> Downloading ftp://ftp.gmplib.org/pub/gmp-4.3.2/gmp-4.3.2.tar.bz2
...
(長いので略)
```

※ gcc47のインストール1時間くらいかかった

終わったら再挑戦

```
$ rbenv install 1.8.7-p374
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Downloading ruby-1.8.7-p374.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/b72a0bc5b824398537762e5272bbb8dc
Installing ruby-1.8.7-p374...

BUILD FAILED

Inspect or clean up the working tree at /var/folders/sv/0m72rddx6_39qr64ygd7msx40000gn/T/ruby-build.20140112221410.79997
Results logged to /var/folders/sv/0m72rddx6_39qr64ygd7msx40000gn/T/ruby-build.20140112221410.79997.log

Last 10 log lines:
x ruby-1.8.7-p374/configure
x ruby-1.8.7-p374/parse.c
/var/folders/sv/0m72rddx6_39qr64ygd7msx40000gn/T/ruby-build.20140112221410.79997/ruby-1.8.7-p374 /var/folders/sv/0m72rddx6_39qr64ygd7msx40000gn/T/ruby-build.20140112221410.79997 ~/Workspace/gyazo-sinatra
checking build system type... i686-apple-darwin13.0.0
checking host system type... i686-apple-darwin13.0.0
checking target system type... i686-apple-darwin13.0.0
checking whether the C compiler works... no
configure: error: in `/var/folders/sv/0m72rddx6_39qr64ygd7msx40000gn/T/ruby-build.20140112221410.79997/ruby-1.8.7-p374':
configure: error: C compiler cannot create executables
See `config.log' for more details

BUILD FAILED

Inspect or clean up the working tree at /var/folders/sv/0m72rddx6_39qr64ygd7msx40000gn/T/ruby-build.20140112221410.79997
Results logged to /var/folders/sv/0m72rddx6_39qr64ygd7msx40000gn/T/ruby-build.20140112221410.79997.log

Last 10 log lines:
x ruby-1.8.7-p374/parse.c
/var/folders/sv/0m72rddx6_39qr64ygd7msx40000gn/T/ruby-build.20140112221410.79997/ruby-1.8.7-p374 /var/folders/sv/0m72rddx6_39qr64ygd7msx40000gn/T/ruby-build.20140112221410.79997 ~/Workspace/gyazo-sinatra
checking build system type... i686-apple-darwin13.0.0
checking host system type... i686-apple-darwin13.0.0
checking target system type... i686-apple-darwin13.0.0
checking whether the C compiler works... no
configure: error: in `/var/folders/sv/0m72rddx6_39qr64ygd7msx40000gn/T/ruby-build.20140112221410.79997/ruby-1.8.7-p374':
configure: error: C compiler cannot create executables
See `config.log' for more details
make: *** No targets specified and no makefile found.  Stop.
```

エラーログは変わったけどまだ駄目。
ぐぐったらopensslが必要って言うのを見つけたのでインストールする
※ これに意味があったのかどうかは今となっては不明

```
$ brew install openssl
==> Downloading http://www.openssl.org/source/openssl-1.0.1f.tar.gz
######################################################################## 100.0%
==> perl ./Configure --prefix=/usr/local/Cellar/openssl/1.0.1f --openssldir=/usr/local/etc/openssl zlib-dynamic shared darwin64-x86_64-cc enable-ec_nistp_64_gcc_128
==> make depend
==> make
==> make test
==> make install MANDIR=/usr/local/Cellar/openssl/1.0.1f/share/man MANSUFFIX=ssl
==> Caveats
This formula is keg-only, so it was not symlinked into /usr/local.

Mac OS X already provides this software and installing another version in
parallel can cause all kinds of trouble.

The OpenSSL provided by OS X is too old for some software.

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include

==> security find-certificate -a -p /Library/Keychains/System.keychain > '/usr/local/etc/openssl/osx_cert.pem.tmp'
==> security find-certificate -a -p /System/Library/Keychains/SystemRootCertificates.keychain >> '/usr/local/etc/openssl/osx_cert.pem.tmp'
==> mv -f /usr/local/etc/openssl/osx_cert.pem.tmp /usr/local/etc/openssl/osx_cert.pem
==> Summary
?  /usr/local/Cellar/openssl/1.0.1f: 429 files, 15M, built in 5.0 minutes
```

再挑戦しても結果は変わらず

```
$ rbenv install 1.8.7-p370
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Downloading ruby-1.8.7-p370.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/98b00bbd1cdde3116155edb6e555b781
Installing ruby-1.8.7-p370...

BUILD FAILED

Inspect or clean up the working tree at /var/folders/sv/0m72rddx6_39qr64ygd7msx40000gn/T/ruby-build.20140112223317.5732
Results logged to /var/folders/sv/0m72rddx6_39qr64ygd7msx40000gn/T/ruby-build.20140112223317.5732.log

Last 10 log lines:
x ruby-1.8.7-p370/wince/sys/types.h
x ruby-1.8.7-p370/wince/sys/stat.c
/var/folders/sv/0m72rddx6_39qr64ygd7msx40000gn/T/ruby-build.20140112223317.5732/ruby-1.8.7-p370 /var/folders/sv/0m72rddx6_39qr64ygd7msx40000gn/T/ruby-build.20140112223317.5732 ~
checking build system type... i686-apple-darwin13.0.0
checking host system type... i686-apple-darwin13.0.0
checking target system type... i686-apple-darwin13.0.0
checking whether the C compiler works... no
configure: error: in `/var/folders/sv/0m72rddx6_39qr64ygd7msx40000gn/T/ruby-build.20140112223317.5732/ruby-1.8.7-p370':
configure: error: C compiler cannot create executables
See `config.log' for more details.

BUILD FAILED

Inspect or clean up the working tree at /var/folders/sv/0m72rddx6_39qr64ygd7msx40000gn/T/ruby-build.20140112223317.5732
Results logged to /var/folders/sv/0m72rddx6_39qr64ygd7msx40000gn/T/ruby-build.20140112223317.5732.log

Last 10 log lines:
x ruby-1.8.7-p370/wince/sys/stat.c
/var/folders/sv/0m72rddx6_39qr64ygd7msx40000gn/T/ruby-build.20140112223317.5732/ruby-1.8.7-p370 /var/folders/sv/0m72rddx6_39qr64ygd7msx40000gn/T/ruby-build.20140112223317.5732 ~
checking build system type... i686-apple-darwin13.0.0
checking host system type... i686-apple-darwin13.0.0
checking target system type... i686-apple-darwin13.0.0
checking whether the C compiler works... no
configure: error: in `/var/folders/sv/0m72rddx6_39qr64ygd7msx40000gn/T/ruby-build.20140112223317.5732/ruby-1.8.7-p370':
configure: error: C compiler cannot create executables
See `config.log' for more details.
make: *** No targets specified and no makefile found.  Stop.
```

※ version変わったらいけんじゃね？とか試行錯誤してたのでp374からp370になってる

もうちょっと調べるとgccを`$CC`環境変数で指定しないといけないらしいので、さっきインストールしたgccを指定して再挑戦

```
$ CC=/usr/local/Cellar/gcc47/4.7.3/bin/gcc-4.7 rbenv install 1.8.7-p370
Downloading ruby-1.8.7-p370.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/98b00bbd1cdde3116155edb6e555b781
Installing ruby-1.8.7-p370...
Installed ruby-1.8.7-p370 to /Users/takc923/.rbenv/versions/1.8.7-p370

Downloading rubygems-1.6.2.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/0c95a9869914ba1a45bf71d3b8048420
Installing rubygems-1.6.2...
Installed rubygems-1.6.2 to /Users/takc923/.rbenv/versions/1.8.7-p370
```

できた。
めっちゃ時間かかった(´・ω・｀)