How to make a specific gem version as default?
Asked Answered
A

7

35

I have two versions of ruby gem.

json (default: 2.0.2, 1.8.6)

Here, the latest version is set to default; however I need json 1.8.6 to be set as default. Is there anyway to make the older versions of the gem as default? cos I am unable to uninstall the default json version. Need a switch between available gem versions.

Abscise answered 2/3, 2017 at 6:38 Comment(7)
Do you use rvm to manage your ruby versions?Towny
@EddeAlmeida Yea I use RVM; anyway through it?Abscise
Sure. You may create a private bundle for this application and install only the gem you need in this bundle.Towny
What version of Ruby are you using for this project?Towny
Then I am editing my answer accordingly. Have you tested it?Towny
Use bundler for your projects.Varletry
stackoverflow.com/users/125816/sergio-tulentsev is correct. Using bundler will allow you to constrain the specific versions of gems to those in your Gemfile.lock. To focus your project on the earlier version, put "gem 'json', '~>1.8.6'" in your Gemfile and run "bundle". Run your server with "bundle exec rails s" and you're good to go. If you're using rvm or rbenv (with gemsets) you can create an application-specific gem cache, which will help to alleviate this issue by separating the available gems (per project, say)Podolsk
O
15

Check what you have with:

gem list json

Set the one you want:

gem install --default -v1.8.6 json

This is most useful for things like "bundler"!!! For other things, using bundler and a Gemfile is probably a better choice.

Orjonikidze answered 17/3, 2019 at 3:33 Comment(4)
Using --default will probably mess up your gem installation, and does not work for selecting a default lower version (I spent some time to fix my ruby installation after I run gem install --default)Alate
@Alate how did you fixed it?Ulda
I uninstalled all gems and ruby, and then I reinstalled itAlate
--default makes it so the gem cannot be removed (easily). See the "Default gem" explanation here: stdgems.org. I don't think the --default option is what you want here. See this answer https://mcmap.net/q/370209/-how-can-i-remove-a-default-gem-want-to-uninstall-a-gem-1-7-7-version-of-json on how to delete a gem that you installed with --default.Perichondrium
S
4

Add

gem 'json', '1.8.6'

to your Gemfile or execute

gem install 'json' -v 1.8.6 # may require sudo if you use system ruby

from terminal.

Seismic answered 2/3, 2017 at 6:45 Comment(3)
I have 1.8.6 already installed; but i tried this still; no luck :(Abscise
so, if you have no any portability concerns, do it like, require 'absolute/path/to/gems/ruby-2.3.1/gems/json-1.8.6'Seismic
didnt work :( cannot load such file -- /Users/%username%/.rvm/gems/ruby-2.4.0/gems/json-1.8.6 (LoadError)Abscise
A
1

A Gemfile is a must but is not enough. You should also change the line

require 'json'

to

require 'bundler/setup'
Bundler.require :default

This will require all the gems specified in your Gemfile that without a group.

Aftershock answered 2/3, 2017 at 9:7 Comment(1)
Technically speaking, this will use Gemfile.lockVarletry
T
0

Add this your Gemfile

gem 'json', '1.8.6'

Now run this in your command line

bundle update

This should set your version you require

Tutorial answered 2/3, 2017 at 9:33 Comment(1)
it doesnt work; cos I have json 2.0.2 already installed and it is set to defaultAbscise
T
0

Since you are using RVM to manage your Ruby versions,as you told in the comments, there is an easy solution: creating a private bundle for this application and install only the version of the gem you need in this bundle.

This is how you may do it:

1) Enter the directory of your application;

2) Type the following command

rvm use ruby-2.4.0@your_app_name --ruby-version --create

(I am assuming you are using Ruby 2.4.0. If this is not your version, replace it accordingly in the command above, please.)

3) Install bundler gem

gem install bundler

4) Make sure your Gemfile declares the version of the gem you need. In this case it must have the line:

gem "json", "1.8.6"

5) Now run

bundle install

And you are done!

WARNING: This procedure will make sure the json gem will be version 1.8.6. But you may have problems with your bundle install if some other gem installed requires a newer version of json gem. In this case you'll have to solve this conflict some other way.

To learn more about different bundles for different applications, read this.

I hope this helps.

Towny answered 2/3, 2017 at 10:14 Comment(0)
M
0

You can manually move the related .gemspec file from
lib\ruby\gems\3.2.0\specifications\default\
to
lib\ruby\gems\3.2.0\specifications\
to unmark the gem as default.

Melisent answered 12/4, 2023 at 7:21 Comment(0)
F
-4

Context:

Running a simple ruby test from CLI

ruby -I test test/controllers/api_controller_test.rb

error:

You have already activated json 2.0.2, but your Gemfile requires json 1.8.6. Since json is a default gem, you can either remove your dependency on it or try updating to a newer version of bundler that supports json as a default gem. (Gem::LoadError)

The workaround was simply running the test with bundle exec as follows:

bundle exec ruby -I test test/controllers/api_controller_test.rb

Footer answered 19/3, 2021 at 12:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.