`require&#39;:无法在服务器上加载此类文件 - <gemname>非rails应用程序

时间:2017-02-02 14:04:19

标签: ruby require

我有一个非rails应用程序,我正在尝试部署。它使用eventmachine和守护进程。我使用mina进行部署,服务器配置为使用RVM。

当我尝试执行

ubuntu@ip-999-99-9-99:/var/www/myapp/current$ ruby bin/run_control.rb start

未能说明

/usr/local/rvm/rubies/ruby-2.2.0/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- daemons (LoadError)
from /usr/local/rvm/rubies/ruby-2.2.0/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from bin/run_control.rb:1:in `<main>'

该文件的内容为

ubuntu@ip-999-99-9-99:/var/www/myapp/current$ cat bin/run_control.rb
require 'daemons'

Daemons.run('bin/run.rb')

RVM信息

ruby-2.2.0:

system:
uname:       "Linux ip-999-99-9-99 4.4.0-53-generic #74-Ubuntu SMP Fri Dec 2 15:59:10 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux"
system:      "ubuntu/16.04/x86_64"
bash:        "/bin/bash => GNU bash, version 4.3.46(1)-release (x86_64-pc-linux-gnu)"
zsh:         " => not installed"

rvm:
version:      "rvm 1.28.0 (latest) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]"
updated:      "1 day 7 hours 3 minutes 31 seconds ago"
path:         "/usr/local/rvm"

ruby:
interpreter:  "ruby"
version:      "2.2.0p0"
date:         "2014-12-25"
platform:     "x86_64-linux"
patchlevel:   "2014-12-25 revision 49005"
full_version: "ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-linux]"

homes:
gem:          "/usr/local/rvm/gems/ruby-2.2.0"
ruby:         "/usr/local/rvm/rubies/ruby-2.2.0"

binaries:
ruby:         "/usr/local/rvm/rubies/ruby-2.2.0/bin/ruby"
irb:          "/usr/local/rvm/rubies/ruby-2.2.0/bin/irb"
gem:          "/usr/local/rvm/rubies/ruby-2.2.0/bin/gem"
rake:         "/usr/local/rvm/rubies/ruby-2.2.0/bin/rake"

environment:
PATH:         "/usr/local/rvm/gems/ruby-2.2.0/bin:/usr/local/rvm/gems/ruby-2.2.0@global/bin:/usr/local/rvm/rubies/ruby-2.2.0/bin:/usr/local/rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
GEM_HOME:     "/usr/local/rvm/gems/ruby-2.2.0"
GEM_PATH:     "/usr/local/rvm/gems/ruby-2.2.0:/usr/local/rvm/gems/ruby-2.2.0@global"
MY_RUBY_HOME: "/usr/local/rvm/rubies/ruby-2.2.0"
IRBRC:        "/usr/local/rvm/rubies/ruby-2.2.0/.irbrc"
RUBYOPT:      ""
gemset:       ""

宝石安装在

:/var/www/myapp/shared/bundle/ruby/2.2.0/gems$ ls
aws-sdk-2.1.1            colorize-0.7.7      i18n-0.7.0                 multi_json-1.11.1  rspec-3.2.0               rspec-support-3.2.1
aws-sdk-core-2.1.1       daemons-1.2.4       jmespath-1.0.2         net-scp-1.2.1      rspec-core-3.2.0          sshkit-1.7.1
aws-sdk-resources-2.1.1  diff-lcs-1.2.5      mina-0.3.4             net-ssh-2.9.2      rspec-expectations-3.2.0
capistrano-3.4.0         eventmachine-1.0.7  mina-multistage-1.0.2  open4-1.3.4        rspec-mocks-3.2.0

mina部署脚本为

require 'mina/bundler'
require 'mina/git'
require 'mina/rvm'    # for rvm support. (http://rvm.io)

set :rvm_path, '/usr/local/rvm/scripts/rvm'

task :environment do
  invoke :'rvm:use[ruby-2.2.0@default]'
end

desc "Deploys the current version to the server."
task :deploy => :environment do
  deploy do
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'deploy:cleanup'

    to :launch do
      queue "mkdir -p #{deploy_to}/#{current_path}/tmp/"
      queue "touch #{deploy_to}/#{current_path}/tmp/restart.txt"
      invoke :'start_event_machine_server'
    end
  end
end

task :start_event_machine_server do
  queue 'cd /var/www/myapp/current && ruby bin/run_control.rb start'
end

1 个答案:

答案 0 :(得分:2)

当您使用Bundler在Ruby的全球宝石商店外安装宝石时,您还需要调整$LOAD_PATH以包含此自定义位置,然后才能要求宝石。

实现此目的的最常用方法是使用bundle exec。此命令随捆绑程序一起提供,并调整Ruby $lOAD_PATH以包含Gemfile中指定的所有gem的位置。

当您按如下方式启动脚本时,Ruby应该能够找到您的宝石(假设您的bin/run_control.rb脚本是可执行的。)

bundle exec bin/run_control.rb start