【问题标题】:How to work around conflicts with Ruby versions and pg gem如何解决与 Ruby 版本和 pg gem 的冲突
【发布时间】:2020-06-19 23:30:49
【问题描述】:

我正在开发某人的网站,试图让 Rails 运行。发送给我的文件包括 gemfile,我得到了 gem 'pg', '0.21.0'. However, when I runbundler install`

Bundler could not find compatible versions for gem "ruby ":
  In Gemfile:
    ruby  (~> 2.6.6.0)

    pg (= 0.21.0) was resolved to 0.21.0, which depends on
      ruby  (>= 2.0.0)

    pg (= 0.21.0) was resolved to 0.21.0, which depends on
      ruby  (< 2.5, >= 2.0) x64-mingw32

此外,如果我在尝试失败后尝试运行 rails serverrails -v,我会得到

Could not find gem 'pg (= 0.21.0) x64-mingw32' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

我正在运行 Ruby 2.6.6p146 和 Rails 4.2.10,尽管我可以轻松更改 Rails 版本,而且似乎对此没有影响。 如果有帮助,我在 Windows 10 上。

看来我需要使用更新版本的 pg。当我尝试时(最早的兼容版本是 1.1.4)bundle install 效果很好。但随后,rails server 抛出

Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). (Gem::LoadError)

看来我陷入了这两个错误之间,根据我在 gemfile 中的 pg 版本,我一定会得到其中一个错误,并且两者都不允许我运行 rails server

有没有办法解决这个问题,最好是允许我在 gemfile 中保留 0.21.0 的解决方案?我也很高兴找到解决第二个错误的解决方案。

这是第二个错误的完整堆栈跟踪:

C:/Ruby26-x64/lib/ruby/2.6.0/x64-mingw32/etc.so: warning: already initialized constant Struct::Passwd
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/core_ext/object/duplicable.rb:111: warning: BigDecimal.new is deprecated; use BigDecimal() method instead.
C:/Ruby26-x64/lib/ruby/2.6.0/forwardable.rb:116: warning: already initialized constant Forwardable::VERSION
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/forwardable-1.3.1/lib/forwardable/version.rb:3: warning: previous definition of VERSION was here
C:/Ruby26-x64/lib/ruby/2.6.0/forwardable.rb:117: warning: already initialized constant Forwardable::FORWARDABLE_VERSION
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/forwardable-1.3.1/lib/forwardable/version.rb:4: warning: previous definition of FORWARDABLE_VERSION was here
=> Booting WEBrick
=> Rails 4.2.10 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
Traceback (most recent call last):
        56: from bin/rails:4:in `<main>'
        55: from bin/rails:4:in `require'
        54: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/commands.rb:17:in `<top (required)>'
        53: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
        52: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:75:in `server'
        51: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:75:in `tap'
        50: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:80:in `block in server'
        49: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/commands/server.rb:78:in `start'
        48: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/commands/server.rb:139:in `log_to_stdout'
        47: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rack-1.6.13/lib/rack/server.rb:337:in `wrapped_app'
        46: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/commands/server.rb:61:in `app'
        45: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rack-1.6.13/lib/rack/server.rb:209:in `app'
        44: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rack-1.6.13/lib/rack/server.rb:300:in `build_app_and_options_from_config'
        43: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rack-1.6.13/lib/rack/builder.rb:40:in `parse_file'
        42: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rack-1.6.13/lib/rack/builder.rb:49:in `new_from_string'
        41: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rack-1.6.13/lib/rack/builder.rb:49:in `eval'
        40: from C:/Users/iijof/Desktop/Jobs/You3dit/y3dialpha/config.ru:in `<main>'
        39: from C:/Users/iijof/Desktop/Jobs/You3dit/y3dialpha/config.ru:in `new'
        38: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rack-1.6.13/lib/rack/builder.rb:55:in `initialize'
        37: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rack-1.6.13/lib/rack/builder.rb:55:in `instance_eval'
        36: from C:/Users/iijof/Desktop/Jobs/You3dit/y3dialpha/config.ru:3:in `block in <main>'
        35: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `require'
        34: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:240:in `load_dependency'
        33: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `block in require'
        32: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `require'
        31: from C:/Users/iijof/Desktop/Jobs/You3dit/y3dialpha/config/environment.rb:5:in `<top (required)>'
        30: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/railtie.rb:194:in `method_missing'
        29: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/railtie.rb:194:in `public_send'
        28: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/application.rb:352:in `initialize!'
        27: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/initializable.rb:54:in `run_initializers'
        26: from C:/Ruby26-x64/lib/ruby/2.6.0/tsort.rb:205:in `tsort_each'
        25: from C:/Ruby26-x64/lib/ruby/2.6.0/tsort.rb:226:in `tsort_each'
        24: from C:/Ruby26-x64/lib/ruby/2.6.0/tsort.rb:347:in `each_strongly_connected_component'
        23: from C:/Ruby26-x64/lib/ruby/2.6.0/tsort.rb:347:in `call'
        22: from C:/Ruby26-x64/lib/ruby/2.6.0/tsort.rb:347:in `each'
        21: from C:/Ruby26-x64/lib/ruby/2.6.0/tsort.rb:349:in `block in each_strongly_connected_component'
        20: from C:/Ruby26-x64/lib/ruby/2.6.0/tsort.rb:431:in `each_strongly_connected_component_from'
        19: from C:/Ruby26-x64/lib/ruby/2.6.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
        18: from C:/Ruby26-x64/lib/ruby/2.6.0/tsort.rb:228:in `block in tsort_each'
        17: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/initializable.rb:55:in `block in run_initializers'
        16: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/initializable.rb:30:in `run'
        15: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/initializable.rb:30:in `instance_exec'
        14: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activerecord-4.2.10/lib/active_record/railtie.rb:118:in `block in <class:Railtie>'
        13: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
        12: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:27:in `each'
        11: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
        10: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
         9: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
         8: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activerecord-4.2.10/lib/active_record/railtie.rb:122:in `block (2 levels) in <class:Railtie>'
         7: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activerecord-4.2.10/lib/active_record/connection_handling.rb:50:in `establish_connection'
         6: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/connection_specification.rb:175:in `spec'
         5: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `require'
         4: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:240:in `load_dependency'
         3: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `block in require'
         2: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `require'
         1: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/postgresql_adapter.rb:16:in `<top (required)>'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/rubygems_integration.rb:408:in `block (2 levels) in replace_gem': can't activate pg (~> 0.15), already activated pg-1.1.4-x64-mingw32. Make sure all dependencies are added to Gemfile. (Gem::LoadError)
        51: from bin/rails:4:in `<main>'
        50: from bin/rails:4:in `require'
        49: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/commands.rb:17:in `<top (required)>'
        48: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
        47: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:75:in `server'
        46: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:75:in `tap'
        45: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:80:in `block in server'
        44: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/commands/server.rb:78:in `start'
        43: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/commands/server.rb:139:in `log_to_stdout'
        42: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rack-1.6.13/lib/rack/server.rb:337:in `wrapped_app'
        41: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/commands/server.rb:61:in `app'
        40: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rack-1.6.13/lib/rack/server.rb:209:in `app'
        39: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rack-1.6.13/lib/rack/server.rb:300:in `build_app_and_options_from_config'
        38: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rack-1.6.13/lib/rack/builder.rb:40:in `parse_file'
        37: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rack-1.6.13/lib/rack/builder.rb:49:in `new_from_string'
        36: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rack-1.6.13/lib/rack/builder.rb:49:in `eval'
        35: from C:/Users/iijof/Desktop/Jobs/You3dit/y3dialpha/config.ru:in `<main>'
        34: from C:/Users/iijof/Desktop/Jobs/You3dit/y3dialpha/config.ru:in `new'
        33: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rack-1.6.13/lib/rack/builder.rb:55:in `initialize'
        32: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/rack-1.6.13/lib/rack/builder.rb:55:in `instance_eval'
        31: from C:/Users/iijof/Desktop/Jobs/You3dit/y3dialpha/config.ru:3:in `block in <main>'
        30: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `require'
        29: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:240:in `load_dependency'
        28: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `block in require'
        27: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:274:in `require'
        26: from C:/Users/iijof/Desktop/Jobs/You3dit/y3dialpha/config/environment.rb:5:in `<top (required)>'
        25: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/railtie.rb:194:in `method_missing'
        24: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/railtie.rb:194:in `public_send'
        23: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/application.rb:352:in `initialize!'
        22: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/initializable.rb:54:in `run_initializers'
        21: from C:/Ruby26-x64/lib/ruby/2.6.0/tsort.rb:205:in `tsort_each'
        20: from C:/Ruby26-x64/lib/ruby/2.6.0/tsort.rb:226:in `tsort_each'
        19: from C:/Ruby26-x64/lib/ruby/2.6.0/tsort.rb:347:in `each_strongly_connected_component'
        18: from C:/Ruby26-x64/lib/ruby/2.6.0/tsort.rb:347:in `call'
        17: from C:/Ruby26-x64/lib/ruby/2.6.0/tsort.rb:347:in `each'
        16: from C:/Ruby26-x64/lib/ruby/2.6.0/tsort.rb:349:in `block in each_strongly_connected_component'
        15: from C:/Ruby26-x64/lib/ruby/2.6.0/tsort.rb:431:in `each_strongly_connected_component_from'
        14: from C:/Ruby26-x64/lib/ruby/2.6.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
        13: from C:/Ruby26-x64/lib/ruby/2.6.0/tsort.rb:228:in `block in tsort_each'
        12: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/initializable.rb:55:in `block in run_initializers'
        11: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/initializable.rb:30:in `run'
        10: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/railties-4.2.10/lib/rails/initializable.rb:30:in `instance_exec'
         9: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activerecord-4.2.10/lib/active_record/railtie.rb:118:in `block in <class:Railtie>'
         8: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
         7: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:27:in `each'
         6: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
         5: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
         4: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
         3: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activerecord-4.2.10/lib/active_record/railtie.rb:122:in `block (2 levels) in <class:Railtie>'
         2: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activerecord-4.2.10/lib/active_record/connection_handling.rb:50:in `establish_connection'
         1: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/connection_specification.rb:174:in `spec'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/connection_specification.rb:177:in `rescue in spec': Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). (Gem::LoadError)

【问题讨论】:

  • 这绝不是一个好情况;它是由于代码、gem 和 Ruby 没有被维护以保持一切协同工作的结果。维护的 Ruby、Rails 和 gems 会慢慢留下旧代码。保留三台服务器,或至少三个代码实例。您的开发分支应该在 Ruby、Rails 和 gems 的最前沿运行,因此您可以提前发现问题。您的测试机器是您的下一个被推送到生产环境并运行候选 Ruby、Rails、gems 和您的 Rails 代码的版本。生产滞后几个版本并运行您所知道的稳定版本。
  • 您解决了这个问题吗?我也有类似的

标签: ruby-on-rails ruby pg


【解决方案1】:

如果您想至少暂时保持pg 的版本级别,您的主要解决方案是退回到早期版本的 ruby​​。您提到您正在运行 2.6.6,但根据错误:

pg (= 0.21.0) was resolved to 0.21.0, which depends on
      ruby  (< 2.5, >= 2.0) x64-mingw32

所以,pg 的那个版本只适用于 ruby​​ 最高版本(但不包括 2.5)。安装旧版本的 ruby​​,确保一切正常运行,然后您可以开始升级 rails 和 ruby​​ 版本。

【讨论】:

  • 感谢您的回答。问题是,当我降级 Ruby 时,我会收到类似的错误,但对于其他 gem,说它们需要更高的升级版本。
猜你喜欢
  • 2016-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-02
  • 2018-10-26
  • 1970-01-01
  • 2019-11-06
  • 2012-07-20
相关资源
最近更新 更多