WEBrick CGI handler pointing to wrong gem location - ruby

I am trying to execute my CGI program from WEBrick CGIhandler and kept failing with 'loaderror' of gem libraries. Is someone seen following behavior before? How could I get my CGI scripts to search valid gem location?
[Situaltion]
When I ran CGI scripts that requires 'mysql2' library via Webrick server, I got a following error:
ERROR CGIHandler: /home/charles/code/svr/lib/cgitest.rb:
/home/charles/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- mysql2/version (LoadError)
Comment this particular 'require' out from cgitest.rb makes this script work so looks just failed to load this library. Added "puts Gem.path" to the script indicates CGIHandler is looking to following directories to find gem;
["/home/charles/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/gems/1.9.1", "/.gem/ruby/1.9.1"]
while actual GEM:HOME and GEM:PATH are following;
["/home/charles/.rvm/gems/ruby-1.9.3-p194", "/home/charles/.rvm/gems/ruby-1.9.3-p194#global"]
I think I am missing something very simple but could not figure them out. Really appreciate if someone lead me to the right direction. Any input would be highly welcome.
[Environment]
Using RVM to install Ruby 1.9.3 to Ubuntu Server 12.4 with bunch of gems including "mysql2" library. And the script can load 'mysql2' when I run from local.
$which ruby
/home/charles/.rvm/rubies/ruby-1.9.3-p194/bin/ruby
$irb
1.9.3-p194 :001 > Gem.path
=> ["/home/charles/.rvm/gems/ruby-1.9.3-p194", "/home/charles/.rvm/gems/ruby-1.9.3-
p194#global"]
1.9.3-p194 :002 > require 'mysql2'
=> true

You need to use wrapper instead of bin/ruby:
/home/charles/.rvm/wrappers/ruby-1.9.3-p194/ruby
This will ensure you load proper environment when starting ruby.

Related

FCGI Ruby Gem not found when using apache

I am using Apache with FastCGI to run a ruby application. I have installed the apache fcgi module and also the Ruby fcgi gem.
When I run the fcgi script 'search.fcgi' like so
ruby search.fcgi
It runs successfully. However when starting Apache I get the following error in my log file when it tries to run the same script:
/usr/local/rvm/rubies/ruby-2.1.8/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- fcgi (LoadError)
from /usr/local/rvm/rubies/ruby-2.1.8/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/user/fcgi/search.fcgi:13:in `<main>'
Line 13 in search.fcgi is simply
require 'fcgi'
It appears as though when running through Apache it is unable to detect the installed gem. I have loaded irb and and run require 'fcgi' which returns true.
Am I missing something here? Some path or config item I need to set?
My problem was caused by the fact that Ruby was installed using RVM. There was some issue with the script from apache accessing some parts of the RVM instillation. The solution was to remove the RVM ruby install and reinstall ruby from source.

Nokogiri Ruby 'require' Issues

I'm new to Ruby and I'm having a lot of trouble trying to use Nokogiri. I've been trying to find a resolution for hours now, so any help is appreciated. I tried searching for and using solutions from other related SO posts before caving and posting my own. When I run ruby -v I get: ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]
(Edit: I have updated ruby with updates-alternatives --config ruby and selected /usr/bin/ruby1.9.1 but when I do ruby -v it is now showing version 1.9.3 WTF am I doing wrong here?)
I have a new project directory at ~/workspace/ruby/rubycrawler/ and I used Bundler to install nokogiri, which installed correctly:
Using mini_portile (0.5.2)
Using nokogiri (1.6.1)
Using bundler (1.5.1)
Your bundle is complete!
Running bundle show nokogiri returns /var/lib/gems/1.9.1/gems/nokogiri-1.6.1.
In the directory I'm running the script from I have a simple html file named "index.html". The script I'm trying to run is even simpler (or so I thought):
require 'nokogiri'
page = Nokogiri::HTML(open("index.html"))
puts page.class # Nokogiri::HTML::Document
The error is rubycrawler.rb:1:in 'require': no such file to load -- nokogiri (LoadError).
I also added require 'rubygems' even though I read it isn't needed for 1.9+ and still no luck.
A lot of searching shows "Did you put this gem in your Gemfile?". So I generate a Gemfile and add gem 'nokogiri'. I try running the small script again and get the same error. I read "Try deleting Gemfile.lock." so I did but still couldn't get it to work. I then read to try testing it out in irb so I tested "open-uri" and "nokogiri" and here's what I got:
irb(main):001:0> require 'open-uri'
=> true
irb(main):003:0> require 'nokogiri'
LoadError: no such file to load -- nokogiri
I'm really having a lot of trouble figuring this out, so really any help at all is really appreciated.
Ruby tools like RVM, Bundler, etc., to the novice, appear to do a lot of magic, but really, there is no magic to them. The key here lies in what Bundler actually does for you. It manages a manifest of dependencies, BUT at runtime, those dependencies STILL have to get loaded somehow, and my gut feeling is that is what is not happening here.
Regardless of what version of Ruby you are using, if you are using Bundler, there's an easy way to do this. Precede the command that starts your program with "bundle exec" and that will make Bundler edit Ruby's load path so that it includes all the things in the manifest (Gemfile.lock).
For example:
$ bundle exec ruby foo.rb
A additional note for anyone using RVM: RVM generally will modify the shebangs in the scripts that launch programs like "ruby" or "rake" so that they use the "ruby_no_exec" shell (or similar) instead of the plain old "ruby" shell. That alternate shell is Bundler-aware and makes it generally unnecessary to type "bundle exec," but since the OP is using system Ruby, that's not applicable and commands should be manually prefixed with "bundle exec".
Hope this helps!
In addition to Kent's answer, I would recommend switching to RVM instead of using the system installed ruby. System rubies tend to be horribly out of date, especially when it comes to important things like features and security updates. It might not help you in your current situation, but it would be well worth the time. If you are unfamiliar: http://rvm.io

Ruby require 'some-gem' works in console, not in Eclipse?

Well, I'm trying to run a simple web server through "rack". So this is my program:
require 'rubygems'
require 'rack'
class HelloWorld
def call(env)
[200, {"Content-Type" => "text/html"}, ["Hello Rack!"]]
end
end
Rack::Handler::Mongrel.run HelloWorld.new, :Port => 9292
If I run it in the console, it works fine. If I run it in Eclipse, it ends up with error:
/Users/MY_SUPER_SECRET_USER/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- rack (LoadError)
from /Users/MY_SUPER_SECRET_USER/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/MY_SUPER_SECRET_USER/Sites/service/service.rb:2:in `<main>'
The one that is working is called like this:
MY_SUPER_SECRET_USER#MacBook-Pro:~/Sites/service $ which ruby
/Users/MY_SUPER_SECRET_USER/.rvm/rubies/ruby-1.9.3-p194/bin/ruby
MY_SUPER_SECRET_USER#MacBook-Pro:~/Sites/service $ ruby service.rb
Then when I try to open localhost:9292 it shows the expected "Hello Rack" code.
I'm on Mac OS X 10.8, with ruby 1.9.3 installed through rvm (this must be obvious). My "rack" package has been installed with sudo gem install rack
So as you can see, the Eclipse is configured with the very same ruby executable. Any suggestions will be of great help!
The line
custom_require.rb:36:in `require': cannot load such file -- rack (LoadError)
means it can't find where you installed the rack gem. Reading around the internet, I see again and again using sudo doesn't seem to work. Try just installing it and see if that fixes it.
$ gem install rack (no sudo)
I ran into this same problem, trying to require gems in Eclipse which it wasn't recognizing, even though everything seemed to be configured correctly (the gems were installed, Eclipse was pointing at the right Ruby interpreter, etc).
I ended up making it work by adding GEM_HOME and GEM_PATH variables to the Environment tab of Debug/Run Configurations.
More detailed answer here: https://stackoverflow.com/a/28419300/525338

Getting an error with Ruby/Selenium

I am trying to use Selenium with Ruby. I am new to Ruby, I set it up using this blog http://testnerdy.blogspot.ca/2009/10/installing-ruby-and-selenium-on-windows.html
and the Ruby script I used is from here - http://testnerdy.blogspot.ca/2009/10/running-selenium-tests-written-in-ruby.html
When I ran the script, I get this error
SeleniumRubyWindowsTest.rb:1:in `require': no such file to load -- selenium/client (LoadError)
from SeleniumRubyWindowsTest.rb:1
Any ideas on what I may be doing wrong?
Have you installed the selenium gem? If so, depending on your version of ruby you may need to do a
require 'rubygems'
as the first line of the script to pull in all the gem dependencies, which selenium could be one.

OSX Ruby Gems Add to ruby path?

I am just starting to learn ruby. It seems that the default gems install path is not part of Ruby. Is this normal behavior? How can I set the default ruby path? Example:
[11:53:33]wuntee:/Library/Ruby/Gems/1.8/gems/packetfu-1.0.0/examples$ sudo ruby arphood.rb
Fetching the oui.txt from IEEE, it'll be a second. Avoid this with arphood.rb <filename>.
arphood.rb:30:in `require': no such file to load -- packetfu (LoadError)
from arphood.rb:30:in `arp_everyone'
from arphood.rb:51
As you can see packetfu is installed in /Library/Ruby/Gems/1.8/gems/, but ruby cant find it...
that's because you're not in the directory where packetfu.rb file lies and there's no require 'rubygems' to add the gems paths in your script

Resources