MacにおけるRails開発環境におけるreadlineライブラリのバージョン不整合問題への対処


# はじめに
MacでRaisの開発環境を構築する際、よく出くわすreadlineライブラリのバージョン不整合問題を整理する。

## 環境
* OS
  * MacOS Sierra (10.12.6)
* brew
  * Homebrew 1.5.3
  * Homebrew/homebrew-core (git revision 914a; last commit 2018-02-07)

## readline
### エラー出力
6系がない場合

```
% ruby -v
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin13]

% gem list | grep pry
pry (0.10.3)
pry-byebug (3.3.0)
pry-rails (0.3.4)

% pry
…
    Sorry, you can't use byebug without Readline. To solve this, you need to
    rebuild Ruby with Readline support. If using Ubuntu, try `sudo apt-get
    install libreadline-dev` and then reinstall your Ruby.
/Users/shungo/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': dlopen(/Users/shungo/.rbenv/versions/2.2.2/lib/ruby/2.2.0/x86_64-darwin13/readline.bundle, 9): Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib (LoadError)
…
```

7系がない場合

```
% ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin15]

% gem list | grep pry
pry (0.10.4)
pry-byebug (3.5.0)
pry-doc (0.11.1)
pry-rails (0.3.6)
pry-stack_explorer (0.4.9.2)

% pry
…
    Sorry, you can't use byebug without Readline. To solve this, you need to
    rebuild Ruby with Readline support. If using Ubuntu, try `sudo apt-get
    install libreadline-dev` and then reinstall your Ruby.
/Users/shungo/.rbenv/versions/2.4.1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require': dlopen(/Users/shungo/.rbenv/versions/2.4.1/lib/ruby/2.4.0/x86_64-darwin15/readline.bundle, 9): Library not loaded: /usr/local/opt/readline/lib/libreadline.7.dylib (LoadError)
  Referenced from: /Users/shungo/.rbenv/versions/2.4.1/lib/ruby/2.4.0/x86_64-darwin15/readline.bundle
  Reason: image not found - /Users/shungo/.rbenv/versions/2.4.1/lib/ruby/2.4.0/x86_64-darwin15/readline.bundle
…
```

### 対処1: brewでバージョン切り替えを行う
6系が欲しい場合

```
% brew install -f https://raw.githubusercontent.com/esse/homebrew-readline6.3.8/master/Formula/readline.rb
```

7系が欲しい場合: デフォルト

```
% brew install readline
```

2つのバージョンがインストールされた状態

```
% brew info readline
readline: stable 7.0.3 (bottled) [keg-only]
Library for command-line editing
https://tiswww.case.edu/php/chet/readline/rltop.html
/usr/local/Cellar/readline/6.3.8 (46 files, 2.0MB)
  Poured from bottle on 2018-02-07 at 18:00:55
/usr/local/Cellar/readline/7.0.3_1 (47 files, 1.5MB)
  Poured from bottle on 2017-07-14 at 10:23:26
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/readline.rb
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local,
because macOS 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..

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/readline/lib
    CPPFLAGS: -I/usr/local/opt/readline/include
```

切り替え

```
% brew switch readline 6.3.8
Cleaning /usr/local/Cellar/readline/6.3.8
Cleaning /usr/local/Cellar/readline/7.0.3_1
Opt link created for /usr/local/Cellar/readline/6.3.8

% brew switch readline 7.0.3_1
Cleaning /usr/local/Cellar/readline/6.3.8
Cleaning /usr/local/Cellar/readline/7.0.3_1
Opt link created for /usr/local/Cellar/readline/7.0.3_1
```


### 対処2: 7系のライブラリを6系としてシンボリックリンクを貼る
以下の記事を参考にシンボリックリンクを貼る。
事前に7系のライブラリをbrewでインストールしている前提。

https://stackoverflow.com/questions/40671803/library-not-loaded-usr-local-opt-readline-lib-libreadline-6-dylib-loaderror

```
% ln -s /usr/local/opt/readline/lib/libreadline.7.0.dylib /usr/local/opt/readline/lib/libreadline.6.dylib
% ls -l /usr/local/opt/readline/lib/libreadline.7.0.dylib /usr/local/opt/readline/lib/libreadline.6.dylib
lrwxr-xr-x  1 shungo  admin      49  2  8 12:53 /usr/local/opt/readline/lib/libreadline.6.dylib -> /usr/local/opt/readline/lib/libreadline.7.0.dylib
-r--r--r--  1 shungo  admin  233828  7 14  2017 /usr/local/opt/readline/lib/libreadline.7.0.dylib
```

### 対処3: rubyが参照するreadlineのパスをインストール時に指定する
いちいちrubyインストール時に指定するのは面倒なので未検証

https://railstutorial.jp/chapters/beginning?version=3.2

### 対処4: railsのサブコマンドのみを利用する条件付きでrb-readlineのgemをインストールする
rails consoleを利用するなどしてpryなどコマンドを直接利用しない。pryを使わない前提ならこちらで十分。

```
% vim Gemfile
…
gem 'rb-readline'
…

% bundle install

% rails console
```