How to Make a Specific Gem Version as Default?

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.

7

6 Answers

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.

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.

3

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.

1

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

1

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest