RVM (Ruby Version Manager) で Ruby の複数のバージョンを一つのマシンで使う
最近ハマってる 3分動画でマスターする初心者向けプログラミング学習サイト - ドットインストール で、今度は Ruby on Railsの基礎 - ドットインストール の勉強をスタートしようかと思ったんですが、どうやら今手元にある Mac OS X 10.6 も Ubuntu 10.04 もデフォルトで入ってる Ruby のバージョンが古い(ruby 1.8.7)ようです。
そこで、既存のバージョンも残しつつ複数の Ruby のバージョンを使い分けられないかなと探してみたところ、
RVM: Ruby Version Manager -
RVM Ruby Version Manager - Documentation
がよさそうだったのでインストールしてみました。
以下、手順は Ubuntu 10.04 仕様になってます。インストールするのは Ruby 1.9.2 と Rails 3.2.6 です。
手順
RVM のダウンロードには Git が必要らしい。
$ git --version git version 1.7.6
Git がない場合は、以下のコマンドでインストール。
sudo apt-get install git-core
続いて、RVM をインストール。
$ bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer) $ source ~/.rvm/scripts/rvm
rvm コマンドを使って Ruby 1.9.3 をインストール。list オプションを使うと、利用可能なバージョンが一覧で表示される。
$ rvm list known $ rvm install 1.9.3
ちなみに、このとき gcc コンパイラが必要なので、入ってなければ事前にインストールしておく。
$ sudo apt-get install build-essential
システムのバージョンと切り替える場合は rvm use system で。戻すときは ruby use
$ rvm use system $ rvm use 1.9.3
Ruby 1.9.3 に戻したところで、次にRails 3.2.6 をインストール。まずはインストール可能な Rails のバージョンを gem の query オプションで確認。
$ gem query --remote --all --name-matches "^rails$"
と、ここでエラー。
*** REMOTE GEMS *** /home/tatsuyaoiw/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- zlib (LoadError) from /home/tatsuyaoiw/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require' from /home/tatsuyaoiw/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems.rb:540:in `gunzip' from /home/tatsuyaoiw/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/remote_fetcher.rb:239:in `fetch_path' from /home/tatsuyaoiw/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/spec_fetcher.rb:269:in `load_specs' from /home/tatsuyaoiw/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/spec_fetcher.rb:231:in `block in list' from /home/tatsuyaoiw/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/spec_fetcher.rb:227:in `each' from /home/tatsuyaoiw/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/spec_fetcher.rb:227:in `list' from /home/tatsuyaoiw/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/spec_fetcher.rb:150:in `find_matching_with_errors' from /home/tatsuyaoiw/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/spec_fetcher.rb:177:in `find_matching' from /home/tatsuyaoiw/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/commands/query_command.rb:116:in `execute' from /home/tatsuyaoiw/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/command.rb:278:in `invoke' from /home/tatsuyaoiw/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/command_manager.rb:147:in `process_args' from /home/tatsuyaoiw/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/command_manager.rb:117:in `run' from /home/tatsuyaoiw/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/gem_runner.rb:65:in `run' from /home/tatsuyaoiw/.rvm/rubies/ruby-1.9.3-p194/bin/gem:25:in `<main>'
cannot load such file -- zlib となっているので、zlib のパッケージをダウンロードしないとダメなんだなあと思いつつ調べてみると、必要な依存パッケージの一覧は rvm notes というコマンドで調べられるんだそう。
$ rvm notes Installation Notes: * If you have any questions, issues and/or ideas for improvement please fork the project and issue a pull request. * If you wish to disable the project .rvmrc file functionality, set rvm_project_rvmrc=0 in either /etc/rvmrc or ~/.rvmrc. then log out and back in. * You _must_ read 'rvm requirements' for additional OS specific requirements for various rubies, and native-extension gems. Expect failures until those are met! * For screen users, please do not forget to read https://rvm.io/workflow/screen/. You will need to add 'shell -/bin/bash' to your $HOME/.screenrc to properly load RVM. * rvm_trust_rvmrcs has been changed to rvm_trust_rvmrcs_flag for consistency * Project rvmrc files are now checked for trust whenever they change, as promised by the note displayed during the review process * Ruby package dependency list for your OS is given by: rvm requirements (以下略)
あれ、ないじゃん。一瞬思ったんですが、よくよく見てみると、 Ruby package dependency list for your OS is given by: rvm requirements となってるので、コマンドを叩いてみる。
$ rvm requirements Additional Dependencies: # For Ruby / Ruby HEAD (MRI, Rubinius, & REE), install the following: ruby: /usr/bin/apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion
と、依存関係が見れた。多いな。。
言われるがままにインストールを開始。
$ sudo apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion
終わったら、改めて gem query でバージョンを確認。
$ gem query --remote --all --name-matches "^rails$"
が、また同じエラーが出る。。これは厄介だなあと思ってたら、Ubuntu 11.10 で RVM をインストールした人のエントリを発見。
解決するには、zlibパッケージ自体のインストールと、それを利用しての Ruby の native 拡張の作成が必要とのこと。詳細は元エントリに書いてあるので省いて、ここでは実行したコマンドのみをメモ。
$ cd ~/.rvm/src/ruby-1.9.3-p194/ext/zlib $ ruby extconf.rb checking for deflateReset() in -lz... yes checking for zlib.h... yes checking for kind of operating system... Unix checking for crc32_combine() in zlib.h... yes checking for adler32_combine() in zlib.h... yes creating Makefile $ make compiling zlib.c linking shared-object zlib.so $ make install /usr/bin/install -c -m 0755 zlib.so /home/tatsuyaoiw/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/x86_64-linux installing default zlib libraries
再トライ。
gem query --remote --all --name-matches "^rails$" *** REMOTE GEMS *** rails (3.2.6, 3.2.5, 3.2.4, 3.2.3, 3.2.2, 3.2.1, 3.2.0, 3.1.6, 3.1.5, 3.1.4, 3.1.3, 3.1.2, 3.1.1, 3.1.0, 3.0.15, 3.0.14, 3.0.13, 3.0.12, 3.0.11, 3.0.10, 3.0.9, 3.0.8, 3.0.7, 3.0.6, 3.0.5, 3.0.4, 3.0.3, 3.0.2, 3.0.1, 3.0.0, 2.3.14, 2.3.12, 2.3.11, 2.3.10, 2.3.9, 2.3.8, 2.3.7, 2.3.6, 2.3.5, 2.3.4, 2.3.3, 2.3.2, 2.2.3, 2.2.2, 2.1.2, 2.1.1, 2.1.0, 2.0.5, 2.0.4, 2.0.2, 2.0.1, 2.0.0, 1.2.6, 1.2.5, 1.2.4, 1.2.3, 1.2.2, 1.2.1, 1.2.0, 1.1.6, 1.1.5, 1.1.4, 1.1.3, 1.1.2, 1.1.1, 1.1.0, 1.0.0, 0.14.4, 0.14.3, 0.14.2, 0.14.1, 0.13.1, 0.13.0, 0.12.1, 0.12.0, 0.11.1, 0.11.0, 0.10.1, 0.10.0, 0.9.5, 0.9.4.1, 0.9.4, 0.9.3, 0.9.2, 0.9.1, 0.9.0, 0.8.5, 0.8.0)
お、出た。次に、最新の Rails 3.2.6 をインストール。
$ gem install rails --version 3.2.6
ここ、ちょっと時間がかかる。で、Ruby のバージョンを確認。
$ rails --version Rails 3.2.6
さらに、Ruby 1.9.2 の環境をデフォルトにしたい場合は --default を使用。
rvm use --default 1.9.2
そんなわけで、無事、環境そろいました。
$ ruby -v ruby 1.9.2p320 (2012-04-20 revision 35421) [x86_64-darwin10.8.0] $ gem -v /Users/tatsuyaoiw/.rvm/rubies/ruby-1.9.2-p320/bin/gem:4: warning: Insecure world writable dir /Users/tatsuyaoiw in PATH, mode 040777 1.8.24 $ rails -v Rails 3.2.6 $ sqlite3 SQLite version 3.6.12 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite>