MacでRuby on Railsの開発環境を構築する

[Ruby on Rails チュートリアル](http://railstutorial.jp/) を行うにあたって仮想マシン内では無くMac上に環境を構築することにしました。


# 0. 前提

- Mac OS X 10.11
- Homebrew

手元の環境には昔[この辺](https://github.com/rbenv/rbenv#installation)の手順でインストールされた名残があったのでそのクリーンアップをしておきます。

```bash
$ rm -rf ~/.rbenv
```


# 1. rbenv, ruby-buildのインストール

```bash
$ brew update
$ brew install rbenv ruby-build
==> Installing dependencies for rbenv: autoconf, ruby-build
==> Installing rbenv dependency: autoconf
==> Downloading https://homebrew.bintray.com/bottles/autoconf-2.69.el_capitan.bo
Already downloaded: /Users/*****/Library/Caches/Homebrew/autoconf-2.69.el_capitan.bottle.4.tar.gz
==> 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: 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 6 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

$ rbenv init
```

.bashrcに `rbenv init` を追記します。

```bash
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ source ~/.bashrc
```


# 2. rubyのインストール

現時点で一番最新の 2.3.1 をインストールします。
`rbenv install --list` で利用できるバージョンの一覧が出力されます。

```bash
$ rbenv install 2.3.1
 rbenv versions
* system (set by /Users/*****/.rbenv/version)
  2.3.1

$ rbenv local 2.3.1
$ ruby --version
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
```


# 3. bundlerを使ってrailsのインストール

チュートリアルだと ~/.rbenv の下にgemをインストールすることになるのでbundlerを使ってカレントディレクトリにrailsをインストールします。

```bash
$ gem install bundler
Fetching: bundler-1.13.2.gem (100%)
Successfully installed bundler-1.13.2
1 gem installed

$ cat << _EOT_ > Gemfile
source 'https://rubygems.org'
gem 'rails', '4.2.2'
_EOT_

$ bundle install --path vendor/bundle
Fetching gem metadata from https://rubygems.org/..........
Fetching version metadata from https://rubygems.org/..
Fetching dependency metadata from https://rubygems.org/.
Resolving dependencies...
Installing rake 11.3.0
Installing i18n 0.7.0
Using json 1.8.3
Installing minitest 5.9.1
Installing thread_safe 0.3.5
Installing builder 3.2.2
Installing erubis 2.7.0
Installing mini_portile2 2.1.0
Installing rack 1.6.4
Installing mime-types-data 3.2016.0521
Installing arel 6.0.3
Using bundler 1.13.2
Installing concurrent-ruby 1.0.2
Installing thor 0.19.1
Installing tzinfo 1.2.2
Installing nokogiri 1.6.8.1 with native extensions
Installing rack-test 0.6.3
Installing mime-types 3.1
Installing sprockets 3.7.0
Installing activesupport 4.2.2
Installing loofah 2.0.3
Installing mail 2.6.4
Installing rails-deprecated_sanitizer 1.0.3
Installing globalid 0.3.7
Installing activemodel 4.2.2
Installing rails-html-sanitizer 1.0.3
Installing rails-dom-testing 1.0.7
Installing activejob 4.2.2
Installing activerecord 4.2.2
Installing actionview 4.2.2
Installing actionpack 4.2.2
Installing actionmailer 4.2.2
Installing railties 4.2.2
Installing sprockets-rails 3.2.0
Installing rails 4.2.2
Bundle complete! 1 Gemfile dependency, 35 gems now installed.
Bundled gems are installed into ./vendor/bundle.

$ bundle exec rails -v
Rails 4.2.2
```

後は `rails` コマンドを適宜 `bundle exec rails` に読み替えていけば良さそうです。


# x. 参考

- [Ruby on Rails チュートリアル：実例を使って Rails を学ぼう](http://railstutorial.jp/)  
- [Groom your app’s Ruby environment with rbenv.](https://github.com/rbenv/rbenv#installation)