Conflicting dependency - ruby-on-rails

I have a conflicting dependency:
Gem A depends on Gem B 2.0
But
Gem C depends on Gem B 1.5
Should I force like this:
gem 'B', '~> 1.5'
All of my other gems are using Gem B 2.5 (the latet version) without problem though, so can I do something like this in my Gemfile?:
gem 'B' # 2.5
gem 'A', dependency: 'b 2.0'
gem 'C', dependency: 'b 1.5'
# gems happily use B 2.5
Update, my exact problem:
rails-observers(>=0) ruby
activemodel(~> 4.0)
jquery-scrollto-rails(>=) ruby
activemodel(~> 3.1.0)
My gemfile isn't enfocring any dependencies at all. No version number option.
bundle install
completes okay but
bundle update
results in the error above

Fork jquery-scrollto-rails on github and upgrade the version. Run it's testsuite. If it runs make a pull request and until it's accepted use your version from github.
If it's not running, then -
Don't complain, fix ;)
Edit
They have a newer version which supports railties 3 < 5
Add this to your Gemfile
gem 'jquery-scrollto-rails', '~> 1.4.3'
Then run $ bundle install
See rubygems
NOTE:
I recommend always using version specifications in your Gemfile. The most optimistic strategy is just by fixing them to the minor version e.g gem 'jquery-scrollto-rails', '~> 1.4'. This is a tremendious help for bundler, which has an np-complete problem to solve. In theory according to Semver there should be no incompatible changes on minor version updates and you could have fixed your problem just by running $ bundle update. A lot of gems do follow this convention, but there are blacksheeps

Related

Bundler: how to remove uninstalled gems

I'm trying to install the pg_search gem. In the first attempt I did not pay attention to the necessary version of ruby (we are using 2.3.1 and 2.4 was required), in the error message that appeared I was asked to run bundle update, but it updated pg_search to 2.3.5 which require ruby >= 2.5. Even though I specified an older version of the gem, it still shows the same message:
Gem::InstallError: pg_search requires Ruby version >= 2.5.
An error occurred while installing pg_search (2.3.5), and Bundler cannot continue.
Make sure that `gem install pg_search -v '2.3.5'` succeeds before bundling.
I already installed the gem by running docker-compose run web gem install pg_search -v 2.1.4, and recreated the container. My Gemfile:
source 'https://rubygems.org'
gem 'rails', '~> 5.2.0'
# Use sqlite3 as the database for Active Record
# Use Puma as the app server
#gem 'mina-puma', :require => false
gem 'puma', '~> 3.7.1'
gem 'pg', '~> 0.18'
gem 'pg_search', '~> 2.1', '>= 2.1.4'
...
Bundler version: bundler (>= 1.3.0)
I would like to know how to remove pg_search 2.3.5 and install 2.1.4.
Even though I specified an older version of the gem
No, you didn't.
You specified '~> 2.1', '>= 2.1.4', which means anything 2.1.4 <= version < 3.0.0.
By running bundle update, this installed the latest available version that met your requirements, which was apparently 2.3.5, not 2.1.4.
If you need to also specify a constraint to ruby version 2.3.1, you can also put this in the Gemfile:
ruby '2.3.1'
...And then running bundle update will also take that into consideration when finding the latest compatible dependencies.
I would like to know how to remove pg_search 2.3.5 and install 2.1.4
You don't have version 2.3.5 installed against this ruby version, because it's incompatible.
Apparently you've already installed version 2.1.4.
The problem is that your Gemfile.lock is still expecting version 2.3.5. There are a few ways you could resolve this, but one way or another you need to update the Gemfile.lock to have a compatible set of dependencies with your ruby version.
The simplest approach is probably to just re-run bundle update pg_search, but make sure you're actually using the correct ruby version this time. That should downgrade the dependency, as the newer library version isn't compatible with the older ruby version.
If you still encounter issues, you could take my advice of adding the ruby constraint to the Gemfile, and revert whatever other changes you've recently made that created this incompatible mix of dependencies.

How does one upgrade a specific ruby gem to a specific (or the latest) version?

I am trying to upgrade a gem (hydra-derivatives) to version 3.3.2 to see if it solves a bug we are having.
hydra-derivatives is not a Gemfile gem; it's bundled as a dependency of another gem, called hydra-works.
What I've Tried
bundle update --conservative
hydra-derivatives but that only upgraded hydra-derivatives to
3.2.2 (& we want 3.3.2) and its dependency mini_magick from 4.5.1 to 4.8.0
adding gem 'hydra-derivatives', '~> 3.3.2' but that gave me:
You have requested:
hydra-derivatives ~> 3.3.2
The bundle currently has hydra-derivatives locked at 3.2.1.
Try running `bundle update hydra-derivatives`
If you are updating multiple gems in your Gemfile at once,
try passing them all to `bundle update`
I don't want to run bundle update hydra-derivatives because I don't want it to update a bunch of unnecessary gems and cause problems, hence why I read about --conservative
a. I ran this anyway to test it, and it upgraded target gem to only 3.2.2 and 15 gems in total!
hydra-derivatives is not a Gemfile gem; it's bundled as a dependency of another gem, called hydra-works.
You can still add this as an explicit dependency in your Gemfile:
# only restrict the version if you know of an incompatibility
gem 'hydra-derivatives' , '~> 3.3'
then run
bundle update hydra-derivatives --conservative
or
bundle update hydra-works --conservative
Remove the hydra-works gem from your Gemfile.
Either remove the gem and its dependencies by hand from the installed gem location or if you have the application in its own Ruby environment using rbenv or rvm run bundle clean --force.
Beware bundle clean --force will remove all of the gems in the Ruby version other than those specified in your Gemfile. If you have other applications that use the same version of Ruby you'll have to reinstall the gems for that application if they are different than what you are using in this application.
Add this to your Gemfile
gem 'hydra-derivatives', '~> 3.3.2'
gem 'hydra-works'
And run bundle install
You should see the correct dependency version now in your Gemfile.lock

How do I know that a gem is compatible with a version of rails?

I'm just getting started with ruby on rails and as I follow the tutorial, there isn't an explanation of how certain gems were gathered and placed in the Gemfile. I've just been copying and pasting them and putting them in my Gemfile and running bundle install.
How does one go about downloading specific versions of gems, and their dependencies as well as making sure that they are compatible with the version of rails I'm using?
Bundler figures out which versions to install depending on your current Rails version if you don't specify the Gem version. Usually Bundler will warn you also when it can't install a version you specified.
gem 'gemname'
This installs whatever version is compatible with your Rails version.
gem 'gemname', '1.5'
This installs version 1.5 only if it supports your current Rails version.
gem 'gemname', '>=1.0'
This installs version 1.0 or greater if available and compatible.
If you want to install a specific version (2.2) but you know that version 3.0 will break your code (some gems do that like Mailchimp gem) you can specify a minimum version and maximum version:
gem 'gemname', '>= 2.2.0', '< 3.0'
Since it is more or less common there is a shortcut for this:
gem 'gemname', '~> 2.2'
The "~>" will match any version smaller than 3.0. It tells bundler to install only 2.X never reaching 3.0.
Every gem you want to install will tell you which version is compatible with your Rails version. Usually it will say the minimum version number. For example the boostrap gem:
https://rubygems.org/gems/bootstrap/versions/4.0.0.alpha3.1
If you look at the site, it tells you the dependencies. It doesn't mention a minimum Rails version so you can install always the latest version:
RUNTIME DEPENDENCIES (2):
autoprefixer-rails >= 6.0.3
sass >= 3.4.19
DEVELOPMENT DEPENDENCIES (13):
actionpack >= 4.1.5
activesupport >= 4.1.5
capybara >= 2.6.0
compass ~> 1.0.3
jquery-rails >= 3.1.0
json >= 1.8.1
minitest ~> 5.8.0
minitest-reporters ~> 1.0.5
poltergeist >= 0
slim-rails >= 0
sprockets-rails >= 2.3.2
term-ansicolor >= 0
uglifier >= 0
If it specifies a Rails version under dependencies like this:
rails >= 4
It means that you need at least Rails 4.0.
For rails 4 and 5 you can check here. If gem ready or not.
Rails automatically install best suited newest version of gem when you run bundle install if you write gem 'gemname'. Specifying a gem version is mention in other answers.
If a specific version of ruby or rails or any dependency is required in a gem. Then it is specified in gemspec file of gem and Gemfile for that gem.
You can cross-check in gemspec or Gemfile of gem also if something break while bundle install.
In your gemfile you can list gems as follows:
To load the latest stable version just leave the version out
gem 'gemname'
To load a specific version, add the version
gem 'gemname', '1.0.0' # or whatever version you want to specify
To load a specific version or whatever the highest subversion is
gem 'gemname', '~> 1.0.0' # this will load version 1.0.x with x being the highest sub-version.
gem 'gemname', '~> 1.0' # will load version 1.x
gem 'gemname', '~> 1.3' # will load version 1.3 or higher but less than 2.0
You can update the gem versions in your gemfile based on how you listed them (per the above) by running bundle update.
To know whether a version is compatible with your Rails version I generally look at the date of the release on rubygems.org compared to the release date of the latest major Rails version. A gem released between June 2013 and June 2016 would most likely be targeting Rails 4. It may or may not work in Rails 5 (which was released June 2016). I find that most gems that work for Rails 4 work in Rails 5, and Rails 5 has been out long enough for issues with a specific gem compatibility to be already documented. Gems with the last release date prior to June 2013 (so for Rails 3 or earlier) I avoid.

Gemfile Twitter + Faraday

In my Gemfile
source 'https://rubygems.org'
ruby '2.1.0'
gem 'rails', '4.0.1'
gem "faraday"
gem "faraday_middleware"
gem "twitter", github: 'sferik/twitter'
if I run
$ bundle install
I get
Bundler could not find compatible versions for gem "faraday": In
Gemfile:
twitter (>= 0) ruby depends on
faraday (~> 0.9.0) ruby
faraday (0.8.9)
TL;DR: Try running bundle update.
Bundler tries to find gems that match in such a way that all their dependencies also match. So consider this:
gem A v1 depends on B v1
gem A v2 depends on B v2
gem C v1 depends on B v1
there is no version of C that knows how to handle B v2.
In this case, Bundler will choose (or even downgrade) A to v1, so that you can A and C running next to each other.
However there are a couple of things that might prevent this from happening, and that will cause the error you are seeing:
There is no A v1, so no match can be made at all. You're stuck in this case, those gems will not work together at all.
You've already installed A v2 and you are adding C later. This means Bundler needs to downgrade A, but it doesn't do downgrades/upgrades when only running bundle install. You'd have to specifically say that it needs to recalculate the dependencies by running bundle update A or for all the gems in your gemfile: bundle update.
One of the gems is from a git repository. Git repositories don't really have versions like gems hosted on rubygems.org do. This means that Bundler will only fetch the latest version and cannot downgrade that gem. You need to specify a branch or revision manually in this case.
My guess is that you are looking at scenario 2. You've already installed (and locked down on) version 0.8.9 of faraday. By adding twitter, your previous lock needs to be updated.
Be careful of running bundle update without arguments however. It will try to get the latest versions of every gem in your gemfile, which might not be what you want.

Rails - Understanding Gems

I'm using the rails devise gem. I noticed a case sensitivity bug which turns out is fixed in the latest version of devise so I'm thinking about upgrading.
In my gem file I have:
gem 'devise', '~> 1.1.3'
When I run bundle I get:
Using devise (1.1.9)
Why the difference. And what setting should I be using in my gem file to upgrade to the latest and greatest?
Thanks
The ~> in your Gem declaration says that Bundler can install any version up to the next major version, so in this case it could install any version of devise that is => 1.1.3 and < 1.2.0.
Including the ~> is good practice, as it means security updates are automatic if the gem is using versioning correctly; in a production environment, you'll probably want to drop this moniker, though, and just set your gem versions statically to avoid issues.
To update to the latest version of the gem, everytime, just use the following with no second version argument:
gem 'devise'
See more information on the Gemfile format at http://gembundler.com/gemfile.html.
Just use :
gem 'devise'
and you will be getting the latest stable gem :)
The difference is because you're telling to Bundler to use 1.1.3 or a major version of this gem in you system, if you want to use a specific version just put '1.1.9' in the version param.
use bundle update devise to update the devse gem and bundle update to update all the gems (which is not advisable)
http://jsbin.com/ihiqe4
if you know the version number you want, try this (assuming it's 1.2.3):
gem 'devise', '1.2.3'
or just leave out the version number
if it has not been released yet, you can point to it's github repository instead.

Resources