There are a lot of ways to launch a Ruby or Rails console:
bundle exec irb,
bundle console, and
rails console are some of the most common. They seem similar, but they each work a little bit differently.
If you don’t know what those differences are, you’ll have some problems. Maybe you won’t be able to get your ActiveRecord models connected to the database. Or you’ll require a file and get the wrong version. Or a library you thought would be avaliable, wasn’t.
How do you make sure you’re using the right console at the right time?
Bundler vs non-Bundler
irb is just a plain Ruby console. It doesn’t care about your
Gemfile. It doesn’t load anything but the core Ruby libraries. Anything else you want, you have to
If you install a gem using
gem install, you can require it inside
irb. If you used
bundle install, you might be able to require it, depending on where Bundler put it. (Bundler will sometimes put gems outside of Ruby’s gem path, if you run something like
bundle install --path or
bundle install --deployment).
irb ignores your
Gemfile, the versions inside your
Gemfile.lock don’t matter.
irb will load the newest version of a gem it can find:
1 2 3 4 5 6 7 8 9 10 11 12 13
This can cause really weird problems with your code, if you’re not expecting it.
irb is great if you’re messing with core Ruby files. It’s fast, and doesn’t need any setup.
But if you want to use your
Gemfile when you run a console, run
bundle exec irb instead. The
bundle exec allows
irb to load the gems that Bundler knows about, and only the gems Bundler knows about:
1 2 3 4 5
We got exactly the Rails version we were looking for.
bundler/setup vs Bundler.require
When would you run
bundle console instead of
bundle exec irb?
bundle exec irb sets things up so you can only require the gems in your
bundle console goes one step further. When you run
bundle console, you don’t even need to require the gems in your
Gemfile. They’re already required for you:
1 2 3 4 5 6 7 8 9
You could also get this behavior if you called
Bundler.require inside your
bundle exec irb console. Any gem in your
Gemfile that isn’t marked
require: false will be automatically required, and you’ll be able to use it without any extra work. When you’re working on projects with a
Gemfile, that’s incredibly convenient.
There’s still one difference to think about:
bundle console and
1 2 3 4 5 6 7 8
bundle console just requires a bunch of gems.
rails console requires those gems, but it will also load your entire Rails environment, set up autoloading, initialize your application, and give you a full Rails environment to play around in.
You can get something like the Rails console from
bundle console if you require
1 2 3 4 5 6 7
Each one, just a little more complicated
So, to recap:
irbis the basic Ruby console. It ignores your
Gemfile, and only core Ruby classes are accessible without
require-ing them. It can’t easily load gems that Bundler installs outside of RubyGems’ load path.
bundle exec irbis like
irb, if you also required
bundler/setup. You can only easily require gems that are in your
Gemfile.lock, but you can load those gems no matter where Bundler put them.
bundle consoleis like
bundle exec irb, if you also called
Bundler.require. All of the gems in your
Gemfile, except the ones marked
require: false, can be used without requiring them. It’s really convenient when you’re writing your own gems, or working on non-Rails code.
rails consoleis like running
bundle consoleinside a Rails app, if you also required
config/environment.rb. You can play with your entire Rails app, autoloads and database connections work, and everything’s hooked up the way you’d expect. If you’re working in a Rails app, this is the most helpful kind of console.
There aren’t too many differences between these consoles. And while most of those differences won’t be too big (Oh, this file isn’t required? Better require it!), others will be totally infuriating if you don’t know what’s going on. (WHY IS THIS LOADING THE WRONG VERSION OF RAKE AGAIN!?)
But if you know the idea behind each of these consoles, you’ll be able to use the right kind of console at the right time. And all the libraries you need will be close by when you need them.