Gem found in irb, not in Ruby - ruby

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

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

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

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.

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.

Related

Rubygems can't find sinatra directly after install

I can't figure out why rubygems is unable to find sinatra on the require "sinatra" line in the following code. I've tried to write the ruby to ensure that sinatra is installed..
Ruby
$:.push("/home/xxxx/ruby/gems")
require 'rubygems'
begin
gem "sinatra"
rescue LoadError
system("gem install sinatra")
Gem.clear_paths
end
require 'sinatra'
get "/" do
"Hello, world!"
end
Error - on line: require 'sinatra'
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require': no such file to load -- sinatra (LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
I'm running the script through SSH with the bash command 'ruby app.rb', if that's relevant.
Gem env returns: (where /home/xxxx/ruby/gems is the correct location of the gems)
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.25
- RUBY VERSION: 1.8.7 (2012-06-29 patchlevel 370) [x86_64-linux]
- INSTALLATION DIRECTORY: /home/xxxx/ruby/gems
- RUBY EXECUTABLE: /usr/bin/ruby
- EXECUTABLE DIRECTORY: /home/xxxx/ruby/gems/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /home/xxxx/ruby/gems
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- "gempath" => []
- "gem" => "--remote --gen-rdoc --run-tests"
- "rdoc" => "--inline-source --line-numbers"
- "gemhome" => "/home/xxxx/ruby/gems"
- REMOTE SOURCES:
- http://rubygems.org/
I would recommend using bundler.
First, gem install bundler on both your dev machine and on the server.
Next create a file named Gemfile which contains something like this:
source "https://rubygems.org"
gem "sinatra"
# add a "gem" line for any other gems your application needs
(The command bundle init will create a skeleton Gemfile for you).
Then you do a "bundle install" to install the gems. This will create a "Gemfile.lock" file which gives the exact version of each gem. That file gets checked into your version control along with the Gemfile.
Once that code has been distributed to the server, then "bundle install" on the server to get the exact same gems installed there.
In your application, do not:
require "rubygems"
Modify $:
Install missing gems
On the server where you want to run your application, instead of just ruby app, you will run bundle exec ruby app.

ruby - require & cannot load such file

I'm new to ruby and I have a problem loading gems.
I've read every topic about this on SO but I couldn't figure out how to make it work :/
I'm on a fresh install of Ruby 1.9.3 and RubyGems 1.8.11
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.11
- RUBY VERSION: 1.9.3 (2011-10-30 patchlevel 0) [i386-mingw32]
- INSTALLATION DIRECTORY: D:/dev/Ruby/lib/ruby/gems/1.9.1
- RUBY EXECUTABLE: D:/dev/Ruby/bin/ruby.exe
- EXECUTABLE DIRECTORY: D:/dev/Ruby/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-mingw32
- GEM PATHS:
- D:/dev/Ruby/lib/ruby/gems/1.9.1
- D:/aoi/.gem/ruby/1.9.1
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
I've installed the gem twice, once from command line
gem install soap4r
And second time i've tried to use RubyMine installer, but the result where the same,
when I try to
require 'rubygems'
resuire 'soap'
The output is the same :
LoadError: cannot load such file -- soap
from D:/dev/Ruby/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from D:/dev/Ruby/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from (irb):1
from D:/dev/Ruby/bin/irb:12:in `<main>'
Thank you for your help.
Get this https://github.com/spox/soap4r-spox and patch your ruby with it, its the default soap module for ruby but was pulled out of 1.9 versions
Try to add this to the top of your file:
gem 'soap4r'
require 'soap/wsdlDriver'
Or as an alternative try Savon
Have you checked that you do not run multiple different ruby versions ? Then your gems are not bound to the correct ruby version.
Moreover, I notice something strange :
http://rubygems.org/gems/soap : "This gem has been yanked, but it is still available for download for other gems that may have depended on it"
On the contrary, there seems to be another more interesting gem around : soap4r.
There is a tuto here. I especially noticed that sort of lines :
require "soap/rpc/standaloneserver"
That means "require soap" may not be sufficient in your case.

Ruby gem environment issue - LoadError: no such file to load -- robots

I'm attempting to write a crawler using the anemone gem, which requires the robots gem. For whatever reason, robots absolutely will not include. Here is some of my environment info:
$ gem list -d robots
*** LOCAL GEMS ***
robots (0.10.1)
Author: Kyle Maxwell
Homepage: http://github.com/fizx/robots
Installed at: /usr/local/lib/ruby/gems/1.9.1
Simple robots.txt parser
$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.10
- RUBY VERSION: 1.9.2 (2011-02-18 patchlevel 180) [x86_64-darwin10.7.0]
- INSTALLATION DIRECTORY: /usr/local/lib/ruby/gems/1.9.1
- RUBY EXECUTABLE: /usr/local/bin/ruby
- EXECUTABLE DIRECTORY: /usr/local/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-10
- GEM PATHS:
- /usr/local/lib/ruby/gems/1.9.1
- /Users/ryan/.gem/ruby/1.9.1
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
$ gem which robots
/usr/local/lib/ruby/gems/1.9.1/gems/robots-0.10.1/lib/robots.rb
Any ideas? All other gems load correctly, I've never had this problem before. Note that I am using ruby version 1.9, so rubygems is implicitly required. Adding
require 'rubygems'
...to the front of a script returns false, since the file is already included, and does not help the situation. Any ideas?
EDIT: Posting examples of failing code. Please note that rubygems returning false does not mean rubygems cannot load - rather that it has already been loaded. See this post: http://www.ruby-forum.com/topic/157442
$ irb
irb(main):001:0> require 'rubygems'
=> false
irb(main):002:0> require 'active_record'
=> true
irb(main):003:0> require 'mechanize'
=> true
irb(main):004:0> require 'robots'
LoadError: no such file to load -- robots
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:59:in `require'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:59:in `rescue in require'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
from (irb):4
from /usr/local/bin/irb:12:in `<main>'
irb(main):005:0>
It looks like the gem has been created with the wrong permissions; there's a bug opened for this on github.
Changing the permissions with
sudo chmod a+r /usr/local/lib/ruby/gems/1.9.1/gems/robots-0.10.1/lib/robots.rb
should fix it, but watch out for other permission issues. You might be better with
sudo chmod -R a+r /usr/local/lib/ruby/gems/1.9.1/gems/robots-0.10.1
to recursively make all the files in the gem readable.
The robots.rb file (and some others) is being installed with the permissions -rw-rw----, so anyone using a local install of rvm or similar where gems are installed as the local user won't have been affected by this.

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.
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.
Make sure that you require it in your Gemfile and do a bundle install.
Edit - Try requiring rubygems, then nokogiri.

How can I configure gem to only ever use system gems?

I want to configure gem to only use system gems - never those in $HOME/.gem. This is because I'm writing a script that will access Gem.path and I don't want it to return the path to gems in my home directory.
I'm pretty sure I haven't explicitly set GEM_HOME or anything like that in my .bashrc, .bash_login etc.
But Gem.path returns my homedir gems first:
irb
> Gem.path
=> ["/home/nfm/.gem/ruby/1.9.1", "/usr/local/lib/ruby/gems/1.9.1"]
Can I stop this from happening? Where is it configured? Or is it just the default to look in homedir first?
If I can't configure this, can I return the system path for gems with regexp hackery?
More details:
which ruby
/usr/local/bin/ruby
ruby --version
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]
gem env
- RUBYGEMS VERSION: 1.3.7
- RUBY VERSION: 1.9.2 (2010-08-18 patchlevel 0) [x86_64-linux]
- INSTALLATION DIRECTORY: /usr/local/lib/ruby/gems/1.9.1
- RUBY EXECUTABLE: /usr/local/bin/ruby
- EXECUTABLE DIRECTORY: /usr/local/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /usr/local/lib/ruby/gems/1.9.1
- /home/nfm/.gem/ruby/1.9.1
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
Update:
So apparently this can be configured in ~/.gemrc:
# Note the use of a symbol before the colon - the string version doesn't work!
:gempath:
- /usr/local/lib/ruby/gems/1.9.1
However, this does not seem to take effect if you fire up irb. This has something to do with the fact that the config file is YAML, and apparently yaml isn't loaded when irb starts (not sure on this one!):
$ irb
> Gem.path
=> ["/home/nfm/.gem/ruby/1.9.1", "/usr/local/lib/ruby/gems/1.9.1"]
> Gem.configuration.path
=> ["/usr/local/lib/ruby/gems/1.9.1"]
# Ready for a WTF moment?
> Gem.path
=> ["/usr/local/lib/ruby/gems/1.9.1"]
So, the answer below seems like the only consistent way to get the correct behavior, even though you'd assume that ~/.gemrc would work and would be a nicer way to wrap you config up.
However, setting :gempath: in my ~/.gemrc worked in the context of the script being in my Rakefile in a Rails app, presumably because yaml is explicitly loaded.
Not sure exactly what's going on with yaml, but this explanation seems consistent with what I'm seeing here.
Mod up! :P
overwrite it here:
#in ~/.bashrc
export GEM_PATH=/usr/local/lib/ruby/gems/1.9.1

Resources