Macに入れたrvmをrbenvに乗り換えてみたメモ


ごめんなさい、完全にメモレベルです

## rvmのアンインストール

```
$ rvm seppuku
Are you SURE you wish for rvm to implode?
This will recursively remove /Users/okisanjp/.rvm and other rvm traces?
(anything other than 'yes' will cancel) > yes
Removing rvm-shipped binaries (rvm-prompt, rvm, rvm-sudo rvm-shell and rvm-auto-ruby)
Removing rvm wrappers in /Users/okisanjp/.rvm/bin
Hai! Removing /Users/okisanjp/.rvm
/Users/okisanjp/.rvm has been removed.

Note you may need to manually remove /etc/rvmrc and ~/.rvmrc if they exist still.
Please check all .bashrc .bash_profile .profile and .zshrc for RVM source lines and delete or comment out if this was a Per-User installation.
Also make sure to remove `rvm` group if this was a system installation.
Finally it might help to relogin / restart if you want to have fresh environment (like for installing RVM again).
```

### 環境整理

.bashrc,.bash_profileなどからrvm関連の記述を削除

~./rvm,~/.rvmrc,/etc/rvmrc などが残っている場合は削除

## rbenvのインストール

<a href="https://github.com/sstephenson/rbenv#installation" target="_blank" class="linkurl">sstephenson/rbenv</a>&nbsp;<a href="http://b.hatena.ne.jp/entry/https://github.com/sstephenson/rbenv#installation" target="_blank"><img src="http://b.hatena.ne.jp/entry/image/https://github.com/sstephenson/rbenv#installation" alt="はてなブックマーク - sstephenson/rbenv" title="はてなブックマーク - sstephenson/rbenv"></a><br />
こちらを参考にしつつ。

インストールはhomebrewで行いました。

```
$ brew install rbenv

$ rbenv -v
rbenv 0.4.0
```

### 環境関連

pathとサブコマンド用autocompleteの設定

```
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
```

### 新しいshellを起動して作業継続


```
$ brew install ruby-build rbenv-gemset rbenv-gem-rehash
```

入れといたほうが良さそうなものを入れておく

## ruby環境のセットアップ

### インストール可能なrubyの一覧

```
$ rbenv install -l
Available versions:
  1.8.6-p383
  1.8.6-p420
  1.8.7-p249
・
・
・
```

### 2.0.0をインストール

```
$ rbenv install 2.0.0-p645
Downloading ruby-2.0.0-p645.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/5e9f8effffe97cba5ef0015feec6e1e5f3bacf6ace78cd1cdf72708cd71cf4ab
Installing ruby-2.0.0-p645...
Installed ruby-2.0.0-p645 to /Users/okisanjp/.rbenv/versions/2.0.0-p645
```

### version確認

```
$ rbenv versions
  system
  2.0.0-p645
* 2.1.4 (set by /Users/okisanjp/.rbenv/version)

$ ruby -v
ruby 2.1.4p265 (2014-10-27 revision 48166) [x86_64-darwin14.0]
```

### 2.0.0に切り替えてみる

```
$ rbenv local 2.0.0-p645 

$ ruby -v
ruby 2.0.0p645 (2015-04-13 revision 50299) [x86_64-darwin14.3.0]
```

localを指定すると、カレントディレクトリに .ruby-version が置かれます

```
$ cat .ruby-version 
2.0.0-p645
```

ディレクトリを移動して確認すると

```
$ cd ~

$ ruby -v
ruby 2.1.4p265 (2014-10-27 revision 48166) [x86_64-darwin14.0]
```

local指定したディレクトリの.ruby-versionファイルを削除すれば元に戻ります

```
$ rm .ruby-version 
remove .ruby-version? y
.ruby-version
$ ruby -v
ruby 2.1.4p265 (2014-10-27 revision 48166) [x86_64-darwin14.0]
```

システム全体で有効となるように切り替える場合は

```
$ rbenv global 2.0.0-p645 
```

この場合は、~/.rbenv/version に使用するバージョンが書き込まれます

```
$ cat ~/.rbenv/version
2.0.0-p645
```

### gemのインストール

試しにgemをインストールしてみます

```
$ gem install mechanize
Fetching: net-http-digest_auth-1.4.gem (100%)
Successfully installed net-http-digest_auth-1.4
Fetching: net-http-persistent-2.9.4.gem (100%)
Successfully installed net-http-persistent-2.9.4
Fetching: mime-types-2.6.1.gem (100%)
Successfully installed mime-types-2.6.1
Fetching: unf_ext-0.0.7.1.gem (100%)
Building native extensions.  This could take a while...
Successfully installed unf_ext-0.0.7.1
Fetching: unf-0.1.4.gem (100%)
Successfully installed unf-0.1.4
Fetching: domain_name-0.5.24.gem (100%)
Successfully installed domain_name-0.5.24
Fetching: http-cookie-1.0.2.gem (100%)
Successfully installed http-cookie-1.0.2
Fetching: ntlm-http-0.1.1.gem (100%)
Successfully installed ntlm-http-0.1.1
Fetching: webrobots-0.1.1.gem (100%)
Successfully installed webrobots-0.1.1
Fetching: mechanize-2.7.3.gem (100%)
Successfully installed mechanize-2.7.3
10 gems installed
```

### 確認

```
$ ruby -v
ruby 2.0.0p645 (2015-04-13 revision 50299) [x86_64-darwin14.3.0]

$ find ~/.rbenv -name mechanize
/Users/okisanjp/.rbenv/versions/2.0.0-p645/lib/ruby/gems/2.0.0/gems/mechanize-2.7.3/lib/mechanize
```

2.0.0p645ディレクトリにインストールされました

### version切り替えてみる

```
$ rbenv local 2.1.4
$ ruby -v
ruby 2.1.4p265 (2014-10-27 revision 48166) [x86_64-darwin14.0]
```

### gem install

```
$ gem install mechanize
Fetching: net-http-digest_auth-1.4.gem (100%)
Successfully installed net-http-digest_auth-1.4
Fetching: mime-types-2.6.1.gem (100%)
Successfully installed mime-types-2.6.1
Fetching: unf-0.2.0.beta2.gem (100%)
Building native extensions.  This could take a while...
Successfully installed unf-0.2.0.beta2
Fetching: domain_name-0.5.24.gem (100%)
Successfully installed domain_name-0.5.24
Fetching: http-cookie-1.0.2.gem (100%)
Successfully installed http-cookie-1.0.2
Fetching: ntlm-http-0.1.1.gem (100%)
Successfully installed ntlm-http-0.1.1
Fetching: webrobots-0.1.1.gem (100%)
Successfully installed webrobots-0.1.1
Fetching: mechanize-2.7.3.gem (100%)
Successfully installed mechanize-2.7.3
8 gems installed
```

### 確認

```
$ find ~/.rbenv -name mechanize
/Users/okisanjp/.rbenv/versions/2.0.0-p645/lib/ruby/gems/2.0.0/gems/mechanize-2.7.3/lib/mechanize
/Users/okisanjp/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/mechanize-2.7.3/lib/mechanize
```

2.1.4の方にも入りました

## versionの一時的な切り替え

```
# 今2.1.4
$ ruby -v
ruby 2.1.4p265 (2014-10-27 revision 48166) [x86_64-darwin14.0]

# 一時的に切り替えるには
$ rbenv shell 2.0.0-p645 
$ ruby -v
ruby 2.0.0p645 (2015-04-13 revision 50299) [x86_64-darwin14.3.0]

# unsetで戻せる
$ rbenv shell --unset
$ ruby -v
ruby 2.1.4p265 (2014-10-27 revision 48166) [x86_64-darwin14.0]

# localをunsetすれば.ruby-versionファイルが消える
$ rbenv local --unset
$ ruby -v
ruby 2.0.0p645 (2015-04-13 revision 50299) [x86_64-darwin14.3.0]
```

とりあえず基本的な部分はこんなところかな

今後色々いじりながら理解していこうと思います