andy4thehuynh
andy4thehuynh

Reputation: 2172

How to check if a gem is installed?

I installed data_mapper for a Sinatra project. Curious, why is it when I do gem install brew, I can $ which brew and get the path of its location and can't for data_mapper? This works for some gems and doesn't for others.

How do I verify a gem is installed properly? Would checking the version assure the gem is downloaded correctly?

Upvotes: 60

Views: 83260

Answers (4)

user513951
user513951

Reputation: 13612

General solution

To get the full list of gems that are installed:

gem list

To test for a particular gem, you can use -i with a regex:

gem list -i "^gem_name$"

(Credit to Timo in the comments for this technique.)


Specific solution for OP

If you can't find data_mapper, it may be that the gem name is different from what you expect.

Also, if you're just doing which brew to find brew, you aren't finding the gem called brew, you're finding the location of the brew executable. Try gem which brew instead.

If you're looking for data_mapper by doing which data_mapper, you probably won't find it. which is a unix program for finding unix executables, and data_mapper probably doesn't have one.

Since your goal is to verify a gem is installed with the correct version, use gem list. You can limit to the specific gem by using gem list data_mapper.

To verify that it's installed and working, you'll have to try to require the gem and then use it in your code.

Upvotes: 85

Smar
Smar

Reputation: 8591

For script usage, gem query probably is better:

gem query --silent --installed --exact rubygems --version 2.0.0

Upvotes: 0

Sathish
Sathish

Reputation: 1475

if you are using bundle for your project then use below command to check for the exact match of your gem within the project.

bundle info ^data_mapper$

Upvotes: 0

Spajus
Spajus

Reputation: 7406

In case you want to use the check in a script, this gives a better output (true or false) and appropriate exit code:

gem list -i <gem_name>

Alternatively add the version option:

gem list -i <gem_name> -v version

Upvotes: 42

Related Questions