27

Is the order in which you list your gems important? Are these two blocks equivalent?

gem 'carrierwave'
gem 'rmagick'

And

gem 'rmagick'
gem 'carrierwave'
dee
  • 1,848
  • 1
  • 22
  • 34
  • Well, it's but long winded for a comment, but CarrierWave is not writing the image url to the model correctly. I'm going to delete everything and start again. If that doesn't work, I'll come with more info. – dee May 23 '13 at 22:02
  • Are you using `fog` storage? File storage isn't supposed to store the url, but the file name. The url is generated based on the config. – PinnyM May 23 '13 at 22:06
  • I was using `fog`. In fact, that's when everything broke. But `object.image => nil` anyway, so nothing was being saved. – dee May 23 '13 at 22:14

3 Answers3

15

When you use Bundle.require (which Rails does), Gems are required in the order they appear in the Gemfile. In wasn’t always like this, but has been this way for a while.

Since Carrierwave requires RMagick explicitly when it is needed, I don’t think it should matter in your case; but strictly speaking the two blocks are not equivalent.

matt
  • 78,533
  • 8
  • 163
  • 197
10

Bundler doesn't load gem dependencies by the order that you list them*, but it does go by source priority using this criteria:

  1. Explicit path or git options append to a gem dependency, e.g.:

    gem 'some-gem', github: 'somebody/some-gem'
    
  2. Explicitly defined dependencies for gems that are otherwise required implicitly from other gem dependecies, i.e., gem 'actionmailer' gem is implicitly required by gem 'rails'

  3. If you have multiple sources added it will search from last to first.

See https://bundler.io/man/gemfile.5.html#SOURCE-PRIORITY


Edit: As per Matt's answer, depending on what you're trying to do (or what gems you're loading) the order MIGHT matter. See Even with bundler your gem order can be significant.

Michał Zalewski
  • 3,123
  • 2
  • 24
  • 37
Noz
  • 6,216
  • 3
  • 47
  • 82
  • You're talking about the order of the sources checked for a gem, but I believe the question was about whether the order of the gem statements in a Gemfile matters -- and I believe the answer is yes. – odigity Oct 07 '15 at 20:17
1

Hello from 2022 using Bundler 2.3.11:

Ordering in the Gemfile does not matter as far as I know. The docs for Bundler do not appear to say this definitively, but see below.

Real life example:

Recently we installed Sidekiq Enterprise, and the sidekiq-ent gem has a dependency on sidekiq-pro and both have a dependency on sidekiq.

from our Gemfile.lock:

  specs:
    sidekiq-ent (2.5.0)
      einhorn (>= 0.7.4)
      sidekiq (>= 6.5.0)
      sidekiq-pro (>= 5.5.0)
    sidekiq-pro (5.5.0)
      sidekiq (>= 6.5.0)

Rubocop was yelling at us for using sidekiq-pro before sidekiq-ent:

Bundler/OrderedGems: Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem sidekiq-ent should appear before sidekiq-pro.

So we tried it Rubocop's way, and no issues ¯_(ツ)_/¯

Smitty
  • 1,765
  • 15
  • 22