How do I require a specific version of a ruby gem?
My problem was twofold:
1) confusing gem command syntax with that used in config.gem lines in a rails environment.rb configuration file.
2) failing to issue a require command after the gem command.
Proper usage in a script is:
gem 'ruby-oci8', '=1.0.7'
require 'oci8' # example is confusing; file required (oci8.rb) is not
# same name as gem, as is frequently the case
Proper usage in a rails 2.3.x environment.rb file is:
config.gem "ruby-oci8", :version=>'1.0.7'
Thanks to the folks at http://www.ruby-forum.com/topic/109100
Ruby: How to install a specific version of a ruby gem?
Use the -v
flag:
$ gem install fog -v 1.8
Require a specific version of RubyGems
I don't believe it's possible to select from among multiple rubygems versions in a single Ruby installation. But you can ensure that the required version is being used. Put the following somewhere where it will be run early in your application's startup, such as the top of your Gemfile (which is just a Ruby file), or at the beginning of a script which doesn't use bundler:
required_rubygems_version = '2.6.0'
if Gem.rubygems_version < Gem::Version.create(required_rubygems_version)
raise "Please upgrade to rubygems #{required_rubygems_version}"
end
How to run a specific version of a ruby gem
I can't reproduce this issue, but I can think of a few reasons why this might happen:
- Bundler artifacts (perhaps in
.bundle/
) pointing to the old version. Try running in a different directory and see if it still happens - A bug in RubyGems (try
gem update --system
) - During install, it asked if you wanted to replace the
brakeman
binary and you selected "no" - If you are using a Ruby version manager, maybe one version is on a different path than another (like a system gem versus one managed by
rvm
) - Any number of
GEM_PATH
,bundler
,gem
,rvm
weirdness that sometimes occurs
In any case, if I were you I'd gem uninstall brakeman
, remove all versions, and install fresh. If you are using rvm
, start with a fresh gemset or rvm gemset empty
the current one.
How to require a specific gem version in rails gemfile?
If you want to make event-brite client work somehow then you can simply bump the version of tzinfo dependency in eventbrite gemspec file.
First clone the following repository
git clone git@github.com:ryanjarvinen/eventbrite-client.rb.git
Then replace the content of eventbrite-client.gemspec file with the following.
Gem::Specification.new do |s|
s.name = %q{eventbrite-client}
s.version = "0.1.3"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Ryan Jarvinen"]
s.date = %q{2011-08-28}
s.description = %q{A tiny EventBrite API client. (http://developer.eventbrite.com)}
s.email = %q{ryan.jarvinen@gmail.com}
s.extra_rdoc_files = [
"LICENSE",
"README.md"
]
s.files = [
".document",
"LICENSE",
"README.md",
"Rakefile",
"VERSION",
"eventbrite-client.gemspec",
"lib/eventbrite-client.rb",
]
s.homepage = %q{http://github.com/ryanjarvinen/eventbrite-client.rb}
s.require_paths = ["lib"]
s.rubygems_version = %q{1.6.2}
s.summary = %q{A tiny EventBrite API client}
if s.respond_to? :specification_version then
s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_development_dependency(%q<rspec>, ["~> 1.3.0"])
s.add_runtime_dependency(%q<httparty>, ["~> 0.8.0"])
s.add_runtime_dependency(%q<tzinfo>, ["~> 1.1"])
else
s.add_dependency(%q<rspec>, ["~> 1.3.0"])
s.add_dependency(%q<httparty>, ["~> 0.8.0"])
s.add_dependency(%q<tzinfo>, ["~> 1.1"])
end
else
s.add_dependency(%q<rspec>, ["~> 1.3.0"])
s.add_dependency(%q<httparty>, ["~> 0.8.0"])
s.add_dependency(%q<tzinfo>, ["~> 1.1"])
end
end
And then you can rebuild the gem with
gem build eventbrite-client.gemspec
Install it using
gem install ./name_of_the_gem.gem
How to publish a lower version of a Ruby gem
Yes, that is possible. Just adjust the required_ruby_version
in your .gemspec
file before uploading.
For example:
spec.required_ruby_version = '>= 2.5.0'
Related Topics
Activerecord Objects in Hashes Aren't Garbage Collected -- a Bug or a Sort of Caching Feature
How to Reinstall a Gem Using Bundler
Why Are Symbols Not Frozen Strings
Best Way to Combine Fragment and Object Caching for Memcached and Rails
What's the Best Way to Parse a Tab-Delimited File in Ruby
Finding All the Users That Have Duplicate Names
Strange Activerecord::Associationtypemismatch
How to Replace Multiple Newlines in a Row with One Newline Using Ruby
Key Issue with Installing Rvm (Ruby Version Manager)
Using a Ruby Script to Login to a Website via Https
Get Today's Date in Jekyll with Liquid Markup
Carrierwave Crop Specific Version
Collect Values from an Array of Hashes
How to Read the Body Text of an Email Using Ruby's Net/Imap Library