How to tell Bundler to use gems installed systemwi

2019-04-02 12:54发布

问题:

When deploying a Rails app with Capistrano, I want Bundler to install gems to shared/bundles dir ONLY IF it can't find gems installed systemwide already. How do I do it?

For example, if I have a pg gem v 0.14 already installed on the system, I want Bundler to use it and not build and install a new one into shared/bundles dir of my application.

I'm using Ubuntu Server and RVM is installed for multiple users.

回答1:

Bundler allows using shared(rubygems) and vendored(bundler) gems, by default bundler/capistrano is configured with:

set :bundle_flags, "--deployment --quiet"

which forces vendored gems only,

you can switch back to shared gems with explicit:

set :bundle_flags, "--system --quiet"

to still install in vendor but use shared gems too:

set :bundle_flags, "--path #{shared_path}/bundles --quiet"

if the deployment was already ran with --deployment(the default) then it could help to run the deploy once with:

set :bundle_flags, "--no-deployment"


回答2:

This is actually a feature of Bundler, essentially it's dependency isolation – you don't want your app depending on an outside, system gems like this. Updating the system gem could then have a impact on running applications.

If you're already using capistrano you should be using require "bundler/capistrano" in your script which will install them in shared/bundles anyway. You could look into packaging before hand as well: http://gembundler.com/v1.2/bundle_package.html