rvmをインストールしてRuby1.8.7とRuby1.9.2を共存させられるようになるまで

asakusa.rbで、1.8と1.9をうまく共存させる方法ありませんかと質問した時に、ひょいひょいっと教えてもらったrvmインストールのメモ。その時持っていたmacbookにはもういれたので別のPC(Ubuntu)で試してます。教えてくださったid:takkan_mさんありがとうございます。


元々gemのインストールをホームディレクトリ以下(~/.gem)にする習慣が無かったので、今回からそれを改めるべく、その辺の設定も一緒にやりました。所要時間:10分くらい。
まずはrvmのインストール。

hibariya@HIBARIYA% gem install rvm

実はこれだけではまだrvmのインストールは終わってないらしい。rvm-installというのを叩く必要がある。インストールが完了した時点で、rvm-installというコマンドが使える人はそのまま実行。使えない場合はPATHが通ってないので、.zshrcとか.bashrcに、

export PATH=$PATH:~/.gem/ruby/1.8/bin/ 

などと追記。sourceで読み直して、rvm-installを実行。

hibariya@HIBARIYA% source ~/.zshrc
hibariya@HIBARIYA% rvm-install 
Installing rvm to /home/hibariya/.rvm/ ...
rvm - shell scripts that allows a user to manage multiple ruby versions in their own account.

<中略>

1) Place the folowing line at the end of your shell's loading files(.bashrc and then .bash_profile for bash and .zshrc for zsh), after all path/variable settings:

     if [[ -s /home/hibariya/.rvm/scripts/rvm ]] ; then source /home/hibariya/.rvm/scripts/rvm ; fi

2) Ensure that there is no 'return' from inside the .bashrc file. (otherwise rvm will be prevented from working properly).
3) CLOSE THIS SHELL and open a new one in order to use rvm.

.zshrc(とか.bashrc)の最後にこの一行を追加してね。という文言が出てくるので、
if -s /home/hibariya/.rvm/scripts/rvm ; then source /home/hibariya
にあたる行を.zshrcの末尾にコピーして貼り付け。sourceで読み直すと使えるようになる。

hibariya@HIBARIYA% source ~/.zshrc
hibariya@HIBARIYA% rvm --version

rvm 0.1.11 by Wayne E. Seguin (wayneeseguin@gmail.com) [http://rvm.beginrescueend.com/]

これでrvm自体のインストールは完了。次はrvmを使って最新のRubyをインストール。

hibariya@HIBARIYA% rvm install head-ruby 
Installing Ruby from source to: /home/hibariya/.rvm/rubies/ruby-head
Updating ruby from http://svn.ruby-lang.org/repos/ruby/trunk
Compiling ruby-head, this may take a while, depending on your cpu(s)...

<中略>

Installation of gems for ruby-head is complete.

自分の環境ではautoconfとbisonがないと言われたのでaptitudeで適当に入れました。結果的には動いてるんですけど、よく見たらRubyインストールの段階で「rvm requires curl, which does not seem to exist in your path :(」とか言われているので後でやり直します。

rvmの使い方はこんな感じです。

hibariya@HIBARIYA% ruby --version
ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux]
hibariya@HIBARIYA% rvm list
   ruby-head [ i386 ]
   system [ ]
hibariya@HIBARIYA% rvm use ruby-head 

Now using ruby head

hibariya@HIBARIYA% ruby --version
ruby 1.9.2dev (2010-02-09 trunk 26623) [i686-linux]

コマンド一つでRubyの環境を切り替えられます。あと、irbとかのバージョンも一緒に切り替わります。

もとに戻すにはresetを使います。何かエラーが出ますが。

hibariya@HIBARIYA% rvm reset
__rvm_reset:6: no matches found: /home/hibariya/.rvm/bin/default_*
__rvm_reset:6: no matches found: /home/hibariya/.rvm/bin/passenger_*
__rvm_reset:6: no matches found: /home/hibariya/.rvm/bin/editor_*
__rvm_reset:18: no matches found: /home/hibariya/.rvm/bin/default*
hibariya@HIBARIYA% ruby --version
ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux]

rvmでインストールできるのはruby-head以外にも色々あるようです。詳しくは、rvm --helpで。

  * ruby    - MRI/YARV Ruby (The Standard), defaults to 1.8.6
  jruby     - JRuby
  rbx       - rubinius
  ree       - ruby Enterprise Edition
  macruby   - MacRuby (Mac OS X Only)
  maglev    - Gemstone Ruby
  ironruby  - IronRuby
  mput      - shyouhei(mput)'s github repository