Can You Have Multiple Versions of a Gem in a Gemfile

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.

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.

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.

How to use two versions of the same gem on the same ruby version in asdf?

Remember that Gemfile and Gemfile.lock should make it possible to have multiple versions of the same gem installed and the correct one will be selected based on whatever constraints are described.

The only time you'll need to force a single version is when dealing with command-line tools (e.g. rails or rake) where only one can be active at any given time.

Gemsets are a byproduct of a time before Bundler and Gemfile.

Managing multiple versions of a Ruby Gem

You should use RVM to manage multiple versions of ruby and gems.
Visit followings links to have an idea of rvm and to install.

https://rvm.io/

https://rvm.io/rvm/install/

Having different rails and gems versions installed on dev environment

You will have all different versions. BUT you will need to add the version number for all gems to your gemfile

For example
Sample Image

and in the gemfile you state:

gem 'remotipart', '1.2.1'

Different versions of the same gem in Gemfile - possible?

No, It is disallowed in bundler

What does it mean when there are two versions of the same gem listed in parentheses in a gem list?

It shows all the versions that are installed locally. By default the newest one will be used unless bundler (via Gemfile) is configured to use another one.



Related Topics



Leave a reply



Submit