2 Gems Need Different Versions of the Same Dependency

2 gems need different versions of the same dependency

I'd go for what @BaroqueBobcat suggests. I just want to add that - if you need the latest Twitter gem and can't wait for the maintainer of Groupon2 to update his gem - you can fork the Groupon2 on GitHub, update its gemspec, see if it still works by running its tests (and try to fix it if it doesn't) and include your own version using its Git URL in your Gemfile like so: gem "groupon2", :git => "https://github.com/yourgithubuser/groupon2.git".

If you want to be nice you can offer your changes to the maintainer of Groupon2 with a pull request for bonus points :)

How to use different versions of a gem at the same time

Using two versions of a single gem usually means: use two versions of the same class.

It's not possible without making modifications to these gems. You may try to place created classes in some module, resolve conflicts in methods imported into other classes, and so on. In general, it is not easy task, and usually the effect is not worth it.

What you should do in such cases is to ask the gem maintainers to update the dependencies, or try to do it yourself.

Maybe you can downgrade (use older version of) one of these gems, to the version in which the dependencies were the same.

Ruby Bundler - Multiple Ruby versions in the same gemfile

Gemfiles Declare Dependencies

A Gemfile declares a dependency on a Ruby version, with or without semantic versioning constraints. It is not meant to control multiple build targets for your application. It simply enforces that the Ruby version available to your app/gem is whatever you've defined. For example:

# Will fail if run with a different RUBY_VERSION.
ruby '2.2.4'

# Allows RUBY_VERSION >= 2.2.4, but <= 2.3.
ruby '~> 2.2.4'

# Allows either Ruby 2.2.4+ or 2.5.5+, with
# a minimum (but no maximum) patch version.
ruby '~> 2.2.4', '~> 2.5.5'

However, it won't install a given Ruby, nor do anything other than raise an error and a non-zero exit status when running bundler install. You need to take a different approach to test multiple targets.

Changing Build Targets with Continuous Integration (CI) Tools

If you're using an external CI like TravisCI, you can create a build matrix that targets multiple Ruby versions to test against. Whether you remove the Ruby version constraint altogether, or specify a supported range, is up to you. Leveraging your CI tool to build against the versions of Ruby you plan to support is really the best approach, though, whether or not you constrain your Ruby runtime in a Gemfile.

For example, you might use a matrix in your travis.yml like so:

language: ruby
rvm:
- 2.2.4
- 2.5.5

Switching Gemfiles

If you insist on doing it the way you're doing it, with a singular Ruby version allowed in your Gemfile, then you might consider having two separate gemfiles with different names in your source tree, such as Gemfile-2.2.4 and Gemfile-2.5.5. You can then specify which Gemfile to use with Bundler's --gemfile flag , or by symlinking a custom Gemfile to the canonical Gemfile for your project.

Here are some examples to consider:

# Resolve against a specific Gemfile with
# hard-coded Ruby version.

$ ls Gemfile*
Gemfile-2.2.4 Gemfile-2.5.5

$ bundle install --gemfile="Gemfile-2.2.4"
# Resolve against whatever custom file is
# symlinked to your ./Gemfile.

$ ln -sf Gemfile{-2.5.5,}

$ ls -F Gemfile*
Gemfile@ Gemfile-2.2.4 Gemfile-2.5.5

$ bundle install

Both approaches work, but the former is more flexible at the cost of needing to specify your chosen Gemfile each time, while the latter can help when you have a development/testing workflow that doesn't support Bundler's --gemfile flag.

Changing Rubies with Ruby Managers

If you have multiple Ruby versions in development, best practice is to use a version manager such as rvm, rbenv, or chruby. You can use your version manager to change rubies back and forth manually as needed.

You might also check whether your version manager supports auto-switching on .ruby-version or other configuration files. You'd still have to update that file each time you want to build or test against a different Ruby, but you wouldn't have to keep changing your Gemfile contents, re-pointing the Gemfile symlink, or updating a flag on each call to Bundler.

Whether or not any given approach is better than others will depend on your workflow. No technical solution will fit all circumstances, so your mileage may legitimately vary.

Different versions of the same gem in Gemfile - possible?

No, It is disallowed in bundler

Can you have multiple versions of a gem in a Gemfile?

You can set an intervall of allowed gems

gem 'rack', '<1.3.3', '>1.2.4'

It will load the most actual one inside the selected intervall.

But I don't think you can require different gem versions.
If a gem would be loaded in different versions, each class and module must get it own namespace to avoid to overwrite the methods of the gem.

Resolve multiple versions of rubygems

I have a feeling that you might be using a different version of ruby than ruby gems is. Either that or ruby doesn't know where to look for your gems.

See the the gem installation guide to ensure your environment is configured to use gems.

If you're still having problems after following instructions, ensure that you haven't got multiple versions of ruby installed. In the event that there are multiple version of Ruby available, make sure your scripts are calling the same version of Ruby as gem is. This is done by comparing the gem environment listing for RUBY_EXECUTABLE against your scripts' shebang line. Double check to follow any symlinks, because most distribution based installations of ruby will symlink /usr/bin/ruby to /usr/bin/ruby1.8

You should also check that your gems were installed by the same user who is running the script.

If you ran gem install without root privileges the new gems will be installed in your home directory. If you're running a script that depends on these gems as another user. Those installed gems will not be found. However, there's no problems if your gems are installed by root and a different user is running scripts that requires those gems.

I experienced this problem while switching from Ruby to Ruby Enterprise Edition. I found that I had to install all my required gems again using REE's instance of gem.

What is the syntax for `gem install` multiple gems, specifying versions for each one?

Since Rubygems 2.0 you can specify a version for multiple package installations like this:

$ gem install akami:1.2.0 atomic:1.1.14 aws-s3:0.6.2 backports:3.3.0 \
brendanlim-sms-fu:1.0.0 builder:3.1.3 capistrano:2.9.0 \
--ignore-dependencies


Related Topics



Leave a reply



Submit