4

I have some gems installed, and I'm trying to use them in a Ruby app:

require 'rubygems'
require 'mygem'

When I run the app, though, I get this error: <internal:lib/rubygems/custom_require>:29:inrequire': no such file to load -- mygem (LoadError)`

But if I try requiring the gem inside irb (making sure to `require 'rubygems' first), it works fine. What am I supposed to do? I tried googling for this problem, but didn't understand.

Running a which on ruby, gem, and irb shows that they're all in /opt/local/bin/, i.e.,

> which ruby
/opt/local/bin/ruby
> which gem
/opt/local/bin/gem
> which irb
/opt/local/bin/irb

Update to answer the questions posed (yep, irb and ruby are pointing to different folders):

$LOAD_PATH and $: in irb both contain seem to be pointing to ruby 1.8 folders:

/opt/local/lib/ruby/site_ruby/1.8
/opt/local/lib/ruby/site_ruby/1.8/i686-darwin10
/opt/local/lib/ruby/site_ruby
/opt/local/lib/ruby/vendor_ruby/1.8
/opt/local/lib/ruby/vendor_ruby/1.8/i686-darwin10
/opt/local/lib/ruby/vendor_ruby
/opt/local/lib/ruby/1.8
/opt/local/lib/ruby/1.8/i686-darwin10
.

$: in ruby points to ruby 1.9.1 folders:

/usr/local/lib/ruby/site_ruby/1.9.1
/usr/local/lib/ruby/site_ruby/1.9.1/i386-darwin9.8.0
/usr/local/lib/ruby/site_ruby
/usr/local/lib/ruby/vendor_ruby/1.9.1
/usr/local/lib/ruby/vendor_ruby/1.9.1/i386-darwin9.8.0
/usr/local/lib/ruby/vendor_ruby
/usr/local/lib/ruby/1.9.1
/usr/local/lib/ruby/1.9.1/i386-darwin9.8.0

gem env shows

RubyGems Environment:
  - RUBYGEMS VERSION: 1.4.1
  - RUBY VERSION: 1.8.7 (2010-12-23 patchlevel 330) [i686-darwin10]
  - INSTALLATION DIRECTORY: /opt/local/lib/ruby/gems/1.8
  - RUBY EXECUTABLE: /opt/local/bin/ruby
  - EXECUTABLE DIRECTORY: /opt/local/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-darwin-10
  - GEM PATHS:
     - /opt/local/lib/ruby/gems/1.8
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
     - "gempath" => ["/opt/local/lib/ruby/gems/1.8"]
     - :sources => ["http://rubygems.org/", "http://gems.github.com", "http://gems.github.com"]
  - REMOTE SOURCES:
     - http://rubygems.org/
     - http://gems.github.com
     - http://gems.github.com

Gem.path in irb points to

/Users/grautur/.gem/ruby/1.8
/usr/local/lib/ruby/gems/1.8

Gem.path in ruby points to

/Users/grautur/.gem/ruby/1.9.1
/usr/local/lib/ruby/gems/1.9.1
grautur
  • 29,955
  • 34
  • 93
  • 128
  • What do your `$LOAD_PATH` or `$:` variables contain? Examining those in each environment will probably be fairly informative. – Damien Wilson May 31 '11 at 23:17
  • Hmm, irb seems to be using ruby 1.8 (`$LOAD_PATH` and `$:` are both /opt/local/lib/ruby/site_ruby/1.8,/opt/local/lib/ruby/site_ruby/1.8/i686-darwin10,/opt/local/lib/ruby/site_ruby,/opt/local/lib/ruby/vendor_ruby/1.8,/opt/local/lib/ruby/vendor_ruby/1.8/i686-darwin10,/opt/local/lib/ruby/vendor_ruby,/opt/local/lib/ruby/1.8,/opt/local/lib/ruby/1.8/i686-darwin10,.), while ruby is using ruby 1.9.1 (`$LOAD_PATH` is empty, `$:` is the 1.9.1 versions of the previous paths). – grautur Jun 01 '11 at 03:21

4 Answers4

2

I'm not sure what's going on. However, the following may help.

In irb, do

require 'rubygems'
require 'mygem'
puts $:

and then, in ruby, do

require 'rubygems'
puts $:

and show us what you get if you haven't worked it out.

Edit: also print out the results of doing gem env on the command line.

Edit 2: See what happens if you type in puts Gem.path after you've required rubygems in both irb and ruby. See thanks to Matt for describing Rubygems

Community
  • 1
  • 1
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
1

You will have to add gem install mygem in your Gamefile and then run bundle install command. Your application will work correctly after doing this.

skuntsel
  • 11,624
  • 11
  • 44
  • 67
1

You may try to add gem 'mygem' before the require, but that should not be necessary.

Michaël Witrant
  • 7,525
  • 40
  • 44
0

I had a similar problem. The solution I found eventually was to setup rvm (ruby version manager) on my system and use it to setup a new ruby environment. it also makes it easy to switch between ruby versions of sets of gems.

Community
  • 1
  • 1
Nat
  • 2,689
  • 2
  • 29
  • 35