DEV Community

Michael Kohl
Michael Kohl

Posted on • Updated on • Originally published at

Rails quick tips #3: Adding a

We all know the problem: different developers prefer different libraries, especially for their development workflow. However, for various reasons we may not just want to add them unconditionally to our application's Gemfile:

  • Added dependencies (including their own dependencies) for something that doesn't directly relate to the actual app.
  • Bikeshedding discussions about the virtue of gem A over gem B or why we should never use gem C 🙄

For this reason I generally add a entry to my .gitignore, which every developer can customize to their heart's content with a list of gems that fit their workflow but may not be interesting to/wanted by other people on the same team. A trivial example looks like this:

eval_gemfile 'Gemfile'

gem 'awesome_print'
Enter fullscreen mode Exit fullscreen mode

Here eval_gemfile ensures that all gems from the main Gemfile will be included in the local development bundle and then we add awesome_print on top of it.

Now we can go ahead and use either of the following two commands to install our gems and create a

$ bundle install
# or
$ bundle
Enter fullscreen mode Exit fullscreen mode

I generally prefer the form using the BUNDLE_GEMFILE environment variable, since it works for all use cases (e.g. rails c). Of course this is a lot of extra typing, so you can use aliases, direnv or even a gemfile configuration option in ./bundle/config to make the process easier.

Discussion (4)

joshcheek profile image
Josh Cheek

Oh, that's a really great tip!

mikeymurph77 profile image
Michael Murphy

This is a great tip! Thank you!!

katafrakt profile image
Paweł Świątkowski

Thanks for sharing!

First question that pops for me: is it possible to automate it in a way that if there is present, use it, and if not, fall back to default Gemfile?

citizen428 profile image
Michael Kohl Author

The easiest approach would be to make the BUNDLE_GEMFILE env var depend on the presence of There are many ways one could do that depending on environment and work flow, for example a shell function to cd into your Rails project:

function rcd() {
  cd $1
  [[ -f ]] && export

or using direnv or wrapper scripts in bin/, the only limit is pretty much your imagination and shell scripting abilities.