Rails 3 Bundler of Love
Saturday, November 6th, 2010
Are you on Rails 3 yet?
If not, you’re in for a treat.
The Rails core team has done a great job with Rails 3. While there are lots of nice improvements, the best of the bunch is bundler. In my opinion, this feature alone is worth the upgrade.
So pay attention here. You’re going to learn how Bundler is going to make your Rails life a lot easier. But first of all…
What is Bundler?
Bundler manages your gems for you. No more “gem install thisorthatgemfile”. Although you can still install gems this way, you won’t want to. That’s because bundler handles this for you, automagically.
First off, inside of a Rails 3 app, you now specify your gems in a “Gemfile”.
See the gemfile under the “doc” folder? Take a look at what’s inside this file:
As you can tell, this is a list of all your app’s gems.
You now manage your gems from this file.
Wanna add a gem? Easy: just add it to the list of gems inside your gemfile. Then jump into your console and issue “bundle install” from your project root. In my case, it would be:
cws-macbook-pro:slowhunch cw$ bundle
Bundler then goes through your gem list and installs the ones you’re missing. Pretty nice, huh? It’s sorta like having your own personal “gem assistant”.
But here’s something even better: Bundler also installs all of your gem’s dependencies. For example, on my current project Slowhunch.com, I used Michael Bleigh’s excellent authentication solution, Omniauth. It has a ton of dependencies (like Nokogiri) and bundler installed all these with ease.
You can also keep your gems fresh and up-to-date. All you do is use “bundle update”. Bundler will then update all your gems to the latest and greatest version.
All of these add up to a seamless gem experience. However, there is one little “gotcha” I encountered when using bundler…
Warning! Watch Out for this Bundler Gotcha
When I first upgraded to Rails 3, combined the old “gem install rspec” syntax with bunder’s new syntax. I wanted to install Rspec, so I issued this command:
bundler install rspec
Whoops. This “reset” my default installation path. As the bundler documentation says, :
“…calls to bundle install will install gems to the directory originally passed to –path. The Bundler runtime will look for gems in that location. You can revert this option by running bundle install –system.
Which means when I issued “bundler install rspec”, bundler created a folder on my drive called “Rspec” and shoved all the gems in there. No good.
Luckily, it’s easy to correct this mistake. All you have to do is “reset” things and clear out bundler’s remembered install path.
bundle install --system
Once you issue that command, everything installs into the correct folder. In my case, the omniauth gem gets installed to:
Which is exactly where I want it. (I wasn’t using RVM at this point). So the moral is: When using bundler, don’t mix your syntax with the old way of updating gems.
Finally, here’s one last thing that might throw you:
Rails 3 Server Doesn’t Start? Sqlite3-ruby or Sqlite Tripping You Up?
If you’re transitioning from Rails 2x and currently using a mysql database, note that in Rails 3, your gemfile contains gem ‘sqlite-ruby’ as a dependency. Which, of course, you don’t need.
To remove that dependency, just comment that line out. Like so:
# gem 'sqlite3-ruby', :require => 'sqlite3'
Once you’ve done that, you’re good to go. Your server should start with the new Rails 3 command…
…and you’re now in Rails 3 nirvana.
In summary, bundler rocks. It’ll save you a ton of time (and frustration) when managing gems. In my opinion, it’s one of the best reasons to make the switch to Rails 3.