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'
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
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)
Oh, that's a really great tip!
This is a great tip! Thank you!!
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?
The easiest approach would be to make the
BUNDLE_GEMFILE
env var depend on the presence ofGemfile.dev
. There are many ways one could do that depending on environment and work flow, for example a shell function tocd
into your Rails project:or using
direnv
or wrapper scripts inbin/
, the only limit is pretty much your imagination and shell scripting abilities.