MacにRuby on Railsの開発環境を構築（Homebrew／rbenv）

## はじめに
Homebrew経由でMacにRuby on Railsの開発環境を構築する手順。

## Ruby、Ruby on Railsインストール手順

### Homebrewインストール

```bash
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
```

### Homebrewアップデート
```bash
brew update
```

### rbenvインストール

```bash
brew install rbenv ruby-build
```

```shell-session
==> Installing dependencies for rbenv: autoconf, pkg-config, openssl, ruby-build
==> Installing rbenv dependency: autoconf
==> Downloading https://homebrew.bintray.com/bottles/autoconf-2.69.el_capitan.bo
######################################################################## 100.0%
==> Pouring autoconf-2.69.el_capitan.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_1.el_capi
######################################################################## 100.0%
==> Pouring pkg-config-0.29.1_1.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/pkg-config/0.29.1_1: 10 files, 627.3K
==> Installing rbenv dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2h_1.el_capitan
######################################################################## 100.0%
==> Pouring openssl-1.0.2h_1.el_capitan.bottle.tar.gz
==> Using the sandbox
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, 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

==> Summary
🍺  /usr/local/Cellar/openssl/1.0.2h_1: 1,691 files, 12M
==> Installing rbenv dependency: ruby-build
==> Downloading https://github.com/rbenv/ruby-build/archive/v20160602.tar.gz
==> Downloading from https://codeload.github.com/rbenv/ruby-build/tar.gz/v201606
######################################################################## 100.0%
==> ./install.sh
🍺  /usr/local/Cellar/ruby-build/20160602: 282 files, 155.5K, built in 5 seconds
==> Installing rbenv
==> Downloading https://homebrew.bintray.com/bottles/rbenv-1.0.0.el_capitan.bott
######################################################################## 100.0%
==> Pouring rbenv-1.0.0.el_capitan.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, 62.0K
```

### .bash_profileもしくは.zshrcに追記

```bash:.bash_profileもしくは.zshrcに以下を追記
eval "$(rbenv init -)"
```

### Rubyインストール
##### インストール可能なRubyバージョンの確認

```bash
rbenv install -l
```

```shell-session
Available versions:
  1.8.5-p113
　　　　〜中略〜
  2.3.0
  2.3.1
  2.4.0-dev
  jruby-1.5.6
　　　　〜中略〜
  jruby-9.1.2.0
  maglev-1.0.0
  maglev-1.1.0-dev
  maglev-2.0.0-dev
  mruby-dev
  mruby-1.0.0
  mruby-1.1.0
  mruby-1.2.0
  rbx-2.2.2
　　　　〜中略〜
  rbx-3.33
  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インストール

```bash
rbenv install 2.3.1
```

```shell-session
Downloading ruby-2.3.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.bz2
Installing ruby-2.3.1...
Installed ruby-2.3.1 to /Users/cacarrot/.rbenv/versions/2.3.1
```

##### 使用するRubyバージョンを設定

```bash
rbenv global 2.3.1
```

##### Rubyバージョン確認

```bash
ruby -v
```

```shell-session
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
```

### Railsインストール
##### インストール可能なRailsバージョンの確認

```bash:正規表現を使用する場合
gem query -ra -n  "^rails$"
```

```shell-session
rails (5.0.0.1, 5.0.0, 4.2.7.1, 4.2.7, 4.2.6, 4.2.5.2, 4.2.5.1, 4.2.5, 4.2.4, 4.2.3, 4.2.2, 4.2.1, 4.2.0, 4.1.16, 4.1.15, 4.1.14.2, 4.1.14.1, 4.1.14, 4.1.13, 4.1.12, 4.1.11, 4.1.10, 4.1.9, 4.1.8, 4.1.7.1, 4.1.7, 4.1.6, 4.1.5, 4.1.4, 4.1.3, 4.1.2, 4.1.1, 4.1.0, 4.0.13, 4.0.12, 4.0.11.1, 4.0.11, 4.0.10, 4.0.9, 4.0.8, 4.0.7, 4.0.6, 4.0.5, 4.0.4, 4.0.3, 4.0.2, 4.0.1, 4.0.0, 3.2.22.4, 3.2.22.3, 3.2.22.2, 3.2.22.1, 3.2.22, 3.2.21, 3.2.20, 3.2.19, 3.2.18, 3.2.17, 3.2.16, 3.2.15, 3.2.14, 3.2.13, 3.2.12, 3.2.11, 3.2.10, 3.2.9, 3.2.8, 3.2.7, 3.2.6, 3.2.5, 3.2.4, 3.2.3, 3.2.2, 3.2.1, 3.2.0, 3.1.12, 3.1.11, 3.1.10, 3.1.9, 3.1.8, 3.1.7, 3.1.6, 3.1.5, 3.1.4, 3.1.3, 3.1.2, 3.1.1, 3.1.0, 3.0.20, 3.0.19, 3.0.18, 3.0.17, 3.0.16, 3.0.15, 3.0.14, 3.0.13, 3.0.12, 3.0.11, 3.0.10, 3.0.9, 3.0.8, 3.0.7, 3.0.6, 3.0.5, 3.0.4, 3.0.3, 3.0.2, 3.0.1, 3.0.0, 2.3.18, 2.3.17, 2.3.16, 2.3.15, 2.3.14, 2.3.12, 2.3.11, 2.3.10, 2.3.9, 2.3.8, 2.3.7, 2.3.6, 2.3.5, 2.3.4, 2.3.3, 2.3.2, 2.2.3, 2.2.2, 2.1.2, 2.1.1, 2.1.0, 2.0.5, 2.0.4, 2.0.2, 2.0.1, 2.0.0, 1.2.6, 1.2.5, 1.2.4, 1.2.3, 1.2.2, 1.2.1, 1.2.0, 1.1.6, 1.1.5, 1.1.4, 1.1.3, 1.1.2, 1.1.1, 1.1.0, 1.0.0, 0.14.4, 0.14.3, 0.14.2, 0.14.1, 0.13.1, 0.13.0, 0.12.1, 0.12.0, 0.11.1, 0.11.0, 0.10.1, 0.10.0, 0.9.5, 0.9.4.1, 0.9.4, 0.9.3, 0.9.2, 0.9.1, 0.9.0, 0.8.5, 0.8.0)
```

##### Railsインストール

```bash
gem install rails -v 5.0.0.1
```

```shell-session
Fetching: i18n-0.7.0.gem (100%)
Successfully installed i18n-0.7.0
Fetching: thread_safe-0.3.5.gem (100%)
Successfully installed thread_safe-0.3.5
Fetching: tzinfo-1.2.2.gem (100%)
Successfully installed tzinfo-1.2.2
Fetching: concurrent-ruby-1.0.2.gem (100%)
Successfully installed concurrent-ruby-1.0.2
Fetching: activesupport-5.0.0.1.gem (100%)
Successfully installed activesupport-5.0.0.1
Fetching: rack-2.0.1.gem (100%)
Successfully installed rack-2.0.1
Fetching: rack-test-0.6.3.gem (100%)
Successfully installed rack-test-0.6.3
Fetching: mini_portile2-2.1.0.gem (100%)
Successfully installed mini_portile2-2.1.0
Fetching: pkg-config-1.1.7.gem (100%)
Successfully installed pkg-config-1.1.7
Fetching: nokogiri-1.6.8.gem (100%)
Building native extensions.  This could take a while...
Successfully installed nokogiri-1.6.8
Fetching: loofah-2.0.3.gem (100%)
Successfully installed loofah-2.0.3
Fetching: rails-html-sanitizer-1.0.3.gem (100%)
Successfully installed rails-html-sanitizer-1.0.3
Fetching: rails-dom-testing-2.0.1.gem (100%)
Successfully installed rails-dom-testing-2.0.1
Fetching: builder-3.2.2.gem (100%)
Successfully installed builder-3.2.2
Fetching: erubis-2.7.0.gem (100%)
Successfully installed erubis-2.7.0
Fetching: actionview-5.0.0.1.gem (100%)
Successfully installed actionview-5.0.0.1
Fetching: actionpack-5.0.0.1.gem (100%)
Successfully installed actionpack-5.0.0.1
Fetching: activemodel-5.0.0.1.gem (100%)
Successfully installed activemodel-5.0.0.1
Fetching: arel-7.1.1.gem (100%)
Successfully installed arel-7.1.1
Fetching: activerecord-5.0.0.1.gem (100%)
Successfully installed activerecord-5.0.0.1
Fetching: globalid-0.3.7.gem (100%)
Successfully installed globalid-0.3.7
Fetching: activejob-5.0.0.1.gem (100%)
Successfully installed activejob-5.0.0.1
Fetching: mime-types-data-3.2016.0521.gem (100%)
Successfully installed mime-types-data-3.2016.0521
Fetching: mime-types-3.1.gem (100%)
Successfully installed mime-types-3.1
Fetching: mail-2.6.4.gem (100%)
Successfully installed mail-2.6.4
Fetching: actionmailer-5.0.0.1.gem (100%)
Successfully installed actionmailer-5.0.0.1
Fetching: nio4r-1.2.1.gem (100%)
Building native extensions.  This could take a while...
Successfully installed nio4r-1.2.1
Fetching: websocket-extensions-0.1.2.gem (100%)
Successfully installed websocket-extensions-0.1.2
Fetching: websocket-driver-0.6.4.gem (100%)
Building native extensions.  This could take a while...
Successfully installed websocket-driver-0.6.4
Fetching: actioncable-5.0.0.1.gem (100%)
Successfully installed actioncable-5.0.0.1
Fetching: thor-0.19.1.gem (100%)
Successfully installed thor-0.19.1
Fetching: method_source-0.8.2.gem (100%)
Successfully installed method_source-0.8.2
Fetching: railties-5.0.0.1.gem (100%)
Successfully installed railties-5.0.0.1
Fetching: bundler-1.12.5.gem (100%)
Successfully installed bundler-1.12.5
Fetching: sprockets-3.7.0.gem (100%)
Successfully installed sprockets-3.7.0
Fetching: sprockets-rails-3.1.1.gem (100%)
Successfully installed sprockets-rails-3.1.1
Fetching: rails-5.0.0.1.gem (100%)
Successfully installed rails-5.0.0.1
Parsing documentation for i18n-0.7.0
Installing ri documentation for i18n-0.7.0
Parsing documentation for thread_safe-0.3.5
Installing ri documentation for thread_safe-0.3.5
Parsing documentation for tzinfo-1.2.2
Installing ri documentation for tzinfo-1.2.2
Parsing documentation for concurrent-ruby-1.0.2
Installing ri documentation for concurrent-ruby-1.0.2
Parsing documentation for activesupport-5.0.0.1
Installing ri documentation for activesupport-5.0.0.1
Parsing documentation for rack-2.0.1
Installing ri documentation for rack-2.0.1
Parsing documentation for rack-test-0.6.3
Installing ri documentation for rack-test-0.6.3
Parsing documentation for mini_portile2-2.1.0
Installing ri documentation for mini_portile2-2.1.0
Parsing documentation for pkg-config-1.1.7
Installing ri documentation for pkg-config-1.1.7
Parsing documentation for nokogiri-1.6.8
Installing ri documentation for nokogiri-1.6.8
Parsing documentation for loofah-2.0.3
Installing ri documentation for loofah-2.0.3
Parsing documentation for rails-html-sanitizer-1.0.3
Installing ri documentation for rails-html-sanitizer-1.0.3
Parsing documentation for rails-dom-testing-2.0.1
Installing ri documentation for rails-dom-testing-2.0.1
Parsing documentation for builder-3.2.2
Installing ri documentation for builder-3.2.2
Parsing documentation for erubis-2.7.0
Installing ri documentation for erubis-2.7.0
Parsing documentation for actionview-5.0.0.1
Installing ri documentation for actionview-5.0.0.1
Parsing documentation for actionpack-5.0.0.1
Installing ri documentation for actionpack-5.0.0.1
Parsing documentation for activemodel-5.0.0.1
Installing ri documentation for activemodel-5.0.0.1
Parsing documentation for arel-7.1.1
Installing ri documentation for arel-7.1.1
Parsing documentation for activerecord-5.0.0.1
Installing ri documentation for activerecord-5.0.0.1
Parsing documentation for globalid-0.3.7
Installing ri documentation for globalid-0.3.7
Parsing documentation for activejob-5.0.0.1
Installing ri documentation for activejob-5.0.0.1
Parsing documentation for mime-types-data-3.2016.0521
Installing ri documentation for mime-types-data-3.2016.0521
Parsing documentation for mime-types-3.1
Installing ri documentation for mime-types-3.1
Parsing documentation for mail-2.6.4
Installing ri documentation for mail-2.6.4
Parsing documentation for actionmailer-5.0.0.1
Installing ri documentation for actionmailer-5.0.0.1
Parsing documentation for nio4r-1.2.1
Installing ri documentation for nio4r-1.2.1
Parsing documentation for websocket-extensions-0.1.2
Installing ri documentation for websocket-extensions-0.1.2
Parsing documentation for websocket-driver-0.6.4
Installing ri documentation for websocket-driver-0.6.4
Parsing documentation for actioncable-5.0.0.1
Installing ri documentation for actioncable-5.0.0.1
Parsing documentation for thor-0.19.1
Installing ri documentation for thor-0.19.1
Parsing documentation for method_source-0.8.2
Installing ri documentation for method_source-0.8.2
Parsing documentation for railties-5.0.0.1
Installing ri documentation for railties-5.0.0.1
Parsing documentation for bundler-1.12.5
Installing ri documentation for bundler-1.12.5
Parsing documentation for sprockets-3.7.0
Installing ri documentation for sprockets-3.7.0
Parsing documentation for sprockets-rails-3.1.1
Installing ri documentation for sprockets-rails-3.1.1
Parsing documentation for rails-5.0.0.1
Installing ri documentation for rails-5.0.0.1
Done installing documentation for i18n, thread_safe, tzinfo, concurrent-ruby, activesupport, rack, rack-test, mini_portile2, pkg-config, nokogiri, loofah, rails-html-sanitizer, rails-dom-testing, builder, erubis, actionview, actionpack, activemodel, arel, activerecord, globalid, activejob, mime-types-data, mime-types, mail, actionmailer, nio4r, websocket-extensions, websocket-driver, actioncable, thor, method_source, railties, bundler, sprockets, sprockets-rails, rails after 154 seconds
37 gems installed
```

##### Railsバージョン確認

```bash
rails -v
```

```shell-session
Rails 5.0.0.1
```

## 参考
- [GitHub - rbenv - Installation - Homebrew on Mac OS X](https://github.com/rbenv/rbenv#homebrew-on-mac-os-x)
- [rbenv + ruby-build はどうやって動いているのか](http://takatoshiono.hatenablog.com/entry/2015/01/09/012040)