Ruby Bundler multiple sources in Gemfile Ruby Bundler multiple sources in Gemfile ruby ruby

Ruby Bundler multiple sources in Gemfile


Bundler 1.7 has a new feature that allows you to select the source for specific gems by nesting them in a block:

source "https://rubygems.org"gem 'gemfromrubygems1' gem 'gemfromrubygems2'source "http://our.own.gem.repo.com/the/path/to/it" do  gem 'gemfromourrepo'end

or specifying it as an option:

source "https://rubygems.org"gem 'gemfromrubygems1' gem 'gemfromrubygems2'gem 'gemfromourrepo', source: "http://our.own.gem.repo.com/the/path/to/it"

See http://bundler.io/v1.7/gemfile.html for details.


According to the Source Priority section in the Gemfile manpage sources are searched from last entered to first entered.

Based on what you said, it sounds like you want to always prefer your gem over rubygems.org. As long as you don't need to vary your preference (ie. some dups from rubygems.org and some dups from your private repo) then your problem is solved simply with the following Gemfile:

source 'https://rubygems.org'source 'http://our.own.gem.repo.com/the/path/to/it'gem 'gemfromrubygems1'  gem 'gemfromrubygems2'gem 'gemfromourrepo'


The only way I found seems like a horrible hack.

Bundler will search for the best version of your gem starting at the source listed last and then searching all the sources listed previously. It doesn't matter where the source lines are relative to the gem lines, only relative to each other.

I tried to make it work using :git and :path, but neither of those work for gemservers. That leaves matching the best version.

If you set the version of your gem to something like 2.mine.1 and push that to your server, you can constrain the version in your Gemfile.

source :rubygemssource 'http://myrepo'    gem 'gemfromourrepo', '~> 2.ourrepo'

Then the best matching version should be one from your server. There's a chance someone could push their own gem of the same name with 2.ourrepo.2 to rubygems, but that is unlikely if it is unique.