その他
    ホーム 技術発信 DoRuby RVMでRailsのバージョンを管理する

    RVMでRailsのバージョンを管理する

    この記事はアピリッツの技術ブログ「DoRuby」から移行した記事です。情報が古い可能性がありますのでご注意ください。

    こんにちは、sasimiです。

    今回は、RubyのバージョンもRailsのバージョンも管理するのに便利なRVMを楽しみたいと思います。

    Rails3とRails2.3.8をインストールし、管理するにはどうすればいいのでしょうか。

    そこで、RVMです。

    ハチネンさんのエントリー RVMで複数のRubyを管理 を読んで、Rails3をインストールしてみました。

    環境はCentOS5.4です。

    今回、以下のことをやっています。

    ■ RVMのインストール

    ■ Rubyのインストール

    ■ gemsetの作成

    ■ Rails3、Rails2のインストール

     RVMのインストール

    [tomonori@localhost]~% cd
    # RVMのダウンロード
    # gitをインストールしている方
    [tomonori@localhost]~% wget -O rvm-install \
    [tomonori@localhost]~% http://rvm.beginrescueend.com/releases/rvm-install-head
    # gitをインストールしていない方
    [tomonori@localhost]~% wget -O rvm-install \
    [tomonori@localhost]~% http://rvm.beginrescueend.com/releases/rvm-install-latest
    
    # ダウンロードしたファイルの実行
    [tomonori@localhost]~% sh rvm-install
    
    #rvm
    [tomonori@localhost]~% source "$HOME/.rvm/scripts/rvm"
    

    rvmのパスが通ったことを確認します

    [tomonori@localhost]~% rvm notes
    
    Notes for Linux ( CentOS release 5.5 (Final) )
    
        *  curl is required.
    
        *  patch is required (for ree, some ruby head's).
    
        *  If you wish to install rbx and/or any MRI head (eg. 1.9.2-head) then you must install and use rvm 1.8.7 first.
    
        *  For MRI/ree (if you wish to use it) you will need:
           $ yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel
           $ yum install -y iconv-devel # NOTE: For centos 5.4 final iconv-devel might not be available :(
        *  For JRuby (if you wish to use it) you will need:
           $ yum install -y java
    

    RVMのインストールはここまで。

    今回、CentOS5.4にインストールしました。

    ここで表示されている通り、readlineとzlibをインストールしました。

    RVMがログインする度に起動するように.zshrc(bashの方は.bashrc)に追加します。

    # .zshrcにrvmを読み込むように追加
    [tomonori@localhost]~% echo 'if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then "' >> ~/.zshrc
    [tomonori@localhost]~% echo '  source "$HOME/.rvm/scripts/rvm" ; ' >> ~/.zshrc
    [tomonori@localhost]~% echo 'fi'  >> ~/.zshrc
    

    ここまでの詳細は

    http://rvm.beginrescueend.com/rvm/install/

    を参照してください。

     Rubyのインストール

    さて、rubyをインストールします。

    % rvm install 1.8.7 -C "--enable-shared, --with-readline-dir=/usr/local"
    % rvm install 1.9.2 -C "--enable-shared, --with-readline-dir=/usr/local"
    
    [tomonori@localhost ~]% ruby -v      
    ruby 1.9.2dev (2010-05-31 revision 28117) [x86_64-linux]
    

    RVMによって複数のRubyを管理するのはハチネンさんのエントリー RVMで複数のRubyを管理 を読んでください。

     gemsetの作成

    ここからはgemsetを利用して、Railsのバージョンを切り替えたいと思います。

    現在のgemsetのリストを確認します

    [tomonori@localhost]~% rvm gemset list
    
    info: gemsets : for ruby-1.9.2-preview3 (found in /home/tomonori/.rvm/gems/)
    global
    

    新しくgemsetを作成します。ここではrails3という名前のgemsetを作成します。

    [tomonori@localhost]~% rvm gemset create rails3
    
    info: Gemset 'rails3' created.
    

    同様に、rails2という名前のgemsetを作成します。

    [tomonori@localhost]~% rvm gemset create rails2
    
    info: Gemset 'rails2' created.
    

     Rails3、Rails2のインストール

    では、rails3というgemsetを使って、gemのインストールをします。

    このgemsetにRails3をインストールします。

    [tomonori@localhost]~% rvm gemset use rails3
    info: Now using gemset 'rails3'
    
    [tomonori@localhost ~]% gem install tzinfo builder memcache-client rack \
    [tomonori@localhost ~]% rack-test rack-mount erubis mail text-format thor \
    [tomonori@localhost ~]% bundler i18n
    [tomonori@localhost]~% gem install rails --pre 
    

    同様に、gemset rails2にRailsの最新版である2.3.8をインストールします。

    [tomonori@localhost]~% rvm gemset use rails2
    
    info: Now using gemset 'rails2'
    
    [tomonori@localhost]~% gem install rails
    

    では、確認します。

    ruby-1.9.2-preview3とgemset rails3の組み合わせで正しくインストールされているかを確認します。

    おなじみのgem listで見てみると、

    [tomonori@localhost]~% rvm use ruby-1.9.2-preview3@rails3
    
    info: Using ruby 1.9.2 preview3 with gemset rails3
    [tomonori@localhost]~% gem list
    
    *** LOCAL GEMS ***
    
    abstract (1.0.0)
    actionmailer (3.0.0.beta4)
    actionpack (3.0.0.beta4)
    activemodel (3.0.0.beta4)
    activerecord (3.0.0.beta4)
    activeresource (3.0.0.beta4)
    activesupport (3.0.0.beta4, 2.3.8)
    arel (0.4.0)
    builder (2.1.2)
    bundler (0.9.26)
    erubis (2.6.5)
    i18n (0.4.1)
    mail (2.2.4)
    memcache-client (1.8.3)
    mime-types (1.16)
    polyglot (0.3.1)
    rack (1.2.0, 1.1.0)
    rack-mount (0.6.4)
    rack-test (0.5.4)
    rails (3.0.0.beta4)
    railties (3.0.0.beta4)
    rake (0.8.7)
    rdoc (2.5.8)
    text-format (1.0.0)
    text-hyphen (1.0.0)
    thor (0.13.6)
    treetop (1.4.8)
    tzinfo (0.3.22)
    

    正しくインストールされているようです。

    同様に、gemset rails2ではどうでしょうか?

    [tomonori@localhost]~% rvm use ruby-1.9.2-preview3@rails2
    
    info: Using ruby 1.9.2 preview3 with gemset rails2
    [tomonori@localhost]~% gem list
    
    *** LOCAL GEMS ***
    
    actionmailer (2.3.8)
    actionpack (2.3.8)
    activerecord (2.3.8)
    activeresource (2.3.8)
    activesupport (2.3.8)
    rack (1.1.0)
    rails (2.3.8)
    rake (0.8.7)
    rdoc (2.5.8)
    

    インストールされていることが確認できましたね。

    しかも、gemsetを切り替えることで、Railsのバージョンの管理ができました。いや、まあ、gemを管理してるだけやん、っていわないで。。。

    最後に

    [tomonori@localhost]~% rvm use ruby-1.9.2-preview3@rails3 --default
    

    これで、次回からログイン時にもこのruby-1.9.2-preview3とgemset rails3のときのものが使われますよ。

    gemsetの名前はなんでもいいので、プロジェクト毎に工夫することができます。

    どうです?積極的にRVMを利用したいと思いませんか?