3

I want to have a specific version of RubyGems for a project. I have tests that fail for 2.5.x but pass for 2.6.x, so I want to make sure users are using 2.6.x or higher. I know you can explicitly update the version of RubyGems, but can you somehow 'require' a specific version like you can for regular gems?

Community
  • 1
  • 1
  • Require in what context? – tadman May 27 '16 at 18:08
  • @tadman Somehow alert a developer trying to run the tests and develop code that their version of RubyGems itself is below a required version. Running `bundle install` will install the right gems but not say anything about RubyGems. The problem arose because `ruby-2.3.1` installs `rubygems-2.5.x` (at the moment) but I had issues which were only solved by using `rubygems-2.6.x`. I suppose the answer could be 'add it to the documentation', but since everything else can be checked I hoped this could be as well. – Ferdinand van Wyk May 27 '16 at 18:20
  • Sounds like an "Add to documentation" step. – tadman May 27 '16 at 18:39

2 Answers2

3

I don't believe it's possible to select from among multiple rubygems versions in a single Ruby installation. But you can ensure that the required version is being used. Put the following somewhere where it will be run early in your application's startup, such as the top of your Gemfile (which is just a Ruby file), or at the beginning of a script which doesn't use bundler:

required_rubygems_version = '2.6.0'
if Gem.rubygems_version < Gem::Version.create(required_rubygems_version)
  raise "Please upgrade to rubygems #{required_rubygems_version}"
end
Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
2

You can add a startup check in your config/initializers that will prevent Rails from starting the application if the Ruby Gems version is wrong.

Create a new file in config/initializers/gem_version_check.rb and add this code to it:

required_rubygems_version
if Gem.rubygems_version < Gem::Version.new(required_rubygems_version)
  raise RuntimeError,  "Ruby Gems version is #{Gem.rubygems_version}, but this project requires Ruby Gems >= #{required_rubygems_version} to be used."
end

This will raise an exception and cause the initialization of the app to fail if the Ruby Gems version is wrong on startup.

/Users/spock/devo/games/config/initializers/gems_version_check.rb:3:in `': Ruby Gems version is 2.4.8, but this project requires Ruby Gems >= 2.6 to be used. (RuntimeError)

Note that this runs every time the Rails server is started, and in all environments, so it also performs a sanity check that the test and production environments are properly configured.

Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43