モダンかもしれないRubyの開発環境構築

あらまし

Ruby On Railsを使ってみたいんですが、Rubyのモダンな開発環境よく知らないPerlプログラマがなんとなく行ったRuby開発環境構築の記録です。

現状

とりあえず現状はこんな感じ。

% which ruby                                              
/usr/bin/ruby
% ruby -v                                                 
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
% which irb                                                  
/usr/bin/irb
% irb -v                                                  
irb 0.9.5(05/04/13)
% rake --version  
rake, version 0.8.3
%  which rake                                           
/usr/bin/rake

perlbrewがあるんだからrubybrewもあるんじゃないの?と思ったんです。

モダンなXX開発環境で検索する

なぜかRuby見当たらない。

公式サイト見ればいいのか?

公式サイトhttp://www.ruby-lang.org/ja/downloads/によると【ruby 1.9.2-p180】が安定版とかかいてある

brewだとちょっとバージョンが違うらしい。

% brew info ruby                                             [~/project]
ruby 1.9.2-p136

perlbrewのようにrubybrewがないかな?

その名はRVM(Ruby Version Manager)

なんとなくそれっぽいのを発見。

そしてなんとなく公式サイトらしいのを発見

http://rvm.beginrescueend.com/

よくわからないけどおもむろにインストール。

okamura% bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head 
)
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
108   979  108   979    0     0   1360      0 --:--:-- --:--:-- --:--:--  2862
Cloning into rvm...
remote: Counting objects: 18280, done.
remote: Compressing objects: 100% (4904/4904), done.
remote: Total 18280 (delta 12386), reused 17750 (delta 11902)
Receiving objects: 100% (18280/18280), 3.16 MiB | 281 KiB/s, done.
Resolving deltas: 100% (12386/12386), done.

  RVM:  Shell scripts enabling management of multiple ruby environments.
  RTFM: http://rvm.beginrescueend.com/
  HELP: http://webchat.freenode.net/?channels=rvm (#rvm on irc.freenode.net)

Installing RVM to /Users/okamura/.rvm/
    Correct permissions for base binaries in /Users/okamura/.rvm/bin...
    Copying manpages into place.

  Notes for Darwin ( Mac OS X )
    For Snow Leopard be sure to have Xcode Tools Version 3.2.1 (1613) or later
    You should download the latest Xcode tools from developer.apple.com.
      (This is since the dvd install for Snow Leopard contained bugs).

    If you intend on installing MacRuby you must install LLVM first.
    If you intend on installing JRuby you must install the JDK.
    If you intend on installing IronRuby you must install Mono (version 2.6 or greater is recommended).

    To seamlessly abandon the Apple-installed system ruby (ruby 1.8.7 patchlevel 174 for Snow Leopard):

    rvm install 1.8.7 # installs patch 302: closest supported version
    rvm system ; rvm gemset export system.gems ; rvm 1.8.7 ; rvm gemset import system # migrate your gems
    rvm --default 1.8.7


  You must now complete the install by loading RVM in new shells.
  
  1) Place the folowing line at the end of your shell's loading files
     (.bashrc or .bash_profile for bash and .zshrc for zsh),
     after all PATH/variable settings:

     [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"  # This loads RVM into a shell session.

     You only need to add this line the first time you install rvm.

  2) Ensure that there is no 'return' from inside the ~/.bashrc file,
     otherwise rvm may be prevented from working properly.

  This means that if you see something like:

    '[ -z "$PS1" ] && return'

  then you change this line to:

  if [[ -n "$PS1" ]] ; then

    # ... original content that was below the '&& return' line ...

  fi # <= be sure to close the if at the end of the .bashrc.

  # This is a good place to source rvm v v v
  [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"  # This loads RVM into a shell session.


EOF - This marks the end of the .bashrc file

     Be absolutely *sure* to REMOVE the '&& return'.

     If you wish to DRY up your config you can 'source ~/.bashrc' at the bottom of your .bash_profile.

     Placing all non-interactive (non login) items in the .bashrc,
     including the 'source' line above and any environment settings.

  3) CLOSE THIS SHELL and open a new one in order to use rvm.


Installation of RVM to /Users/okamura/.rvm/ is complete.


okamuuu, 

Thank you very much for using RVM! I sincerely hope that RVM helps to
make your work both easier and more enjoyable.

If you have any questions, issues and/or ideas for improvement please
join#rvm on irc.freenode.net and let me know, note you must register
(http://bit.ly/5mGjlm) and identify (/msg nickserv <nick> <pass>) to
talk, this prevents spambots from ruining our day.

My irc nickname is 'wayneeseguin' and I hang out in #rvm typically

  ~09:00-17:00EDT and again from ~21:00EDT-~23:00EDT

If I do not respond right away, please hang around after asking your
question, I will respond as soon as I am back.  It is best to talk in
#rvm itself as then other users can help out should I be offline.

Be sure to get head often as rvm development happens fast,
you can do this by running 'rvm get head' followed by 'rvm reload'
or opening a new shell

  w&#10687;&#8255;&#10687;t  

    ~ Wayne

というわけで質問ある場合wayneeseguinさんにIRCで聞けば良いらしい。
で、途中で表示されているように、以下のようにしてPATHを通します。
小生の場合はこんな感じです。

% echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"' >> ~/.zshrc_custom
% source ~/.zshrc_custom

確認

% where rvm | tail -n 1          
/Users/okamura/.rvm/bin/rvm
% rvm -v                                                            

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

インストールできるrubyをさがす。いろいろ表示される

% rvm list known 
... 

こんな感じでお目当ての1.9.2を発見

% rvm list known | grep 1.9.2                                [~/project]
[ruby-]1.9.2-preview1
[ruby-]1.9.2-preview3
[ruby-]1.9.2-rc1
[ruby-]1.9.2-rc2
[ruby-]1.9.2[-p180]
[ruby-]1.9.2-head

さっそくinstallしてみる。ちょいと時間かかります。

% rvm install 1.9.2  

rvmにインストールしたrubyを確認する。

% rvm list                                                           [~]

rvm rubies

   ruby-1.9.2-p180 [ x86_64 ]

切り替えてみる

% which ruby                                                        
/usr/bin/ruby
% rvm use 1.9.2                                                      
Using /Users/okamura/.rvm/gems/ruby-1.9.2-p180
% which ruby                                                         
/Users/okamura/.rvm/rubies/ruby-1.9.2-p180/bin/ruby

元に戻す場合はこうする

% rvm system 
% which ruby 
/usr/bin/ruby


続く。と思う。