Using Project-Specific Gems Without Bundle Exec
January 29, 2015 •
If you use Ruby a lot like I do, you’ve probably gone through at least a few steps of this cycle:
- Ruby is awesome!
- Now, I just need to figure out how to make it do more
- RubyGems are awesome! They help me make Ruby do more!
- But managing them is a pain
- Bundler is awesome! It manages the Gems and their dependencies!
- But I just need to figure out how to always use the right version when I’m working on a specific project that uses different versions than everything else
-
bundle exec
is awesome! It loads the right versions of everything for the current project! - But it is a lot of typing for every command
- Aliases are awesome! Now I only have to type
be rake
instead ofbundle exec rake
! -
Bundler binstubs are awesome! Now I only have to type
bin/rake
instead ofbe rake
… - Huh … that isn’t any better, as a matter of fact, that’s more typing. And I have to remember to do the extra step of generating the binstubs for every project. Why bother in that case?
And that’s where things stopped for me for a long time … Until now! Ok, maybe I’m getting ahead of myself. Here’s what I figured out recently …
I’ve been slowly refining my dotfiles project using Thoughtbot’s dotfiles as a basis. There is a lot to grok in that collection of useful tools and settings and I’ve been taking my time figuring it out. One thing that I noticed, but didn’t think much of was this little bit of code in the shell profile:
# mkdir .git/safe in the root of repositories you trust
export PATH=".git/safe/../../bin:$PATH"
At least, until recently when I put two and two together. I remembered that I could use Bundler’s binstubs, which, combined with this trick would allow me to type simply rake
instead of bin/rake
or even be rake
. Assuming you already have a project that uses Ruby, RubyGems and Bundler, here’s how:
- Add the above
PATH
modification to your shell profile. Ensure that you execute it after utilities like rbenv or rvm. - From within your project directory:
- Execute
bundle binstub GEM
. For example:bundle binstub rake
- Execute
mkdir .git/safe
- Execute
Now, if you execute which rake
you should see that it points to the newly-generated binstub instead of the globally-installed one. Party on dudes!