DEV Community

loading...

Rails quick tips #3: Adding a Gemfile.dev

citizen428 profile image Michael Kohl Originally published at citizen428.net Updated on ・1 min read

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 Gemfile.dev 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 Gemfile.dev.lock:

$ bundle install --gemfile=Gemfile.dev
# or
$ BUNDLE_GEMFILE=Gemfile.dev 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. BUNDLE_GEMFILE=Gemfile.dev 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)

pic
Editor guide
Collapse
joshcheek profile image
Josh Cheek

Oh, that's a really great tip!

Collapse
mikeymurph77 profile image
Michael Murphy

This is a great tip! Thank you!!

Collapse
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 Gemfile.dev present, use it, and if not, fall back to default Gemfile?

Collapse
citizen428 profile image
Michael Kohl Author

The easiest approach would be to make the BUNDLE_GEMFILE env var depend on the presence of Gemfile.dev. 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 Gemfile.dev ]] && export BUNDLE_GEMFILE=Gemfile.dev
}

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