2

I constantly run into a problem where a Gemfile specifies to get the latest version of a gem, but given the constraints of my system I simply get an error because the latest version is not compatible with ruby.

For example let's say I have:

gem "my-gem", ">=1.2"

The latest version is 1.5, but only up to version 1.4 works with my version of Ruby. Instead of producing an error, it would make more sense to install 1.4, since it meets my requirement and works with what I have. (i.e. Bundler should install "the latest version that is satisfied by all dependencies" and produce an error when this version is less than what is specified in the Gemfile)

In fact, the default behavior of bundler effectively imposes ">=1.5" even though that's not what the Gemfile requires.

Is there any way, short of manually changing the Gemfile, that bundler can behave sensibly and just try to provide the latest version of the gem that it has the requirements for?


Update:

I agree that using ~> x.x.x is the smart way to go, but it doesn't do the same thing as what I'm asking about. ~> x.x.x specifically says "install just this version plus patches; do not go to x.x+1". But that is not the same as "give me the latest version that I can satisfy the dependencies for"

Angelo
  • 328
  • 4
  • 14
  • I think Bundler excludes versions that are incompatible with your Ruby version *if* that dependency is described in the gem specification. This is not always the case, though. – tadman Jan 24 '17 at 22:53
  • Why don't you just use `gem "my-gem", "< 1.5"`, when you know that `1.5` doesn't work with your version of Ruby? Btw. is the `spec.required_ruby_version` configured in the gem's gemspec? – spickermann Jan 24 '17 at 23:00

2 Answers2

2

Since version 1.13, bundler does respect the required ruby version of gems.

In order for bundler to resolve the correct versions, you have to specify your desired ruby version in the Gemfile itself. This is required there since the specified ruby version affects the resolved gems in the Gemfile.lock which in itself is independent from the running Ruby.

You can define the desired ruby version like this in your Gemfile:

ruby "~> 2.1.0"

Please refer to bundler's documentation for details and additional options for specifying the ruby version. The feature was announced in the changelog for bundler 1.13.

Holger Just
  • 52,918
  • 14
  • 115
  • 123
  • The problem with ~> is that it doesn't do the same thing as what I'm asking about. ~> specifically says "install just this version plus patches" (which I agree should be how Gemfiles are generally specified). But that is not the same as "give me the latest version that I can satisfy dependencies for" – Angelo Jan 25 '17 at 02:31
  • 1
    Notice the difference between specifying the gem version (which is what the other answer is about) and specifying your desired Ruby version (which is what I'm talking about here). When you specify your Ruby version in the `Gemfile` and your gem specifies its `require_ruby_version` in its gemspec, bundler will automatically find a version of the gem which suits the ruby requirement. Thus, it will probably not attempt to use version 1.5 of your gem. – Holger Just Jan 25 '17 at 11:25
  • Thanks for pointing that out. (Although I haven't tested this yet,) is there a way to specify this on the command line? Ideally I'd like a way to do this without modifying code received from another source. – Angelo Jan 25 '17 at 16:32
1
gem "my-gem", "~>1.4.0"

or

gem "my-gem", '>= 1.4', '< 1.5'

should do.

From the documentation :

The specifier ~> has a special meaning, best shown by example. ~> 2.0.3 is identical to >= 2.0.3 and < 2.1

and this article :

gem('nokogiri', '>= 1.0.0', '< 2.0.0')

Which means that you’ll take any version of Nokogiri starting at 1.0.0 but not 2.0.0 or higher.

Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
  • 2
    `~> 1.4` is identical to `>= 1.4` and `< 2.0`. This doesn't cover the OP's requirement. – spickermann Jan 24 '17 at 23:02
  • The problem with ~> is that it doesn't do the same thing as what I'm asking about. ~> specifically says "install just this version plus patches" (which I agree should be how Gemfiles are generally specified). But that is not the same as "give me the latest version that I can satisfy dependencies for" – Angelo Jan 25 '17 at 02:31