How to specify minimum bundler version for Gemfile? - ruby

When my Gemfile is using :mri_20, and previous versions of bundler do not support that, is it a good idea to add
gem 'bundler', '~>1.3.5'
to the Gemfile? Is there a better way to enforce a minimum bundler version?

This won't have any affect on the bundler used to manage the gems in the Gemfile. The version of bundler that's used is the one that's available in your current ruby environment.
The best way to manage this is with gemsets - you can create a gemset with a known, working version of bundler and always switch to that gemset when working with that project.
To check the bundler version, run:
$ bundle --version
Bundler version 1.3.5
If you want to enforce the bundler version when running bundle install, put this at the top of the Gemfile:
# Gemfile
if Gem::Version.new(Bundler::VERSION) < Gem::Version.new('1.3.5')
abort "Bundler version >= 1.3.5 is required"
end

Related

How can `gem list` show multiple `default` versions for a gem?

When I run gem list bundler, I get the following result
+ gem list bundler
*** LOCAL GEMS ***
bundler (default: 2.3.22, default: 2.1.4)
How is it possible that two versions are marked as default? Is there a way to update the default one?
Context
The reason I ended up looking into which is the default Bundler version is that I've been running into this warning on a self-managed CI machine on and off for a while:
Gem::LoadError: You have already activated bundler 2.3.22 [or whichever latest version is available], but your Gemfile requires bundler 2.1.4.
I looked into my Gemfile.lock and it requires bundler like this:
bundler (>= 1.12.0, < 3.0.0)
So I'm guessing at some point, Bundler activated (which I believe means selected for runtime use) version 2.1.4 because that was the default available one.
I hope to be able to forever prevent that error by forcing the latest version of bundler to be the one used at runtime, but I'm not sure how to achieve it.
That is, how can I get the gem list bundler output to not show default: 2.1.4?
Thanks for your help!
To delete the duplicate default versions
gem environment
you will find the installation directory:
INSTALLATION DIRECTORY: /home/seb/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0
in /specifications/default you will find your .gemspec files, you will remove your duplicate one
rm bundler-2.2.31.gemspec
to verify: gem list bundler
(2.2.31, 2.2.29, default: 2.2.15)
you can update the default version with gem install bundler:2.3.18 --default but it appears that bundler use a arbitrary version that fit his needs. If you want use a specific version to bundle the Gemfile, you add it in your gemfile
gem "bundler", "~> 2.3"
it will appear at the end of the gemfile.lock
BUNDLED WITH 2.3.18

how to set bundler version with rvm?

I have a project that uses RVM, and it have .ruby-gemset and .ruby-version files, but I want set the bundler vession too. I tried .ruby-bundler but not worked.
When enter in the folder this set the gemset and ruby version correctly but not install de bundler version correctly, the effect is that in my project each machine is with a specific version of bundler, and each commit change the Gemfile.lock, there is a easy way to force everyone use the same bundler verion?
Edit last 2 lines of the Gemfile.lock in your project:
BUNDLED WITH
2.2.0
change 2.2.0 with the version you want.
Then run bundle install, the bundler should inform you about the bundler version problem. And bundler will provide you some instructions about how to upgrade the bundler.
You can set the bundler version in the Gemfile, and bundler will use the correct version.

Bundler conflict requirements for the Ruby version

I installed the rbenv but when try to execute this command
bundle install --path vendor/bundle I got an error.
Error:
Bundler found conflicting requirements for the Ruby version:
In Gemfile:
Ruby (~> 2.3.7.0)
github-pages (~> 212) was resolved to 212, which depends on
jekyll-feed (= 0.15.1) was resolved to 0.15.1, which depends on
Ruby (>= 2.4.0)
How do I fix this?
As mentioned in the comments the error is saying you have 2 options, upgrade ruby to a higher version or downgrade the github pages, you mentioned you are using rbenv which is very useful for this scenario. All you need to do is upgrade your ruby version. Go to your project folder and open a terminal then you can do something as follow:
rbenv install x.y.z, where x.y.z is the ruby version you want to install (e.g. 2.4.0), after that all you need to do is specify which version of ruby do you want to use in that project, so in the same terminal run: rbenv local x.y.z. That's it you should be able to run bundle install.
A couple of notes:
Remember to update your gemfile with the proper ruby version (after upgrade)
If you want to install your gems in a particular path and you are using bundle >= 2 (to check gem version: gem list | grep bundle), you should use a configuration file instead of specify the path in the command. Create a .bundle folder in your root directory and add a file called config, then in such file add the path (e.g. BUNDLE_PATH: ./vendor/bundle)
In your gemfile you have the listed
Ruby (~> 2.3.7.0)
github-pages (~> 212)
github-pages 212 depends on jekyll-feed 0.15.1, which requires ruby 2.4.
To use github-pages 212, you'll need to upgrade to ruby 2.4+, alternatively, downgrade github-pages to an older version that also supports 2.3

Bundler version for Ruby 2.3.8?

I currently have 2 ruby versions, 2.5.5 and 2.3.8, I am managing them with rbenv, and for gems, I use bundler to manage my specific gem versions. I have an issue when I want to switch to a project that uses 2.3.8 or a version that has to do with Ruby version 2.3.
My question is how do I get bundler to run the command to bundle install interact with my 2.3 projects.
My current version of bundler is 2.0.2.
I have already tried installing a lower version of bundler of which the bundler website claims to interact with ruby version 2.3.
I thought that if I specified the command to run bundle _version_ install it would work, but it still gave me the response that it needed ruby version 2.5 for bundler to work.
Lots of help appreciated.
In some situations, isolation can help. I would like to recommend rvm (https://github.com/rvm/rvm) for managing ruby versions. This tool is very similar to rbenv but in comparison, it allows you to create gemsets which are kind of containers of gems for special purposes. For instance:
rvm install 2.5.5 --disable-binary
rvm use 2.5.5#name-of-gemset --create
gem install bundler
bundle install # inside your project folder with Gemfile
I think that you will not have problems with versions again.

Ruby: Rails: Which version of a gem is used?

I have a Rails 3 application which has the following line in the Gemfile.
gem 'sqlite3', '1.3.6'
However, if I list my local gems I get:
> gem list sqlite3 --local
*** LOCAL GEMS ***
sqlite3 (1.3.6, 1.3.4)
When my Rails apps does a
require 'sqlite3'
which version of the gem is selected? Is it that the first gem in the list is selected? Is there a way to tell the Ruby runtime to use version 1.3.4 even if version 1.3.6 is installed, and mandated by the Gemfile?
You could find out with
bundle exec gem list sqlite3
Either the Gemfile will specify a version or Gemfile.lock will have the version.
Examples:
Gemfile:
gem 'tiny_tds', '0.5.0'
Gemfile.lock:
tiny_tds (0.5.0)
Edit: if you want to see the version, use iltempos' suggestion or in the rails console type
1.9.3p194 :001 > SQLite3::VERSION
=> "1.3.6"
the Gemfile list all the dependencies of your Rails application, you can add constrains about the version to use for each gem. In your example you specified that your application depends on sqlite3 1.3.6 so it will use the version 1.3.6.
In general the exact version of the gems required by your application are in the Gemfile.lock.
There are several syntaxes you can you to specify the versions:
gem 'gemname', '1.2.3' - Requires gemname version 1.2.3
gem 'gemname', '>= 1.2.3' - Requires gemname version 1.2.3 or any higher version. Can break things
gem 'gemname', '~> 1.2' - Require gemname 1.2 or a minor update like 1.2.9. You get updates but not mayor one which could break compatibility
The interesting thing to note is that once the Gemfile.lock is written and checked in your version control, it's shared between all the members of the team that all will use the same, exact, versions of the required gems.
If you need to update a required gem you can do that in 2 steps:
Update the Gemfile if needed
Run bundle update gemname
The step 2 will download the new gem version (if there is a new version respecting the constrain in the Gemfile), install it and update the Gemfile.lock accordingly.

Resources