【问题标题】:Rubygems can't find sinatra directly after installRubygems 安装后无法直接找到 sinatra
【发布时间】:2014-09-07 07:13:48
【问题描述】:

我无法弄清楚为什么 ruby​​gems 无法在以下代码的 require "sinatra" 行中找到 sinatra。我已经尝试编写 ruby​​ 以确保已安装 sinatra..

红宝石

$:.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

错误 - 在线:需要 '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'

如果相关的话,我正在使用 bash 命令“ruby app.rb”通过 SSH 运行脚本。

Gem env 返回:(其中 /home/xxxx/ruby/gems 是 gem 的正确位置)

    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/

【问题讨论】:

  • 如果您从 ruby​​ 脚本安装 gem,您可能会将其安装在不同的文件夹中。你应该检查/home/xxxx/ruby/gems 看看它是否真的存在。
  • 这个文件夹中有一个安装/home/xxxx/ruby/gems/gems/sinatra-1.4.5 但我刚刚卸载了它,脚本并没有替换它。我认为你是对的。 $:.push("/home/xxxx/ruby/gems") 肯定是在设置 gems 文件夹??有小费吗?谢谢
  • 尝试从命令行运行gem install sinatra,然后运行一个只需要sinatra的示例ruby脚本,并执行一个简单的get
  • 刚试过,脚本是:$:.push("/home/xxxx/ruby/gems") require 'rubygems' require 'sinatra',我得到了同样的错误。
  • 你在使用捆绑软件吗?

标签: ruby rubygems


【解决方案1】:

我会推荐使用捆绑器。

首先,gem install bundler 在您的开发机器和服务器上。

接下来创建一个名为 Gemfile 的文件,其中包含如下内容:

source "https://rubygems.org"

gem "sinatra"
# add a "gem" line for any other gems your application needs

(命令bundle init 将为您创建一个骨架Gemfile)。

然后您执行“捆绑安装”来安装 gem。这将创建一个“Gemfile.lock”文件,该文件给出每个 gem 的确切版本。该文件与 Gemfile 一起被检入您的版本控制中。

将代码分发到服务器后,然后在服务器上“捆绑安装”以在服务器上安装完全相同的 gem。

在您的应用程序中,不要

  • 需要“红宝石”
  • 修改$:
  • 安装缺失的 gems

在您要运行应用程序的服务器上,您将运行bundle exec ruby app,而不仅仅是ruby app

【讨论】:

  • 谢谢。我敢肯定这是一个非常愚蠢的问题,但是您如何称呼 gemfile? '宝石文件'? 'gemfile.rb'?
  • 我按照您的指南操作,bundle installCould not find bundler 并列出了不在我的 gem 文件夹中的 gem。但是gem query --local 返回正确的宝石列表。 bundle 是否以某种方式指向不同的安装? (我无法完全控制服务器)
  • @Ian 需要安装 bundler gem;我已经编辑了答案。
  • 我找到了解决方案..!当我使用 SSH 时,我需要将这一行添加到我的 .bash_profile 中:export GEM_PATH=/home/xxxx/ruby/gems/:${GEM_PATH} 感谢您的指点,您肯定有帮助
  • @Ian 杰出!您可能会考虑将其添加为答案并打勾。然后有相同症状的其他人可以很快看到您是如何解决问题的。
猜你喜欢
  • 2011-11-10
  • 2012-05-13
  • 2013-11-20
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2019-03-08
  • 2019-10-08
  • 2010-10-04
相关资源
最近更新 更多