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

4

6

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?

Ocana answered 22/4, 2017 at 2:27 Comment(0)
G
3

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.

Globefish answered 22/4, 2017 at 5:56 Comment(1)
This project has not been updated for two years. It was a nice idea, but I think it's deprecatedJezabella
B
4

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.

B answered 22/4, 2017 at 5:48 Comment(3)
"Bundler figures out which versions to install depending on your current Rails version if you don't specify the Gem version." I was not aware of this so I did a quick Google search and didn't find anything... admittedly I didn't scour every source. Is there a link to how Bundler knows what gem version aligns with what version of rails?Crawl
I see, rails would be in the gem's gemspec file as a dependency. It could also be in the gem's gemfile (if it has one) which will not be listed on rubygems so you would have to go to the gem's github repository to check it's gemfile. This is no guarantee that the gem will work with a rails version though since the gem owner may not list a rails version limitation in their gemspec or gemfile. For the major gems though (e.g., devise, paperclip) this is probably pretty reliable.Crawl
Hi Steve Carey, Bundler has it's own website. You can read more about it here: bundler.io Bundler's job is to match all gem versions so that they work together.B
G
3

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.

Globefish answered 22/4, 2017 at 5:56 Comment(1)
This project has not been updated for two years. It was a nice idea, but I think it's deprecatedJezabella
P
1

as for August 2023, one could use RailsBump, It's a continuation of Ready4Rails.

P.S: Even RailsBump won't guarantee to give you the definite answer about a certain gem compatibility. so you'd still need to test the compatibility yourself by testing the application

Pericarditis answered 25/8, 2023 at 10:51 Comment(0)
C
0

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.

Crawl answered 22/4, 2017 at 3:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.