Local Gemfile solution that works
By using bundler-gemlocal
developers can have their own custom Gemfile, called Gemlocal
by default, in addition to team-wide Gemfile
. The solution is easy to use and ensures no issues with Gemfile.lock
.
-
Download
bundler-gemlocal.sh
into your home directory:$ cd $ curl https://raw.githubusercontent.com/dadooda/bundler-gemlocal/master/bundler-gemlocal.sh -O
-
Edit your user's shell startup files to source
bundler-gemlocal.sh
. I recommend.bashrc
. If you don't have one, edit.profile
:$ echo -e "\n. bundler-gemlocal.sh" >> .bashrc
-
Reload your shell (Mac users: open new Terminal window). Then check that
b
function is available:$ type -t b function
b
is a Gemlocal-aware alias tobundle
.bx
is a Gemlocal-aware alias tobundle exec
.
Examples:
$ b install --path vendor/bundle
$ b check
$ bx gem list -l
$ bx rails console
…
-
Cd to your project directory:
$ cd /path/to/project
-
Edit
.gitignore
:/Gemlocal /Gemlocal.lock
-
Edit
config/boot.rb
or the file which initializes Bundler in your project:ENV["BUNDLE_GEMFILE"] ||= File.exists?(fn = File.expand_path("../../Gemlocal", __FILE__)) ? fn : File.expand_path("../../Gemfile", __FILE__)
Ruby 2.0+ users may prefer to use
File.expand_path("../Gemlocal", __dir__)
which looks a little less cryptic. -
Edit
Gemlocal
:# Source the main Gemfile. eval_gemfile File.expand_path("../Gemfile", __FILE__) group :development do # Console enhancements etc. gem "irb_hacks" gem "ori" gem "rdoc" gem "wirb" # … end
-
Install the local bundle for the first time:
$ b install
-
Add
Gemlocal.example
:$ curl https://raw.githubusercontent.com/dadooda/bundler-gemlocal/master/Gemlocal.example -O
-
All done! From now on, keep editing both
Gemfile
andGemlocal
as you like. Then do ab install
and it'll sort everything out for you.
-
Because I like this:
$ ls -1 Gem* Gemfile # o Gemfile.lock # o Gemlocal # - Gemlocal.lock # -
more than this:
$ ls -1 Gem* Gemfile # o Gemfile.local # - Gemfile.local.lock # - Gemfile.lock # o
Legend: (
o
: in the repo,-
: not in the repo). -
Because "Gemlocal" is both a term and a filename, just like "Gemfile". It's more documentation-friendly.
-
Because Internet is full of half-working
Gemfile.local
recipes that will cause you nothing but pain. Find this page by the word "Gemlocal" and stay trouble-free!
- Git should ignore
Gemlocal
andGemlocal.lock
in project root (see Project setup example). - A basic
Gemlocal.example
with comments on how to enable it should be in Git repository. - Team-wide
Gemfile
should contain no "developer comfort" gems (consoles, debuggers, profilers, …) or contain a bare, safe, time-proven, approved minimum set of 1-2 items. Adding more of such gems toGemfile
should be prohibited. - Rails initializers (
config/initializers/*
) for "developer comfort" gems should gracefully handle the case when a specific gem is missing.- A simple yet effective way is to achieve it is via "example files". Say,
subsys.rb
should be ignored by Git, whereassubsys.rb.example
should be in Git repository.
- A simple yet effective way is to achieve it is via "example files". Say,
- It's highly desirable that
Gemlocal.example
lists, in a commented form, all "developer comfort" gems which are known to be used by particular team members. - Project documentation should list Bundler commands in their canonical form:
bundle …
. Documentation sections covering developer tools should mention that the project is ready forbundler-gemlocal
use.
Feedback of any kind is highly appreciated.
— Alex Fortuna, © 2015-2018