読者です 読者をやめる 読者になる 読者になる

<Mac> 色んなバージョンのRubyを使い分けるため「rbenv」をインストールする

やりたいこと

バージョンの異なるRubyを、一つの環境で使い分けたい。

インストール方法

Homebrewを使って、rbenvをインストールする。

$ brew install rbenv
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
gmic          jsonlint      mfterm        minimesos     treefrog      ttygif        twarc
==> Updated Formulae
advancecomp              generate-json-schema     librdkafka               runit
ammonite-repl            git-annex                mailutils                rust
apache-spark             goaccess                 micropython              sassc
armor                    grails                   mkdocs                   scala
ats2-postiats            graphite2                mksh                     sdl_image
aws-sdk-cpp              gtk-gnutella             mlt                      shellinabox
awscli                   gtkextra                 msitools                 skinny
bash                     hebcal                   node                     stunnel
bitrise                  heroku                   node-build               sysdig
cake                     homebank                 oniguruma                telegraf
calabash                 imagemagick              open-scene-graph         terraform
citus                    ios-sim                  openssl@1.1              tmux
cmake                    jemalloc                 osmosis                  tomcat
conan                    jenkins                  passenger                typesafe-activator
consul                   jmxtrans                 ponyc                    vim
consul-backinator        jsdoc3                   pre-commit               voldemort
corectl                  kapacitor                pulledpork               wartremover
dpkg                     kotlin                   pyenv                    wdc
emscripten               kubernetes-cli           qbs                      webkitgtk
filebeat                 lbdb                     quantlib                 winetricks
flow                     libcec                   quazip                   youtube-dl
freetds                  libexosip                quilt                    z3
fuseki                   liblastfm                qxmpp
fzf                      libosip                  rclone
gdb                      libphonenumber           rhash
==> Deleted Formulae
bzr-explorer        coin                czmqpp              libechonest         libqglviewer

==> Installing dependencies for rbenv: autoconf, pkg-config, openssl, ruby-build
==> Installing rbenv dependency: autoconf
==> Downloading https://homebrew.bintray.com/bottles/autoconf-2.69.sierra.bottle.4.tar.gz
######################################################################## 100.0%
==> Pouring autoconf-2.69.sierra.bottle.4.tar.gz
==> Caveats
Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/autoconf
==> Summary
🍺  /usr/local/Cellar/autoconf/2.69: 70 files, 3.0M
==> Installing rbenv dependency: pkg-config
==> Downloading https://homebrew.bintray.com/bottles/pkg-config-0.29.1_2.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring pkg-config-0.29.1_2.sierra.bottle.tar.gz
🍺  /usr/local/Cellar/pkg-config/0.29.1_2: 10 files, 627.4K
==> Installing rbenv dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2j.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.2j.sierra.bottle.tar.gz
==> Using the sandbox
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local.

Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries

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
    PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig

==> Summary
🍺  /usr/local/Cellar/openssl/1.0.2j: 1,695 files, 12M
==> Installing rbenv dependency: ruby-build
==> Downloading https://github.com/rbenv/ruby-build/archive/v20160913.tar.gz
==> Downloading from https://codeload.github.com/rbenv/ruby-build/tar.gz/v20160913
######################################################################## 100.0%
==> ./install.sh
🍺  /usr/local/Cellar/ruby-build/20160913: 304 files, 165.1K, built in 10 seconds
==> Installing rbenv
==> Downloading https://homebrew.bintray.com/bottles/rbenv-1.0.0.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring rbenv-1.0.0.sierra.bottle.tar.gz
==> Caveats
Rbenv stores data under ~/.rbenv by default. If you absolutely need to
store everything under Homebrew's prefix, include this in your profile:
  export RBENV_ROOT=/usr/local/var/rbenv

To enable shims and autocompletion, run this and follow the instructions:
  rbenv init
==> Summary
🍺  /usr/local/Cellar/rbenv/1.0.0: 36 files, 62K

色んな記事に、ruby-buildをインストールすること、と書かれていますが、すでにインストールされていて不要でした。

$ brew install ruby-build
Updating Homebrew...
Warning: ruby-build-20160913 already installed

Rubyのインストール先の変更

デフォルトだと ~/.rbenv にインストールされます。ユーザーのホームディレクトリ配下なので特に問題ないのですが(ローカルの共通環境が汚れない)、ここではHomebrewでパッケージを管理するという方針なので、警告(上のCaveats)に従って変更しておきます。
rbenv + ruby-build はどうやって動いているのか - takatoshiono's blog を参考にさせてもらいました。

$ rbenv root
/Users/yamadatakaaki/.rbenv
$ echo 'export RBENV_ROOT=/usr/local/var/rbenv' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ source .bashrc
$ rbenv root
/usr/local/var/rbenv

試しに、Ruby 2.3.1をインストールしてみる

$ rbenv --version # => バージョン確認
rbenv 1.0.0
$ rbenv install --list # => インストール可能なバージョン一覧の表示
(省略)
$ rbenv install 2.3.1 # => rubyのインストール
(省略)
$ rbenv rehash # => rbenv の再読み込み
$ rbenv versions # => インストールあれているすべてのRubyを表示
 * system (set by /usr/local/var/rbenv/version)
  2.3.1
$ rbenv global 2.3.1 # => defaultで使うrubyのバージョン
$ ruby --version
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin16]