rbenv 導入メモ

Ruby 複数バージョンを切り替えたりするやつ。
さくらのVPS (CentOS release 6.7 (Final)) にて検証。

## rbenv 導入
```
# yum install -y openssl-devel readline-devel zlib-devel
# git clone git://github.com/sstephenson/rbenv.git /usr/local/rbenv
# git clone git://github.com/sstephenson/ruby-build.git /usr/local/rbenv/plugins/ruby-build

# visudo
「Defaults secure_path」の下あたりに以下を追加
----------------------
Defaults !secure_path
Defaults env_keep += "PATH RBENV_ROOT"
---------------------

# vi /etc/bashrc
最下部あたりに以下を追加
---------------------
export RBENV_ROOT=/usr/local/rbenv
export PATH=${RBENV_ROOT}/bin:${PATH}
eval "$(rbenv init -)"
---------------------

# . /etc/bashrc
# rbenv install --list
Available versions:
  1.8.6-p383
  1.8.6-p420
…
  1.9.3-p551
…
  2.0.0-p647
…
  2.2.2
  2.2.3
  2.3.0-dev
…
```

## Ruby 2.2.2 を入れてみる
```
# rbenv install --list | grep 2.2.2
  2.2.2
  rbx-2.2.2

# rbenv install 2.2.2 -v
…
Installed ruby-2.2.2 to /usr/local/rbenv/versions/2.2.2
…

# rbenv global 2.2.2
# rbenv rehash
# rbenv versions
* 2.2.2 (set by /usr/local/rbenv/version)

# ruby --version
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
```

## Ruby 2.0.0 を入れてみる
```
# rbenv install --list | grep 2.0.0
…
  2.0.0-p645
  2.0.0-p647
…

# rbenv install 2.0.0-p647 -v
…
Installed ruby-2.0.0-p647 to /usr/local/rbenv/versions/2.0.0-p647
…

# rbenv versions
  2.0.0-p647
* 2.2.2 (set by /usr/local/rbenv/version)

# rbenv global 2.0.0-p647
# rbenv versions
* 2.0.0-p647 (set by /usr/local/rbenv/version)
  2.2.2

# ruby --version
ruby 2.0.0p647 (2015-08-18 revision 51631) [x86_64-linux]
```

## Ruby 1.9.3 を入れてみる
```
# rbenv install --list | grep 1.9.3
…
  1.9.3-p550
  1.9.3-p551

# rbenv install 1.9.3-p551 -v
…
Installed ruby-1.9.3-p551 to /usr/local/rbenv/versions/1.9.3-p551
…

# rbenv versions
  1.9.3-p551
* 2.0.0-p647 (set by /usr/local/rbenv/version)
  2.2.2

# rbenv global 1.9.3-p551
# rbenv versions
* 1.9.3-p551 (set by /usr/local/rbenv/version)
  2.0.0-p647
  2.2.2

# ruby --version
ruby 1.9.3p551 (2014-11-13 revision 48407) [x86_64-linux]
```


## プロジェクト毎の切替
```
$ ruby --version
ruby 1.9.3p551 (2014-11-13 revision 48407) [x86_64-linux]

$ mkdir project1 && cd project1 && rbenv local 1.9.3-p551 && cd ..
$ mkdir project2 && cd project2 && rbenv local 2.0.0-p647 && cd ..
$ mkdir project3 && cd project3 && rbenv local 2.2.2 && cd ..

$ cd project1 && ruby --version && cd ..
ruby 1.9.3p551 (2014-11-13 revision 48407) [x86_64-linux]

$ cd project2 && ruby --version && cd ..
ruby 2.0.0p647 (2015-08-18 revision 51631) [x86_64-linux]

$ cd project3 && ruby --version && cd ..
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
```


## おまけメモ：gem
```
# rbenv global 2.2.2
# ruby --version
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]

# gem --version
2.4.5

# gem environment
RubyGems Environment:
  - RUBYGEMS VERSION: 2.4.5
  - RUBY VERSION: 2.2.2 (2015-04-13 patchlevel 95) [x86_64-linux]
  - INSTALLATION DIRECTORY: /usr/local/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0
  - RUBY EXECUTABLE: /usr/local/rbenv/versions/2.2.2/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/local/rbenv/versions/2.2.2/bin
  - SPEC CACHE DIRECTORY: /root/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /usr/local/rbenv/versions/2.2.2/etc
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /usr/local/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0
     - /root/.gem/ruby/2.2.0
…
  - SHELL PATH:
     - /usr/local/rbenv/versions/2.2.2/bin
     - /usr/local/rbenv/libexec
     - /usr/local/rbenv/plugins/ruby-build/bin
     - /usr/local/rbenv/shims
     - /usr/local/rbenv/bin
     - /usr/local/sbin
     - /usr/local/bin
…

# gem update
Updating installed gems
…
Gems updated: bigdecimal json minitest power_assert psych test-unit

# gem list
# gem list --remote
# gem install bundler
# bundle -v
Bundler version 1.10.6

# cd path_to_project
# bundle install
```

## その他メモ： rehash について
- [rbenvでrehashがいらなくなった | bgbgbg](http://blog.bgbgbg.net/archives/2596)

rbenv rehash はいらなくなったとかなんとか。


## 雑感
rbenv install xxx は体感で5分くらいかかった気がする。
「-v」オプションを付けないとウンともスンとも出力が進まないのでヤキモキする。