Using a gem without installing it

断了今生、忘了曾经 提交于 2020-01-02 02:10:06

问题


I need to run a bunch of ruby scripts that I have written on a server that I don't have sudo access to.
On my own machine, I have installed a bunch of gems using 'sudo gem install ..' and used them in my code..
Is there any mechanism which would let me use these gems without formally installing them on a remote machine?


回答1:


You can install them in a user directory: http://docs.rubygems.org/read/chapter/3#page83

This looks like it lets you install the gems anywhere you want.




回答2:


You can, but it's tricky.

First, install them using the --install-dir option, i.e.:

gem install gem_name --install-dir /some/directory/you/can/write/to

Second, make sure you have a .gemrc file in your home directory that looks something like this:

gemhome: /some/directory/you/can/write/to
gempath:
 - /some/directory/you/can/write/to
 - /usr/local/lib/ruby/gems/1.8

gemhome is where gems should look first when seeking a gem. gempath is all the paths it should check in when seeking a gem. So in the .gemrc above, I'm telling my code to look first in the local directory, and if not found, check the system gem directory.

Third, be aware that some code - even code within gems - can make assumptions about where gems are located. Some code may programmatically alter gempath or gemhome. You may need to "alter it back" in your own code.

There's not a lot (read: no) documentation on how to do that - the best way to figure it out is to read the tests that are included with the RubyGems source. Here's how I hack the gem paths in a rake task to point to my frozen version of capistrano:

  Gem.use_paths(Gem.dir, ["#{RAILS_ROOT}/vendor/gems"])
  Gem.refresh # picks up path changes


来源:https://stackoverflow.com/questions/913153/using-a-gem-without-installing-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!