異なるバージョンのrails をアプリケーション毎に使う方法

これは、自分のマシンに入っているrails は2.0.1 だけど、特定のアプリケーションは1.0.0 で動かしたいという時の解決方法。

方法1. vendor 以下にrails をfreeze する

この方法は、アプリケーション内にrails のソースを入れてしまう方法。方法は以下を参考


この方法のメリットは、以下のような感じ

  • rails をgem で入れる必要がない
  • gem に依存してないので、プロジェクトのディレクトリをそのままあげてサーバで動かせる
    • もちろん、railsruby とかは必要。バージョンを考えなくていいという話
  • ソースが身近にある


逆にデメリット

  • プロジェクトディレクトリが重くなる
  • いちいちvendor に入れるの面倒

方法2. 複数のrails をgem にインストールする

この方法は、gem 内に複数のrails を入れるというもの。どのバージョンを使うかは「config/environment.rb」内で指定する。例えば、現在は2.0.1 が入っているが、1.0.0 を使いたい場合は以下のようになる。

  • 1. gem をインストール

$ sudo gem install rails -v 1.0.0

  • 2. config/enviroment.rb でgem のバージョンを指定
# Be sure to restart your web server when you modify this file.

# Uncomment below to force Rails into production mode when 
# you don't control web/app server and can't set it the proper way
# ENV['RAILS_ENV'] ||= 'production'

# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '1.0.0' unless defined? RAILS_GEM_VERSION

# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')

Rails::Initializer.run do |config|
...
  • 3. rake rails:update:configs

$ rake rails:update:configs


これで1.0.0 が使えるようになる。例えば、会社のrails のバージョンは1.0.0 に統一されているなど、複数のプロジェクトで同じ古いバージョンのrails を使っているなら、こっちの方法の方がいい。