Using the new Rails 3.0 Gem bundler with Passenger, Mongrel and Heroku

UPDATE 19th Feb 2010: Bundler is moving pretty fast! For the most up to date information I’d checkout http://github.com/carlhuda/bundler and specifically this gist (http://gist.github.com/302406) to find out the latest!

Recently we were unsure whether we would be deploying a site to our own hosted system or heroku. I love heroku but there are times when it just doesnt suit the project and a bit more fine grained control is nessesary.

In order to use heroku it was suggested that you move over to the new gem bundler that Yahuda has been working on as part of rails 3.0. However it seems there are a couple of different ways to get the bundler running. The default recomended way works great for mongrel and heroku but didn’t play so nice with passenger.

The default is to place all of the following code into config/preinitializer.rb:

require "#{File.dirname(__FILE__)}/../vendor/bundler_gems/environment"

class Rails::Boot
  def run
    load_initializer
    extend_environment
    Rails::Initializer.run(:set_load_path)
  end

  def extend_environment
    Rails::Initializer.class_eval do
      old_load = instance_method(:load_environment)
      define_method(:load_environment) do
        Bundler.require_env RAILS_ENV
        old_load.bind(self).call
      end
    end
  end
end

After a bit of searching around it seems that in some spawn methods this does not work great with passenger.

The solution

Instead use the following:

In your config/preinitializer.rb just include this part

require "#{File.dirname(__FILE__)}/../vendor/bundler_gems/environment"

Then in config/boot.rb place this just before the last Rails.boot! line like so:

# for bundler
class Rails::Boot
  def run
    load_initializer
    extend_environment
    Rails::Initializer.run(:set_load_path)
  end

  def extend_environment
    Rails::Initializer.class_eval do
      old_load = instance_method(:load_environment)
      define_method(:load_environment) do
        Bundler.require_env RAILS_ENV
        old_load.bind(self).call
      end
    end
  end
end

# All that for this:
Rails.boot!

That should allow you to easily boot from either server and works great with heroku.

Thanks to Mathew Todd for giving the solution based upon gem cutters commit here.

Advertisements

Ruby Gems and OSX 10.6 (Snow Leopard)

I recently upgraded to Snow Leopard. I had to fight the urge to install sooner because we had some big projects underway at work and couldn’t afford any downtime.

Due to not having time to do a complete re-install I simply did the upgrade and I must admit I am pretty impressed with how smoothly it went. When I get some more time I’ll do a clean install but I’m happy with things for now.

One of the things I am finding however is that a lot of gems that required native extensions need to be rebuilt. For a number of these things are quite obvious and its just a case of either updating or removing the gem and re-installing.

Autotest however wasn’t quite so obvious. Some gems install dependancies that required rebuilding and its not always 100% obvious like the following:

/Library/Ruby/Gems/1.8/gems/sys-uname-0.8.3/lib/sys/uname.bundle: dlopen(/Library/Ruby/Gems/1.8/gems/sys-uname-0.8.3/lib/sys/uname.bundle, 9): no suitable image found.  Did find: (LoadError)
/Library/Ruby/Gems/1.8/gems/sys-uname-0.8.3/lib/sys/uname.bundle: no matching architecture in universal wrapper - /Library/Ruby/Gems/1.8/gems/sys-uname-0.8.3/lib/sys/uname.bundle
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
from /Library/Ruby/Gems/1.8/gems/autotest-fsevent-0.1.1/lib/autotest/fsevent.rb:3
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `require'
from /Users/stevesmith/.autotest:2
from /Library/Ruby/Gems/1.8/gems/ZenTest-4.1.3/lib/autotest.rb:195:in `load'
from /Library/Ruby/Gems/1.8/gems/ZenTest-4.1.3/lib/autotest.rb:195:in `initialize'
from /Library/Ruby/Gems/1.8/gems/ZenTest-4.1.3/lib/autotest.rb:194:in `each'
from /Library/Ruby/Gems/1.8/gems/ZenTest-4.1.3/lib/autotest.rb:194:in `initialize'
from /Library/Ruby/Gems/1.8/gems/autotest-rails-4.1.0/lib/autotest/rails.rb:7:in `initialize'
from /Library/Ruby/Gems/1.8/gems/ZenTest-4.1.3/lib/autotest.rb:138:in `new'
from /Library/Ruby/Gems/1.8/gems/ZenTest-4.1.3/lib/autotest.rb:138:in `run'
from /Library/Ruby/Gems/1.8/gems/ZenTest-4.1.3/bin/autotest:55
from /usr/bin/autotest:19:in `load'
from /usr/bin/autotest:19

The gem sys-uname makes use of native extensions but is obviously not reinstalled as autotest is its just assumed to already exist. In this case simply doing

sudo gem uninstall sys-uname
sudo gem install sys-uname

will bring it back. I guess this isn’t the most tricky thing to figure out but I thought I’d post it in case it was of use to anyone.