Can't find gems in irb: "NameError: uninitialized constant Gem from (irb)"

Bryan Locke picture Bryan Locke · Nov 6, 2009 · Viewed 10.3k times · Source

When I run a Rails app it finds all of my installed gems correctly.

This is the first time I tried to call some gems from inside irb and it couldn't find them.

blocke:~$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'rails'
LoadError: no such file to load -- rails
 from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
 from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
 from (irb):2
irb(main):003:0> 

When I run "gem environment" this is what I get:

blocke:~$ gem environment
RubyGems Environment:
  - RUBYGEMS VERSION: 1.3.5
  - RUBY VERSION: 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
  - INSTALLATION DIRECTORY: /usr/lib/ruby/gems/1.8
  - RUBY EXECUTABLE: /usr/bin/ruby1.8
  - EXECUTABLE DIRECTORY: /usr/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-linux
  - GEM PATHS:
     - /usr/lib/ruby/gems/1.8
     - /home/blocke/.gem/ruby/1.8
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
     - "gem" => "--no-ri --no-rdoc"
     - :sources => ["http://gemcutter.org", "http://gems.rubyforge.org/", "http://gems.github.com"]
  - REMOTE SOURCES:
     - http://gemcutter.org
     - http://gems.rubyforge.org/
     - http://gems.github.com

According to irb, the gem path is the same except in the reverse order:

blocke:~$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> Gem.path
=> ["/home/blocke/.gem/ruby/1.8", "/usr/lib/ruby/gems/1.8"]

Answer

stask picture stask · Nov 6, 2009

It doesn't work because it cannot find 'rails.rb' file (I think there is no such file in rails gem).

You can either require specific rails component, like 'activerecord':

require 'rubygems'
require 'activerecord'

Or use 'gem' command, like this:

require 'rubygems'
gem 'rails'