Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gem loads in irb but not console

This one is driving me nuts. I can load a gem via irb:

steve@server:/var/www/listings$ irb
irb(main):001:0> Gem.path
=> ["/home/steve/.gem/ruby/1.9.1", "/usr/local/ruby/lib/ruby/gems/1.9.1"]
irb(main):002:0> require 'nokogiri'
=> true

But I can't load it through the rails console:

irb(main):001:0> Gem.path
=> ["/home/steve/.gem/ruby/1.9.1", "/usr/local/ruby/lib/ruby/gems/1.9.1"]
irb(main):002:0> require 'nokogiri'
=> false

The gem (nokogiri) is installed

steve@server:/var/www/listings$ gem which nokogiri
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/nokogiri-1.4.3.1/lib/nokogiri.rb

And bundle agrees

steve@server:/var/www/listings$ bundle show nokogiri
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/nokogiri-1.4.3.1

But, of course, rake spec fails with

/usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:304:in `rescue in depend_on': No such file to load -- Nokogiri (LoadError)

Other environment info:

steve@server:/var/www/listings$ ruby --version
ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]
steve@server:/var/www/listings$ rails --version
Rails 3.0.1
steve@server:/var/www/listings$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 1.3.7
  - RUBY VERSION: 1.9.2 (2010-08-18 patchlevel 0) [i686-linux]
  - INSTALLATION DIRECTORY: /usr/local/ruby/lib/ruby/gems/1.9.1
  - RUBY EXECUTABLE: /usr/local/ruby/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/local/ruby/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-linux
  - GEM PATHS:
     - /usr/local/ruby/lib/ruby/gems/1.9.1
     - /home/steve/.gem/ruby/1.9.1
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/

Any suggestions??

Edit

By the way, Nokogiri is in the Gemfile and bundle install completes without complaint.

This is a project I'm moving from windows to Ubuntu. On windows it's working fine (oddly enough!) so I'm pretty sure it's an environment thing.

like image 235
lambinator Avatar asked Nov 06 '10 23:11

lambinator


People also ask

What does gem command do?

The gem command will simply tell it which version of the gem you'd like to load, basically converting the gem name and version # to a load path (from my understanding). You still need to require the library.

How does gem install work?

What does gem install do? gem install , in its simplest form, does something kind of like this. It grabs the gem and puts its files into a special directory on your system. You can see where gem install will install your gems if you run gem environment (look for the INSTALLATION DIRECTORY: line):


2 Answers

require returns false if that file has already been loaded. Try it out in your irb session by performing the require statement twice in a row. The second one will return false:

irb(main):001:0> require 'nokogiri'
=> true
irb(main):002:0> require 'nokogiri'
=> false

If the file could not be found, require will raise a LoadError instead.

Your exception message (No such file to load -- Nokogiri), makes it seem like something is requiring 'Nokogiri' instead of 'nokogiri', which might be a problem on a case-sensitive operating system.

like image 76
jason.rickman Avatar answered Nov 15 '22 06:11

jason.rickman


Make sure that you require it in your Gemfile and do a bundle install.

Edit - Try requiring rubygems, then nokogiri.

like image 40
tsimon Avatar answered Nov 15 '22 05:11

tsimon