Rubyのインストール(Mac OS X, rbenv)

Rubyのインストール(Mac OS X, rbenv)

Mac OS Xでrbenvを使ってRubyをインストールする手順とトラブルシューティングです。

手順

Mac OS X

基本的にはbrew installと一連のコマンドで完結する。

全体の流れとしては、rbenvとruby-buildをインストールして、それからrbenvでRuby 2.1.0をインストールし、最後に.bash_profileなどのprofileに初期化コマンドを追加する。

なお、inetractive shellにおいてのみrbenvを使いたい場合は.bashrcに追加する。

 

まずrbenvとruby-buildをインストールする。

$ brew install rbenv ruby-build
Warning: Your Xcode (4.6.3) is outdated
Please update to Xcode 5.0.1.
Xcode can be updated from the App Store.
==> Downloading https://github.com/sstephenson/rbenv/archive/v0.4.0.tar.gz
######################################################################## 100.0%
==> Caveats
To use Homebrew's directories rather than ~/.rbenv add to your profile:
  export RBENV_ROOT=/usr/local/var/rbenv
To enable shims and autocompletion add to your profile:
  if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
==> Summary
🍺  /usr/local/Cellar/rbenv/0.4.0: 31 files, 152K, built in 2 seconds
==> Installing ruby-build dependency: openssl
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/openssl-1.0.1f.mountain_lion.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.1f.mountain_lion.bottle.tar.gz
==> 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
==> Installing ruby-build
==> Downloading https://github.com/sstephenson/ruby-build/archive/v20140214.tar.gz
######################################################################## 100.0%
==> ./install.sh
🍺  /usr/local/Cellar/ruby-build/20140214: 108 files, 472K, built in 2 seconds

rbenvでRuby 2.1.0をインストールする。

$ CONFIGURE_OPTS="--with-openssl-dir=`brew --prefix openssl`" rbenv install 2.1.0
Downloading ruby-2.1.0.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/9e6386d53f5200a3e7069107405b93f7
Installing ruby-2.1.0...
Installed ruby-2.1.0 to /Users/yusuke.kuoka/.rbenv/versions/2.1.0

最後に.bash_profileにrbenvの初期化コマンドを追加する。

# ~/.bash_profileの最後
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi

試しにrbenvでデフォルトのRubyを切り替えて、起動してみる。

以下のようにrubyが~/.rbenv/shims/rubyを指していて、バージョンが切り替え先のそれになっていればOK。

$ rbenv global 2.1.0
$ which ruby
/Users/yusuke.kuoka/.rbenv/shims/ruby
$ ruby -v
ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-darwin12.0]

トラブルシューティング

The Ruby openssl extension was not compiled. Missing the OpenSSL lib?

現象

rbenv install <Rubyのバージョン>を実行すると、以下のようなエラーとなってしまい、インストールが完了しない。

$ rbenv install 2.1.0
Downloading ruby-2.1.0.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/9e6386d53f5200a3e7069107405b93f7
Installing ruby-2.1.0...
BUILD FAILED
Inspect or clean up the working tree at /var/folders/zw/8c90w7pn36x6m54v9rw22w_nzj1j43/T/ruby-build.20140218101217.61630
Results logged to /var/folders/zw/8c90w7pn36x6m54v9rw22w_nzj1j43/T/ruby-build.20140218101217.61630.log
Last 10 log lines:
                              io-console 0.4.2
                              json 1.8.1
                              minitest 4.7.5
                              psych 2.0.2
                              rake 10.1.0
                              rdoc 4.1.0
                              test-unit 2.1.0.0
installing rdoc:              /Users/yusuke.kuoka/.rbenv/versions/2.1.0/share/ri/2.1.0/system
installing capi-docs:         /Users/yusuke.kuoka/.rbenv/versions/2.1.0/share/doc/ruby
The Ruby openssl extension was not compiled. Missing the OpenSSL lib?

対応方法

brew install openssl済みでないなら、まずそうする。それから、rbenvがRubyのビルドに使うOpenSSLのパスを上書きする。

 

Mac OS XにはデフォルトでOpenSSLがインストールされているが、それは古い。rbenvはその古いOpenSSLをデフォルトで利用しようとするが、それだと以下のようにOpenSSLの設定に失敗する。

/var/folders/zw/8c90w7pn36x6m54v9rw22w_nzj1j43/T/ruby-build.20140218101217.61630.log
(略)
generating makefile exts.mk
Failed to configure -test-/win32/dln. It will not be installed.
Failed to configure -test-/win32/fd_setsize. It will not be installed.
Failed to configure gdbm. It will not be installed.
Failed to configure openssl. It will not be installed.
(略)

このエラーを回避するためには、以下のようにOpenSSLへのパスをHomebrewでインストールしたもので上書きしてrbenv installを実行する。

$ CONFIGURE_OPTS="--with-openssl-dir=`brew --prefix openssl`" rbenv install 2.1.0

参考:homebrew - OpenSSL error installing ruby 2.0.0-p195 on Mac with rbenv - Stack Overflow