Bundle install无法安装Gemfile中的gem

3
我正在输入以下命令:
rake -RAILS_ENV=production db:schema:load

在我的生产服务器上,Rails 应用程序的主目录下,我收到了以下错误信息:
rake aborted!
Please install the postgresql adapter: `gem install activerecord-postgresql-adapter` (pg is not part of the bundle. Add it to Gemfile.)

这很奇怪,因为我知道pg在我的Gemfile中:

source 'https://rubygems.org'

gem 'rails', '3.2.8'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

group :development, :production do
  gem 'pg'
  gem 'devise'
  gem 'haml-rails'
  gem 'paperclip'
  gem 'will_paginate'
end

group :development do
  gem 'capistrano'
end


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

当我从该目录运行bundle install时,我会得到以下输出(其中不包括pg):

Fetching gem metadata from https://rubygems.org/.......
Using rake (0.9.2.2)
Using i18n (0.6.1)
Using multi_json (1.3.6)
Using activesupport (3.2.8)
Using builder (3.0.0)
Using activemodel (3.2.8)
Using erubis (2.7.0)
Using journey (1.0.4)
Using rack (1.4.1)
Using rack-cache (1.2)
Using rack-test (0.6.1)
Using hike (1.2.1)
Using tilt (1.3.3)
Using sprockets (2.1.3)
Using actionpack (3.2.8)
Using mime-types (1.19)
Using polyglot (0.3.3)
Using treetop (1.4.10)
Using mail (2.4.4)
Using actionmailer (3.2.8)
Using arel (3.0.2)
Using tzinfo (0.3.33)
Using activerecord (3.2.8)
Using activeresource (3.2.8)
Using cocaine (0.2.1)
Using coffee-script-source (1.3.3)
Using execjs (1.4.0)
Using coffee-script (2.2.0)
Using rack-ssl (1.3.2)
Using json (1.7.5)
Using rdoc (3.12)
Using thor (0.16.0)
Using railties (3.2.8)
Using coffee-rails (3.2.2)
Using orm_adapter (0.4.0)
Using warden (1.2.1)
Using devise (2.1.2)
Using haml (3.1.7)
Using haml-rails (0.3.4)
Using jquery-rails (2.1.1)
Using paperclip (3.1.2)
Using bundler (1.2.1)
Using rails (3.2.8)
Using sass (3.2.1)
Using sass-rails (3.2.5)
Using uglifier (1.2.7)
Using will_paginate (3.0.3)
Your bundle is complete! It was installed into /usr/local/lib

如果相关的话,这是我的Gemfile.lock文件(pg仅作为依赖项列出):

GEM
  remote: https://rubygems.org/
  specs:
    actionmailer (3.2.8)
      actionpack (= 3.2.8)
      mail (~> 2.4.4)
    actionpack (3.2.8)
      activemodel (= 3.2.8)
      activesupport (= 3.2.8)
      builder (~> 3.0.0)
      erubis (~> 2.7.0)
      journey (~> 1.0.4)
      rack (~> 1.4.0)
      rack-cache (~> 1.2)
      rack-test (~> 0.6.1)
      sprockets (~> 2.1.3)
    activemodel (3.2.8)
      activesupport (= 3.2.8)
      builder (~> 3.0.0)
    activerecord (3.2.8)
      activemodel (= 3.2.8)
      activesupport (= 3.2.8)
      arel (~> 3.0.2)
      tzinfo (~> 0.3.29)
    activeresource (3.2.8)
      activemodel (= 3.2.8)
      activesupport (= 3.2.8)
    activesupport (3.2.8)
      i18n (~> 0.6)
      multi_json (~> 1.0)
    arel (3.0.2)
    bcrypt-ruby (3.0.1-x86-mingw32)
    builder (3.0.0)
    capistrano (2.12.0)
      highline
      net-scp (>= 1.0.0)
      net-sftp (>= 2.0.0)
      net-ssh (>= 2.0.14)
      net-ssh-gateway (>= 1.1.0)
    cocaine (0.2.1)
    coffee-rails (3.2.2)
      coffee-script (>= 2.2.0)
      railties (~> 3.2.0)
    coffee-script (2.2.0)
      coffee-script-source
      execjs
    coffee-script-source (1.3.3)
    devise (2.1.2)
      bcrypt-ruby (~> 3.0)
      orm_adapter (~> 0.1)
      railties (~> 3.1)
      warden (~> 1.2.1)
    erubis (2.7.0)
    execjs (1.4.0)
      multi_json (~> 1.0)
    haml (3.1.7)
    haml-rails (0.3.4)
      actionpack (~> 3.0)
      activesupport (~> 3.0)
      haml (~> 3.0)
      railties (~> 3.0)
    highline (1.6.13)
    hike (1.2.1)
    i18n (0.6.1)
    journey (1.0.4)
    jquery-rails (2.1.1)
      railties (>= 3.1.0, < 5.0)
      thor (~> 0.14)
    json (1.7.5)
    mail (2.4.4)
      i18n (>= 0.4.0)
      mime-types (~> 1.16)
      treetop (~> 1.4.8)
    mime-types (1.19)
    multi_json (1.3.6)
    net-scp (1.0.4)
      net-ssh (>= 1.99.1)
    net-sftp (2.0.5)
      net-ssh (>= 2.0.9)
    net-ssh (2.5.2)
    net-ssh-gateway (1.1.0)
      net-ssh (>= 1.99.1)
    orm_adapter (0.4.0)
    paperclip (3.1.2)
      activemodel (>= 3.0.0)
      activerecord (>= 3.0.0)
      activesupport (>= 3.0.0)
      cocaine (>= 0.0.2)
      mime-types
    pg (0.14.0-x86-mingw32)
    polyglot (0.3.3)
    rack (1.4.1)
    rack-cache (1.2)
      rack (>= 0.4)
    rack-ssl (1.3.2)
      rack
    rack-test (0.6.1)
      rack (>= 1.0)
    rails (3.2.8)
      actionmailer (= 3.2.8)
      actionpack (= 3.2.8)
      activerecord (= 3.2.8)
      activeresource (= 3.2.8)
      activesupport (= 3.2.8)
      bundler (~> 1.0)
      railties (= 3.2.8)
    railties (3.2.8)
      actionpack (= 3.2.8)
      activesupport (= 3.2.8)
      rack-ssl (~> 1.3.2)
      rake (>= 0.8.7)
      rdoc (~> 3.4)
      thor (>= 0.14.6, < 2.0)
    rake (0.9.2.2)
    rdoc (3.12)
      json (~> 1.4)
    sass (3.2.1)
    sass-rails (3.2.5)
      railties (~> 3.2.0)
      sass (>= 3.1.10)
      tilt (~> 1.3)
    sprockets (2.1.3)
      hike (~> 1.2)
      rack (~> 1.0)
      tilt (~> 1.1, != 1.3.0)
    thor (0.16.0)
    tilt (1.3.3)
    treetop (1.4.10)
      polyglot
      polyglot (>= 0.3.1)
    tzinfo (0.3.33)
    uglifier (1.2.7)
      execjs (>= 0.3.0)
      multi_json (~> 1.3)
    warden (1.2.1)
      rack (>= 1.0)
    will_paginate (3.0.3)

PLATFORMS
  x86-mingw32

DEPENDENCIES
  capistrano
  coffee-rails (~> 3.2.1)
  devise
  haml-rails
  jquery-rails
  paperclip
  pg
  rails (= 3.2.8)
  sass-rails (~> 3.2.3)
  uglifier (>= 1.0.3)
  will_paginate

.bundle/config的内容:

BUNDLE_FROZEN: '1'
BUNDLE_PATH: /usr/local/lib/
BUNDLE_DISABLE_SHARED_GEMS: '1'
BUNDLE_WITHOUT: development:test

我正在使用以下技术: - rails 3.2.8 - ruby 1.9.3 - rvm 1.16.5
请问为什么"bundle install"没有安装Gemfile中列出的所有gems?希望能得到解释。

你的 .bundle/config 文件内容是什么? - Andrew Marshall
谢谢,但我已经安装了PostgreSQL。which psql 命令返回 /usr/bin/psql,而 psql --version 命令返回 psql (PostgreSQL) 9.1.6。在Debian上安装Rails时,我需要做些特殊的设置吗? - Dr.Seuss
很奇怪,你谈论的是debian安装,但是你的Gemfile.lock显示了一个Windows构建平台(x86-mingw32)。 - rewritten
我的部署服务器是Debian;我的开发机器是Windows。 - Dr.Seuss
我和你有完全相同的问题。'pg' 在 gem 查询中,但 bundle 就是不安装 'pg'。所以每次都会出现错误 "指定了数据库适配器“postgresql”,但未加载 gem。请在 Gemfile 中添加 gem 'pg'"。你现在解决这个问题了吗?能否提供详细的解决方案? - Ivan Wang
显示剩余3条评论
3个回答

3

通过一些调查,我发现以下因素导致了我的问题:

1)我在安装了 Windows 版本的 gem pg(0.14.0-x86-mingw32)的 Windows 机器上进行开发。

2)然后将其添加到我的 Gemfile.lock 中,因此 capistrano 在 Debian 箱上安装我的应用程序时会使用 pg 作为依赖项。

3)capistrano 使用“--deployment”标志运行 bundler,这要求您的 Gemfile.lock 必须是最新的和准确的。

我如何解决这个问题是从 deploy.rb 中删除了 capistrano 的“--deployment”标志,并使用以下变量定义:

set :bundle_flags, "--quiet"

require 'bundler/capistrano'

然后再次运行cap deploy:update

仍在努力找到一种方法,使Gemfile.lock反映出在生产环境上应安装pg,而在开发机器上应安装pg,以便我可以将--deployment选项重新包含到部署任务中。


1

关于 database.yml 文件

应该是这样的

development:
    adapter: postgresql
    encoding: utf8
    database: xxx
    host: localhost
    username: username
    password: pw

PostgreSQL 的适配器是 postgresql。

对于 gem 文件,请确保 pg 已经存在, 如果在开发和生产环境中都存在,就不需要使用 block。


database.yml 已经是这样的了,而且 Gemfile 中肯定已经包含了 'pg'。 - Dr.Seuss

1

你必须使用bundler来执行rake:

bundle exec rake db:schema:load RAILS_ENV=production

谢谢,但这会产生相同的错误信息。您能否解释一下使用和不使用bundle命令运行rake任务之间的区别?我从来没有弄清楚过。只是它是否使用在bundle中定义的gemset运行rake任务吗? - Dr.Seuss
它确保加载的 gem 是在 Gemfile 中声明的。你能发布 bundle show pg 的输出吗? - rewritten
Could not find gem 'pg' in the current bundle. - Dr.Seuss
gem "pg" 放到代码块之外。搜索您的项目中所有对 mingw 的引用。删除 .bundle 目录,让系统托管您的 gem。 - rewritten
抱歉,当你说让系统托管你的宝石时,你是什么意思? - Dr.Seuss

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接