specify gem version to be used with a specific gem? - ruby-on-rails

This is more of a general question, I'm new to rails.
I trying to use a gem that requires an older version of json, json -v 1.6.5
other gems in my rails application depends on newer version of json, json -v 1.8
I'm wondering if it is possible to specify json version to be used with a specific gem?
Thank you

No it's impossible to use 2 versions of a gem at the same time.
Use 2 versions of gem at same time

If bundle update doesn't resolve the dependencies I would fork on of the gems you need to use the same version as the other and then point to my fork.
gem 'some_gem', git: 'https://github.com/user/My-Fork.git'

Each of your dependencies will have a version constraint and your project can only use one version of each gem.
The answer to your specific question is that yes, you can control the version of an indirect dependency by specifying which version to use directly in your gemfile. However, this version must satisfy the version constraint of the direct dependency.
A brief example. Let's say your gemfile looks like this:
source 'https://rubygems.org/'
gem 'somegem', '~> 1.0'
And your gemfile lock looks something like this (note, some parts omitted for brevity):
GEM
remote: https://rubygems.org/
specs:
somegem (1.0)
json (~> 1.8)
json (1.8)
The Gemfile.lock indicates that somegem is dependent on json and that the json version must be greater than or equal to 1.8 but less than 2.0 (read more about the ~> operator here).
If you want to use lets say json version 1.9, you can modify your gemfile or use bundler commands to update the version used in the lock file.
E.g.
source 'https://rubygems.org/'
gem 'somegem', '~> 1.0'
gem 'json' , '~> 1.9'
In your specific case, if you have two dependencies that use conflicting versions of an indirect dependency, the gems will be incompatible. However, this example is meant to show that you can specify the version of an indirect dependency IF it meets the constraint specified by the direct dependency.
Rails uses Bundler to manage Ruby dependencies. An overview of the gemfile would be a good place to start learning how to manage your project's dependencies.

Related

bundle update rails resolving to 6.0.1 release candidate

I'm trying to update rails from 5.2 to 6.0. In my Gemfile I have declared:
rails (~> 6.0) was resolved to 6.0.1.rc1
My expectation there is that I'd end up with 6.0.4 the ~> meaning optimistically resolve to the latest 6.0.* version?) Resolving to a release candidate isn't something I want to do.
I can specify it directly, of course, but I'd rather not tie the Gemfile to a specific version and count on bundler to resolve it correctly.
One quick fix would be to alter your Gemfile. If you want to retain the optimistic ~> 6.0 operator, you can add a second matcher for the version, like this (and thus report an error if it can't do so):
gem 'rails', '~> 6.0', '>= 6.0.4'
I often use this pattern to lock in security patches without losing the flexibility of the ~> operator.
I'd guess that something is preventing bundler from using a later Rails gem version - that is, you have a dependency that's locking your gem version to 6.0.1. If the above doesn't resolve it, can you post the relevant portions of your Gemfile in your question? And search Gemfile.lock for rails to see whether you have any gems that require rails 6.0.1 and not later versions.

How can I find out what gem is dependent on termios in my Gemfile?

I have updated all of my gems, including to Rails 3.2.8, prior to a new deployment. However, my application is now broken because something is trying to install gem "termios" version 0.9.4.
Apparently, 0.9.4 does not work on any computer or server I own. There are some newer versions, 0.9.6 specifically, but they are not posted in wherever bundler looks for gems.
There are some version on Github, but they have been mysteriously renamed "ruby-termios". Well, some gem in my Gemfile is not looking for ruby-termios. It's looking for termios. Failure.
How can I find out which gem is trying to install this so I can see if it can be whacked?
Check your Gemfile.lock - it has all the gems and their dependencies listed in it. As long as you've been able to install these gems in the past, you'll be able to tell where that dependency is coming from.
The gem command will dump out the tree of dependencies for you.
$ gem dependency
Or if you want to check just a specific gem.
$ gem dependency foo

Does Bundler Gem take in consideration your Ruby environment?

My question is simple one, does gem bundler considers your ruby environment (e.g. 1.8.7 | 1.9.2) before deciding which gem to take based on gem file?
Let's say your gemfile contains
gem 'thor'
gem 'json'
gem 'grit'
When you run bundle install will take versions of the gem that are compatible with your current ruby environment or just latest gems?
It depends! Bundler relies on the configuration of the Gemspecs that each Gem provides.
Gemspecs offer the posibility to provide different or additional dependencies based on the runtime environment. IE you can change the dependencies for JRuby or provide different binaries for i386 architectures.
As far as i know, it's not possible to declare a gem as 1.9 or 1.8 compatible (which would have made sense to me). I think it's partly so, because 1.9 is 99% downward compatible.
You are always forced to have a look at the gems themselves. Because of this, there are sites like http://isitruby19.com/
As you might see, it's not an issue of Bundler, but RubyGems.

gem list is showing two versions of a Ruby gem immediately after updating the gem

I just updated the mime-types gem with gem update mime-types. gem list displayed mime-types (1.16) prior to updating. After the update gem list shows mime-types (1.17.2, 1.16). Why are two version displayed?
More info: I have other Rails projects on the same computer. I have not updated the mime-types gem in any other projects. Running gem list from another project's directory (where mime-types has not been updated) displays mime-types (1.16).
You have both versions installed. If you want to delete old versions (which will not always will be possible due to dependencies) use gem cleanup.
Which version of RubyGems do you have? gem -v
This is interesting: I have the newest version of RubyGems but my system behaves differently:
gem list => all the gems, all the versions. No matter from where I call it.
gem list --local => same as before but user-wide.
bundle list => all the gems in a project (one version per gem)
The same goes for bundle update and gem update.
bundle update replaces the old version by the new one (the dependencies are taken care by bundler), but gem update keeps both. So if you want to keep only the newest version, run gem cleanup.
bundle outdated might be useful: it displays the outdated gems in your project (based on rubygems.org)
This can happen because of gem dependencies.
For example if another gem depends on that gem, and the other gem does not have a version specified for it, and(/or) it gets updated and if its dependency on that gem's version changes... well you get the idea.
Sometimes I do a bundle and I see a ton of new versions getting downloaded. All due to changed... dependencies.

Specify a plugin as gem from github in Gemfile

I am including 'acts_as_rateable' gem in my Gemfile like this
gem 'acts_as_rateable', :git => 'git://github.com/azabaj/acts_as_rateable.git'
and then when I do bundle install it gives me this error message!
Could not find gem 'acts_as_rateable
(>= 0, runtime)' in
git://github.com/azabaj/acts_as_rateable.git
(at master). Source does not contain
any versions of 'acts_as_rateable (>=
0, runtime)'
I am developing a plugin of my own, when I include that, even that gives the same error like this..
I assume this has something to do with the gemspec?
Please help
Rails version : 3.0.1
Rubygems version : 1.3.7
Bundler version : 1.0.3
let me know if you need any other details..
If you want to pull a gem directly from GitHub, you can put this into your GemFile:
gem 'twitter', github: 'sferik/twitter'
Which will use the default branch. To specify the branch to use:
gem 'twitter', github: 'sferik/twitter', branch: 'branch_name'
The problem is that the repository you link to is not a RubyGem. You can get with
$ rails plugin install git://github.com/azabaj/acts_as_rateable.git
Edit: This answer was accurate on the date it was published. Rails 4 doesn't support plugins anymore, so you will have to make this into a local gem yourself. Bundler has some commands that will help you with it, or alternatively you can use a different library, e.g. https://github.com/anton-zaytsev/acts_as_rateable.
Jakub Hampl is right, but it seems strange to depend on git repos like that. I guess you're you making it yourself? If so, make it a real gem. It should have a acts_as_rateable.gemspec and you'll be able to depend on it like you wrote. Bundler makes your life easy, create the gemspec with
$ bundle gem acts_as_rateable

Resources