7

We have a Gemfile currently in our git repository. However, there's a gem I use only locally in my environment (my team doesn't use it). In order to use it, I have to add it to our Gemfile, but every time I check out to our master/dev main branch, I have to remove it because of conflicts with the tracked gemfile.

What I would like is something like a Gemfile.local which would inherit the gems imported from the Gemfile but to also allow new gems to be imported there to use on my machine only. This file would be ignored in .gitignore. Is this even possible?

luispcosta
  • 542
  • 1
  • 6
  • 20

2 Answers2

9

Set BUNDLE_GEMFILE environment variable:

BUNDLE_GEMFILE=Gemfile.local bundle exec rails c

To provide a “delta” only in the Gemfile.local put require_relative 'Gemfile' on top of it (or Bundler::Dsl#eval_gemfile as suggested by @MichaelKohl in comments.)

Michael Kohl
  • 66,324
  • 14
  • 138
  • 158
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • The question is about adding a _new_ gem, not redirecting existing one. – Sergio Tulentsev Feb 19 '18 at 10:14
  • Yes, the second one would kinda work. Except that `Gemfile.local` would have to be a full self-sufficient gemfile (cue the problems with syncing it to the main branch). What I imagine Luis asks: is there a way to supply a local, git-ignored __delta__ to the main Gemfile? A patch, if you will. – Sergio Tulentsev Feb 19 '18 at 10:19
  • I'm guessing one could simply `load` the main gemfile from the local one and then supply the delta. – Sergio Tulentsev Feb 19 '18 at 10:25
  • 2
    Or use `Bundler::Dsl#eval_gemfile` for that purpose. – Michael Kohl Feb 19 '18 at 10:27
  • 1
    This worked, thanks. I've used `Bundler::Dsl#eval_gemfile` :) – luispcosta Feb 19 '18 at 10:44
  • When first setting this up, I would recommend copying the existing `Gemfile.lock` to `Gemfile.local.lock`, otherwise the gem versions used to create `Gemfile.local.lock` the first time you run `BUNDLE_GEMFILE=Gemfile.local bundle install` may be very different than the ones in your team's `Gemfile.lock`. – Steve Mar 29 '19 at 17:18
  • I want to clarify what I think @MichaelKohl means by 'use' `Bundler::Dsl#eval_gemfile` is to add a line like: `eval_gemfile 'Gemfile'` within `Gemfile.local`. – AustinC Apr 26 '23 at 20:53
1

Put below code at the top of your Gemfile.local to load existing gemfile:

if File.exists?('Gemfile') then
  eval File.read('Gemfile')
end

It will load all gems from existing Gemfile. You can add new gems also as you need.

Run below commands to install gems from new Gemfile.local:

bundle install --gemfile=Gemfile.local
Prince Bansal
  • 286
  • 2
  • 5