When collaborating on a Ruby project, you might run into a situation where you
would like to add gems to the project’s gem bundle without adding it to
Gemfile, forcing the gems onto your collaborators. Still, you want to have
them managed by Bundler.
This is possible to accomplish without any changes to files tracked in the project repository, by leveraging some Git and Bundler configuration directives.
Ignoring our local configuration
First of all, make sure that
Gemfile.local* are ignored by Git.
This can be done on a global or on a repository level.
Set up a global
.gitignore file and add the patterns to
Run the following commands from anywhere.
git config --global core.excludesfile ~/.gitignore echo "/.bundle/" >> ~/.gitignore echo "/Gemfile.local*" >> ~/.gitignore
Add the patterns to the non-tracked local
Run the following commands from the project repository root.
echo "/.bundle/" >> .git/info/exclude echo "/Gemfile.local*" >> .git/info/exclude
Setting up our local configuration
echo 'eval_gemfile "Gemfile"' > Gemfile.local echo 'gem "fuubar"' >> Gemfile.local echo 'gem "fuubar-cucumber"' >> Gemfile.local
Gemfile.local.lock(assuming the project has a
cp Gemfile.lock Gemfile.local.lock
Tell Bundler to use
bundle config --local gemfile Gemfile.local
bundle exec rspec -f Fuubar
One caveat with this setup is that Bundler binstubs won’t
work unless you set the
BUNDLE_GEMFILE environment variable too. This is
because Bundler binstubs only read and conditionally set
export BUNDLE_GEMFILE=$(readlink -f Gemfile.local) bin/rspec -f Fuubar
Another caveat is that updates to
to be synced manually. If someone else updates
Gemfile.lock those changes need
to be copied to
Gemfile.local.lock. Similarly, if you update a gem, you need
to make sure the update is applied to both