Friday, March 4, 2011

Bundler with rails-2.2.2

I am working on a rails application, which is on rails-2.2.2. I have used bundler on rails-3 application, but never tried bundler for rails-2.2.2. The benefit of bundler is huge, it is gem manager for rails application. So, now no pain of vendoring/localizing gems in rails application.

gembundler.com gives step-by-step process on how to install/configure bundler for rails-2.3.x and rails-3.x applications. So, I wanted to find out how does it work with rails-2.2.2 application.

So, here is how I did.
  • First of all I have installed bundler
    gem install bundler // installed bundler 1.0.10
  • Updated rubygems
    gem update --system // rubygems > 1.3.6
  • Created config/preinitializer.rb file with below content:
    begin
      require "rubygems"
      require "bundler"
    rescue LoadError
      raise "Could not load the bundler gem. Install it with `gem install bundler`."
    end

    if Gem::Version.new(Bundler::VERSION) <= Gem::Version.new("0.9.24")
      raise RuntimeError, "Your bundler version is too old for Rails 2.3." +
       "Run `gem install bundler` to upgrade."
    end

    begin
      # Set up load paths for all bundled gems
      ENV["BUNDLE_GEMFILE"] = File.expand_path("../../Gemfile", __FILE__)
      Bundler.setup
    rescue Bundler::GemNotFound
      raise RuntimeError, "Bundler couldn't find some gems." +
        "Did you run `bundle install`?"
    end
     
  • Created Gemfile with something similar:
    require 'rubygems'
    source 'http://rubygems.org'
    source 'http://gemcutter.org'
    source 'http://gems.github.com'

    gem 'rails', '2.2.2'
    gem 'hpricot', '0.6.0'
    gem "composite_primary_keys", '1.0.8'
    gem 'javan-whenever', '0.3.7'
    gem 'mysql', '2.8.1'
    gem 'activerecord-oracle_enhanced-adapter', '1.1.9'
    gem 'ruby-oci8', '1.0.3'

    group :test do
      gem 'mocha', '0.9.5'
      gem 'rspec',             '1.3.1', :require => 'spec'
      gem 'rspec-rails',       '1.3.3'
      gem 'database_cleaner',  '0.5.0'
      gem 'capybara'
      gem 'ruby-debug'
      gem 'factory_girl'
    end
  • From the application root, I ran:
    bundle install
That is it and I have my application up and running with bundler. Once I tested my application, I removed gems folder from /vendor (no need for localized version of gems now.)

6 comments:

Alex Rothenberg said...

Gourav,

Thanks for posting these instructions!

One other step you may want to add is putting
require 'bundler/capistrano' in your Capfile so capistrano runs bundler steps on the server whenyou deploy.

--Alex

Niranjan said...

Thanks Gourav for the steps !
I am going to try bundler in my app with rails 2.3.9 version. :-)

Gourav Tiwari said...

Thanks Alex, I will try this.
@Niranjan, I am glad it worked. By the way, I am still waiting for your post on libxml kind of issues in bundler :)

Drew said...

did you have any trouble with your gems not being auto required? I just upgraded from Rails 2.1 and find that I have to manually require them all (it was working fine with bundler before the upgrade)

Gourav Tiwari said...

@Drew, I did not face any challenges in auto requiring gems. I mean for me it was working out-of-the-box. I would be happy to see the challenges you are facing. Which version you have migrated to from rails-2.1 ?

Drew said...

@Gourav, thanks for your response. I talked with my team member today who set up bundler for us. Sounds like he had to monkey with boot.rb to get it to work. That might be causing problems for us with the upgrade.