MavericksでRuby1.9.3環境設定

注意

当然ですが内容は古いです。1.9.3も現在('14/2/24)は1.9.3-p545まで更新されています(そして'15/2/24を以て1.9.3は保守終了)。ですので参考にしてくださるのは結構ですが、鵜呑みにしないでくださいね。

Xcodeインストール

App Storeからインストール。

Command Line Developer Toolsインストール

参考:MavericksでCommand Line Developer Toolsをインストールする方法が変更
従来のCommand Line Tools。次のコマンドでインストール画面が起動する。

$ xcode-select --install

Homebrewインストール

$ ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/man/man1/brew.1

Press ENTER to continue or any other key to abort
==> /usr/bin/sudo /bin/mkdir /usr/local

WARNING: Improper use of the sudo command could lead to data loss
or the deletion of important system files. Please double-check your
typing when using sudo. Type "man sudo" for more information.

To proceed, enter your password, or type Ctrl-C to abort.

Password: ********
==> /usr/bin/sudo /bin/chmod g+rwx /usr/local
==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local
==> Downloading and installing Homebrew...
remote: Finding bitmap roots...
remote: Counting objects: 138880, done.
remote: Compressing objects: 100% (48103/48103), done.
remote: Total 138880 (delta 96392), reused 131992 (delta 89720)
Receiving objects: 100% (138880/138880), 25.50 MiB | 1.12 MiB/s, done.
Resolving deltas: 100% (96392/96392), done.
From https://github.com/mxcl/homebrew
 * [new branch]      master     -> origin/master
HEAD is now at 5c3463f amtterm 1.3
==> Installation successful!
You should run `brew doctor' *before* you install anything.
Now type: brew help
$ brew -v
Homebrew 0.9.5

rbenvインストール

$ brew install rbenv
==> 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 4 seconds

.bashrcの設定は以前に行ったのが(TimeMachineからのバックアップが)残っているので略。
若干変更した。

$ cat .bashrc
 :
# rbenv
#export RBENV_ROOT=/usr/local/var/rbenv
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
#eval "$(rbenv init -)"
PATH=/usr/local/bin:/usr/local/sbin:$PATH
source ~/.rbenv/completions/rbenv.bash
 :

Rubyインストール…

$ rbenv install
rbenv: no such command `install'

あ、ruby-buildが必要だ。

ruby-buildインストール

前置き:いままでにruby-buildをインストールしていた場合

brew upgrade ruby-build」を行って、インストール可能なRubyバージョン情報を更新してください。
(参考:ruby 1.9.3-p448インストール - 別館 子子子子子子(ねこのここねこ)
しかし「Error: ruby-build-yyyymmdd already installed」と出て更新されない場合には「brew install --HEAD ruby-build」してください。

$ brew unlink ruby-build
Unlinking /usr/local/Cellar/ruby-build/20131008... 4 links removed
$ brew install --HEAD ruby-build
==> Cloning https://github.com/sstephenson/ruby-build.git
Cloning into '/Library/Caches/Homebrew/ruby-build--git'...
remote: Counting objects: 127, done.
remote: Compressing objects: 100% (90/90), done.
remote: Total 127 (delta 48), reused 99 (delta 33)
Receiving objects: 100% (127/127), 38.83 KiB | 0 bytes/s, done.
Resolving deltas: 100% (48/48), done.
Checking connectivity... done
==> ./install.sh
🍺  /usr/local/Cellar/ruby-build/HEAD: 97 files, 428K, built in 3 seconds
本編:いままでにruby-buildをインストールしていなかった場合
$ brew install ruby-build
==> Installing dependencies for ruby-build: autoconf, pkg-config, libyaml
==> Installing ruby-build dependency: autoconf
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/autoconf-2.
######################################################################## 100.0%
==> Pouring autoconf-2.69.mavericks.bottle.tar.gz
🍺  /usr/local/Cellar/autoconf/2.69: 69 files, 2.0M
==> Installing ruby-build dependency: pkg-config
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/pkg-config-
######################################################################## 100.0%
==> Pouring pkg-config-0.28.mavericks.bottle.2.tar.gz
🍺  /usr/local/Cellar/pkg-config/0.28: 10 files, 604K
==> Installing ruby-build dependency: libyaml
==> Downloading http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gz
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/libyaml/0.1.4
==> make install
🍺  /usr/local/Cellar/libyaml/0.1.4: 7 files, 332K, built in 14 seconds
==> Installing ruby-build
==> Downloading https://github.com/sstephenson/ruby-build/archive/v20131115.tar.
######################################################################## 100.0%
==> ./install.sh
🍺  /usr/local/Cellar/ruby-build/20131115: 91 files, 404K, built in 2 seconds
$ rbenv install --list
Available versions:
  1.8.6-p383
  1.8.6-p420
  1.8.7-p249
  1.8.7-p302
  1.8.7-p334
  1.8.7-p352
  1.8.7-p357
  1.8.7-p358
  1.8.7-p370
  1.8.7-p371
  1.8.7-p374
  1.9.1-p378
  1.9.1-p430
  1.9.2-p0
  1.9.2-p180
  1.9.2-p290
  1.9.2-p318
  1.9.2-p320
  1.9.3-dev
  1.9.3-p0
  1.9.3-p125
  1.9.3-p194
  1.9.3-p286
  1.9.3-p327
  1.9.3-p362
  1.9.3-p374
  1.9.3-p385
  1.9.3-p392
  1.9.3-p429
  1.9.3-p448
  1.9.3-preview1
  1.9.3-rc1
  2.0.0-dev
  2.0.0-p0
  2.0.0-p195
  2.0.0-p247
  2.0.0-preview1
  2.0.0-preview2
  2.0.0-rc1
  2.0.0-rc2
  2.1.0-dev
  2.1.0-preview1
  jruby-1.5.6
  jruby-1.6.3
  jruby-1.6.4
  jruby-1.6.5
  jruby-1.6.5.1
  jruby-1.6.6
  jruby-1.6.7
  jruby-1.6.7.2
  jruby-1.6.8
  jruby-1.7.0
  jruby-1.7.0-preview1
  jruby-1.7.0-preview2
  jruby-1.7.0-rc1
  jruby-1.7.0-rc2
  jruby-1.7.1
  jruby-1.7.2
  jruby-1.7.3
  jruby-1.7.4
  jruby-1.7.5
  jruby-1.7.6
  jruby-1.7.7
  jruby-1.7.8
  maglev-1.0.0
  maglev-1.1.0-dev
  maglev-2.0.0-dev
  mruby-dev
  rbx-1.2.4
  rbx-2.0.0
  rbx-2.0.0-dev
  rbx-2.0.0-rc1
  rbx-2.1.0
  rbx-2.1.1
  ree-1.8.6-2009.06
  ree-1.8.7-2009.09
  ree-1.8.7-2009.10
  ree-1.8.7-2010.01
  ree-1.8.7-2010.02
  ree-1.8.7-2011.03
  ree-1.8.7-2011.12
  ree-1.8.7-2012.01
  ree-1.8.7-2012.02
  topaz-dev

Ruby 1.9.3インストール(準備含む)

ここ以下でオプション無しにRubyをインストールしていますが、実質的にはオプションが必要です。のでここは無視してくださいm(_ _)m

$ rbenv install 1.9.3-p448 
Downloading ruby-1.9.3-p448.tar.gz...
Installing ruby-1.9.3-p448...
Installed ruby-1.9.3-p448 to /usr/local/var/rbenv/versions/1.9.3-p448

↑ここまで無視してください

Rubyをインストールする前にreadlineとopensslをインストール
$ brew install readline
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/readline-6.
######################################################################## 100.0%
==> Pouring readline-6.2.4.mavericks.bottle.1.tar.gz
==> Caveats
This formula is keg-only, so it was not symlinked into /usr/local.

OS X provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only.

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/readline/lib
    CPPFLAGS: -I/usr/local/opt/readline/include

==> Summary
🍺  /usr/local/Cellar/readline/6.2.4: 31 files, 1.6M

'14/5/1追記:OpenSSLは4/8に脆弱性対策版1.0.1gが出ました。参考:homebrewでOpenSSLの脆弱性対策版を入れたのでRubyも更新させる - 別館 子子子子子子(ねこのここねこ)
('14/6/6追記:OpenSSLは6/6に脆弱性対策版1.0.1hが出ました。参考:homebrewでOpenSSLを1.0.1hへ更新 - 別館 子子子子子子(ねこのここねこ)

$ brew install openssl
==> Downloading http://openssl.org/source/openssl-1.0.1e.tar.gz
######################################################################## 100.0%
==> perl ./Configure --prefix=/usr/local/Cellar/openssl/1.0.1e --openssldir=/usr
==> make depend
==> make
==> make test
==> make install MANDIR=/usr/local/Cellar/openssl/1.0.1e/share/man MANSUFFIX=ssl
==> security find-certificate -a -p /Library/Keychains/System.keychain > '/usr/l
==> security find-certificate -a -p /System/Library/Keychains/SystemRootCertific
==> mv -f /usr/local/etc/openssl/osx_cert.pem.tmp /usr/local/etc/openssl/osx_cer
==> 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

==> Summary
🍺  /usr/local/Cellar/openssl/1.0.1e: 429 files, 15M, built in 3.0 minutes

opensslをMavericks標準からhomebrewのものへ変更。

$ brew link openssl --force
Linking /usr/local/Cellar/openssl/1.0.1e... 1139 symlinks created
$ which openssl
/usr/local/bin/openssl
$ openssl version
OpenSSL 1.0.1e 11 Feb 2013
SSL証明書をインストールMavericksでは不要でした)
$ brew install curl-ca-bundle
==> Downloading https://downloads.sourceforge.net/project/machomebrew/mirror/cur
######################################################################## 100.0%
==> Caveats
To use these certificates with OpenSSL:

  export SSL_CERT_FILE=/usr/local/opt/curl-ca-bundle/share/ca-bundle.crt
==> Summary
🍺  /usr/local/Cellar/curl-ca-bundle/1.87: 2 files, 252K, built in 3 seconds
$ brew list curl-ca-bundle
/usr/local/Cellar/curl-ca-bundle/1.87/share/ca-bundle.crt
$ ln -s /usr/local/Cellar/curl-ca-bundle/1.87/share/ca-bundle.crt /usr/local/etc/openssl/cert.pem
ln: /usr/local/etc/openssl/cert.pem: File exists

おや?証明書ファイルがある?

$ ls -l /usr/local/etc/openssl/
total 672
lrwxr-xr-x  1 riocampos  admin      12 11 22 16:43 cert.pem -> osx_cert.pem
drwxr-xr-x  2 riocampos  admin      68 11 22 16:43 certs
drwxr-xr-x  9 riocampos  admin     306 11 22 16:43 misc
-rw-r--r--  1 riocampos  admin   10835 11 22 16:43 openssl.cnf
-rw-r--r--  1 riocampos  admin  324993 11 22 16:43 osx_cert.pem
drwxr-xr-x  2 riocampos  admin      68 11 22 16:43 private

あ、証明書ファイルが用意されてる。このファイルは全てhomebrewでインストールしたものだから、証明書も組み込むようになったんだ。
さっきのログでも

==> mv -f /usr/local/etc/openssl/osx_cert.pem.tmp /usr/local/etc/openssl/osx_cer

とあるし(後ろは欠けてますが)。
ということでcurl-ca-bundleはuninstallします。

$ brew uninstall curl-ca-bundle
Uninstalling /usr/local/Cellar/curl-ca-bundle/1.87...
追記:curl-ca-bundleは'14/4/24ごろ正式に廃止されました。

https://github.com/jacknagel/homebrew/commit/5384eb3bc023ced4db69d091a85995514dcec3b2

  • chdiza

curl-ca-bundle is needed on Tigerbrew, …

  • mikemcquaid

Even 10.6 is no longer officially supported, incidentally.

Remove curl-ca-bundle by jacknagel · Pull Request #28658 · Homebrew/legacy-homebrew · GitHub

うーむ…まぁAppleも10.6(Snow Leopard)以前はセキュリティアップデートさえ出してないもんなぁ。
そして一番大きい要因は

  • jacknagel

As I explained, it's easy to add custom certs to our openssl installation. This formula is a security liability.

ってことでしょうか。curl-ca-bundleでニセ証明書ファイルを容易に埋め込めるから。
(追記ここまで)

Ruby 1.9.3(readlineとopenssl有効にしたもの)インストール

注意:Ruby 1.9.3-p484 リリースが11/22に出ていますので、必ず1.9.3-p484以上を入れてください。('14/5/1追記:いまは1.9.3-p545です)

$ env CONFIGURE_OPTS="--with-readline-dir=`brew --prefix readline` --with-openssl-dir=`brew --prefix openssl`" rbenv install 1.9.3-p484
Downloading ruby-1.9.3-p484.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/8ac0dee72fe12d75c8b2d0ef5d0c2968
Installing ruby-1.9.3-p484...
Installed ruby-1.9.3-p484 to /Users/riocampos/.rbenv/versions/1.9.3-p484

rbenv-gemsetインストール

参考:rbenvとrbenv-gemsetでgemをよろしく管理する - 細かきこと

$ brew install rbenv-gemset
==> Downloading https://github.com/jf/rbenv-gemset/archive/v0.4.1.tar.gz
######################################################################## 100.0%
🍺  /usr/local/Cellar/rbenv-gemset/0.4.1: 11 files, 48K, built in 2 seconds

'14/5/1追記:
いまは--enable-sharedを追加して

$ env CONFIGURE_OPTS="--enable-shared --with-readline-dir=`brew --prefix readline` --with-openssl-dir=`brew --prefix openssl`" rbenv install -f 1.9.3-p484 

(参考:Mavericksにqtbindingsをインストールする - 別館 子子子子子子(ねこのここねこ)
としています。
(追記ここまで)

rbenv-rehashインストール

参考:rbenv で gem を使った時に rbenv rehash しなくて良くする - はやくプログラムになりたい

$ gem install rbenv-rehash
Fetching: rbenv-rehash-0.3.gem (100%)
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.

あ、1.9.3へ変更し忘れてた。

$ rbenv global 1.9.3-p484 
$ rbenv rehash 
$ rbenv versions
  system
  1.9.3-p392
  1.9.3-p448
* 1.9.3-p484 (set by /Users/riocampos/.rbenv/version)

改めてrbenv-rehashをインストール。

$ gem install rbenv-rehash
Fetching: rbenv-rehash-0.3.gem (100%)
Successfully installed rbenv-rehash-0.3
1 gem installed

bundlerインストール

$ gem install bundler
Fetching: bundler-1.3.5.gem (100%)
Successfully installed bundler-1.3.5
1 gem installed

だいたいここまでで良いかな。