问题
I'm trying to get some gems working on a web-host which supports ruby and some ruby gems, but not some of the ones I need to use.
Following the instructions I found here, I kept the original gem location in my gem path, and added my own at /path/to/my/home/gems to ~/.gemrc
gemhome: /users/home/myuser/gems
gempath:
- /usr/local/lib/ruby/gems/1.8
- /users/home/mysuser/gems
I downloaded some gems manually and did installs using the --local and --install-dir options of the gem command. I then did a "gem list", and can in fact see the gems I installed in my user directory, as well as the original gems in the normal system path.
If I kick on IRB or do a ruby -e, all the system gems work fine. However, I can't get my user directory installed gems loaded:
$ ruby -r rubygems -e "require 'nokogiri'"
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require__': no such file to load -- nokogiri (LoadError)
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'
from -e:1
I even tried the old school require_gem:
$ irb -r rubygems
irb(main):001:0> require_gem 'nokogiri'
Gem::LoadError: Could not find RubyGem nokogiri (> 0.0.0)
from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:204:in `report_activate_error'
from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:141:in `activate'
from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:37:in `require_gem_with_options'
from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:31:in `require_gem'
from (irb):1
From my "gems list" I can see:
nokogiri (1.3.3)
Nokogiri is an HTML, XML, SAX, and Reader parser
I have this same problem with all the user-directory install gems. What am I missing here?
EDIT: This is not a rails app. This is for a command line-application. The program will be executed by my user account, just like the tests shown above in.
EDIT: Per Rafe's suggestion, I looked at Gem.path from IRB. It only lists the system gem path, /usr/local/lib/ruby/gems/1.8, and not my user dir path as well.
回答1:
I had the same problem, I fixed it by adding the following at the top of the ruby file
require 'rubygems'
Hope that helps
回答2:
My current working (but less than ideal) solution is to do a
Gem.path.push "/path/to/my/gems"
I say less than ideal because this is what I expected the contents of the .gemrc to do for me automatically, for all ruby executions under my users environment. Not sure why it doesn't - but the above at least works.
回答3:
I imagine the problem is that the Web application is not using the same configuration that your user account is, probably because it's running as another user. One option is to load the gems into vendor/gems
in your application.
You can do that your local machine using rake gems:unpack
, check those gems into version control, and then deploy them on the server. That is, to me, the most reliable approach. Alternatively, you can change the configuration of your application to look for gems in your home directory.
回答4:
I have been wrestling with this and have determined that the .gemrc doesn't appear to be loaded in all cases. For example, it is honored when installing gems, but not necessarily when running irb or script/console.
I also found that that path to the local directory needed to be different than what I have seen described on many sites. The following works for me:
---
gem: --no-ri --no-rdoc
gemhome: /home/.gem/ruby/1.8
gempath:
- /home/.gem/ruby/1.8
- /usr/lib/ruby/gems/1.8
回答5:
I've had a similar problem but the accepted answer here is apparently not the way to go (at least it wasn't for me). If possible, you shouldn't do this in the gem files:
require 'rubygems'
Why not you ask ? Look at this article: http://tomayko.com/writings/require-rubygems-antipattern
In any case, you should be fine by just requiring 'rubygems' when calling irb:
> irb -r 'rubygems'
and you should have the same result. Not sure why for this question, this method doesn't work.
来源:https://stackoverflow.com/questions/1260561/ruby-gem-listed-but-wont-load-gem-in-user-dir-not-ruby-dir