Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I need to, from a bash script, check to see if certain Ruby gems are installed .

I thought I could do something like

if ! gem list <name>; then do_stuff; fi

but testing on the command line using echo $? shows that gem list <name> returns 0 regardless of if name is actually found.

Does this mean I have to use grep to filter the output of gem list, or is there a better way I can check to see if a gem is installed?

share|improve this question
add comment

2 Answers 2

up vote 11 down vote accepted

gem list <name> -i will return the string true if the gem is installed and false otherwise. Also, the return codes are what you would expect.

For more informations, see gem help list.

share|improve this answer
    
To check if a specific version is installed, just add --version, e.g.: gem list -i compass --version 0.12.4. –  moeffju Mar 27 at 16:45
add comment

It looks as if the gem spec command will fail with an error if the named gem is not installed. So:

if ! gem spec <name> > /dev/null 2>&1; then
  echo "Gem <name> is not installed!"
fi

I don't know if this is the canonical way of solving this, but it works.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.