Ruby-on-rails 错误信息:在打包前确保 `gem install pg -v '0.18.1'` 成功

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30162572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:14:30  来源:igfitidea点击:

Error message: Make sure that `gem install pg -v '0.18.1'` succeeds before bundling

ruby-on-railsruby

提问by phd

i have a problem with ruby. I tried a lot but nothing works for me.

我有红宝石问题。我尝试了很多,但没有任何效果对我有用。

When i want to start the rails server, i get this error message:

当我想启动 rails 服务器时,我收到此错误消息:

An error occurred while installing pg (0.18.1), and Bundler cannot continue. Make sure that "gem install pg -v '0.18.1" succeeds before bundling.

安装 pg (0.18.1) 时出错,Bundler 无法继续。在捆绑之前确保“gem install pg -v '0.18.1”成功。

This is what i tried already:

这是我已经尝试过的:

sudo install gem
bundle install
bundle install --path vendor/cache
gem install pg -v '0.18.1'

When i try gem install pg -v '0.18.1'i get this error message:

当我尝试时,gem install pg -v '0.18.1'我收到此错误消息:

Could not find gem 'pg (>= 0) ruby' in any of the gem sources listed in your Gemfile or installed on this machine. Run bundle installto install missing gems.

在 Gemfile 中列出的或安装在此机器上的任何 gem 源中找不到 gem 'pg (>= 0) ruby​​'。运行bundle install以安装缺少的 gem。

But bundle installdoesn't work either. I get this error message:

bundle install也不起作用。我收到此错误消息:

An error occurred while installing pg (0.18.1), and Bundler cannot continue. Make sure that gem install pg -v '0.18.1'succeeds before bundling.

安装 pg (0.18.1) 时出错,Bundler 无法继续。gem install pg -v '0.18.1'在捆绑之前确保成功。

I also tried to start the server in a new ruby project.

我还尝试在新的 ruby​​ 项目中启动服务器。

Nothing helps..

没有什么帮助..

Thanks for your help!

谢谢你的帮助!

These are my changes in my Gemfile:

这些是我在 Gemfile 中的更改:

group :production do
   gem 'pg'
   gem 'rails_12factor'
 end

group :development do
   gem 'sqlite3'
 end

采纳答案by phd

My teacher at Bloc had the solution! If anybody have the same problem run the following commands:

我在 Bloc 的老师有解决方案!如果有人遇到同样的问题,请运行以下命令:

  1. spring stop
  2. gem uninstall pg

  3. bundle install --without production

  1. spring stop
  2. gem uninstall pg

  3. bundle install --without production

This solved my problem.

这解决了我的问题。

回答by Mihai-Andrei Dinculescu

If you're on Ubuntu, most likely you're missing a hidden dependency

如果您使用的是Ubuntu,很可能您缺少一个隐藏的依赖项

sudo apt-get install libpq-dev

If you are on OS X, try these steps

如果您使用的是OS X,请尝试以下步骤

  • Install Xcode command line tools (Apple Developer site). If you have it already installed, update it using brew update.
  • brew uninstall postgresql
  • brew install postgresql
  • gem install pg
  • 安装 Xcode 命令行工具(Apple Developer 站点)。如果您已经安装了它,请使用更新它brew update
  • brew uninstall postgresql
  • brew install postgresql
  • gem install pg

回答by kartik

If you are an Ubuntu user you need to do the following prior installing the gem

如果您是 Ubuntu 用户,则需要在安装 gem 之前执行以下操作

sudo apt-get install libpq-dev

Then perform gem install pg -v '0.18.1'or just bundle installif you have you r gem in a GEMFILE.

然后执行gem install pg -v '0.18.1'或者只是bundle install如果你在 GEMFILE 中有你的 gem。

回答by Maged Makled

If you are using Mac and Homebrew, Looks like libpqxx lib was missing.

如果您使用的是 Mac 和 Homebrew,则似乎缺少 libpqxx 库。

brew install libpqxx

This command should do it.

这个命令应该可以做到。

回答by Jai Kumar Rajput

If you are not sure where your pg_config is, and assuming you are on Linux or Mac, you can run the following command:

如果您不确定 pg_config 在哪里,并且假设您使用的是 Linux 或 Mac,则可以运行以下命令:

which pg_config

this will return ==> /usr/pgsql-9.1/bin/pg_config

这将返回 ==> /usr/pgsql-9.1/bin/pg_config

now use this path as

现在将此路径用作

bundle config build.pg --with-pg-config=/usr/pgsql-9.1/bin/pg_config

Done now bundle install

现在完成 bundle install

回答by noctrnal

ARCHFLAGS="-arch x86_64" bundle installworked for me as discussed here.

ARCHFLAGS="-arch x86_64" bundle install正如这里讨论的那样对我来说有效。

回答by minimallinux

Anyone running in to this (a year later!) using Rails 5.1.2 I did the following after first installing and initiating Postgresql (Centos 7). So assuming you've already got postgresql installed and the postgres server&user set up.(+ the usual standard dev tools for Linux).

任何人(一年后!)使用 Rails 5.1.2 我在第一次安装和启动 Postgresql (Centos 7) 后做了以下工作。因此,假设您已经安装了 postgresql 并设置了 postgres 服务器和用户。(+ 通常用于 Linux 的标准开发工具)。

Add extra deps for rails to build gems.

$ sudo yum install postgresql-devel

Add postgres path in ~/.profile   

export PATH=/usr/bin/postgres:$PATH  (or your installed path)

Add another user/role with create db privileges using pgAdmin or shell
(should be the same user as the system/rails user because the postgres user doesn't have permissions for /rails/db/schema.rb, but the system/rails user does)

Below are shell commands for postgres create role and database.

$ sudo -u postgres psql (enter postgres password)
$ create role (linux/rails user) with createdb login password 'password';
$ \du  (check its done and has createDB privs)
$ CREATE DATABASE name;

You will automatically become owner of the new database if no other arguments are presented.

如果没有提供其他参数,您将自动成为新数据库的所有者。

Or you can use a gui like DBeaverto do the same.

或者你可以使用像DBeaver这样的gui来做同样的事情。

So the above sets up for rails to access postgresql and build the pg gem once you've swapped out the default Gemfile & config/database.yml

Now create app and set it up (no need for -d postgresql flag because we swap out the Gemfile and the config/database.yml file contents completely and rails will install a postgresql db on bundle update/install.

(change some to rake for earlier versions of rails)

$ rails new app
$ cd app
$ atom (or editor) Gemfile config/database.yml
Swap out both file contents (to ones shown below) & save.
$ bundle update
$ bundle install

Check it with

$ rails db:create

(postgresql database should now be connected), so scaffold something

$ rails g scaffold Users name:string email:string comment:text
$ rails db:migrate
$ rails server

http://localhost:3000 shows the default page and http://localhost:3000/users brings up your new Users page using postgresql not sqlite3. Put something in to test it.

Below are the Gemfile and config/database.yml files I used for Rails 5.1.2, including taps for Heroku.

下面是我用于 Rails 5.1.2 的 Gemfile 和 config/database.yml 文件,包括 Heroku 的 taps。

Gemfile rails 5.1.2

Gemfile 导轨 5.1.2

 source 'https://rubygems.org'

    gem 'rails',        '5.1.2'
    gem 'puma',         '3.9.1'
    gem 'sass-rails',   '5.0.6'
    gem 'uglifier',     '3.2.0'
    gem 'coffee-rails', '4.2.2'
    gem 'jquery-rails', '4.3.1'
    gem 'turbolinks',   '5.0.1'
    gem 'jbuilder',     '2.7.0'
    gem 'taps'

    #Postgresql Database
    group :production do
    gem 'pg', '0.21.0'
    end

    group :development, :test do
      gem 'sqlite3', '1.3.13'
      gem 'byebug',  '9.0.6', platform: :mri
    end

    group :development do
      gem 'web-console',           '3.5.1'
      gem 'listen',                '3.0.8'
      gem 'spring',                '2.0.2'
      gem 'spring-watcher-listen', '2.0.1'
    end
    # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
    gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

config/database.yml file contents (watch out for indentation)

development:
    adapter: postgresql
    encoding: unicode
    database: development or app_name
    pool: 5
    username: (user created for postgres/rails)
    password: password
    host: localhost


test:
    adapter: postgresql
    encoding: unicode
    database: development or app_name
    pool: 5
    username: (user created for postgres/rails)
    password: password
    host: localhost


production:
    adapter: postgresql
    encoding: unicode
    database: development or app_name
    pool: 5
    username: (user created for postgres/rails)
    password: password
    host: localhost

Doing all the above will get you a dev/prod postgresql database in rails but no test, you can also access the database directly from rails by just running "rails db" and entering password.

完成上述所有操作后,您将在 rails 中获得一个 dev/prod postgresql 数据库,但无需测试,您还可以通过运行“rails db”并输入密码直接从 rails 访问该数据库。

Its simple but gets you up and running with rails/postgresql quickly.

它很简单,但可以让您快速启动并运行 rails/postgresql。

回答by Alexander Karmes

For Alpine linux, you'd want to install postgresql-devfirst:

对于 Alpine linux,您需要先安装postgresql-dev

apk add --update postgresql-dev

回答by Aetherus

Try install pglike this:

尝试pg像这样安装:

gem install pg -- --with-pg-dir=/path/to/postgresql/root

If it doesn't work, try

如果它不起作用,请尝试

gem install pg -- --with-pg-include=/path/to/postgresql/root/include \
                  --with-pg-lib=/path/to/postgresql/root/lib

回答by Kuba Niechcia?

Bundler has some problem identifing the PostgreSQL server path. If you are pretty sure that the your PostgreSQL server is installed properly, all you need to do should be adding the path to this to PATHvariable. Example command:

Bundler 在识别 PostgreSQL 服务器路径时遇到了一些问题。如果您非常确定您的 PostgreSQL 服务器安装正确,那么您需要做的就是将路径添加到PATH变量中。示例命令:

export PATH=/path/to/postgres/bin/:$PATH

If you would still have some issues, it is very likely that you have something wrong with PostgreSQL install. If so, try installing Postgres.appand execute command as below:

如果您仍然有一些问题,很可能是您安装 PostgreSQL 时出现问题。如果是这样,请尝试安装Postgres.app并执行如下命令:

export PATH=/Applications/Postgres.app/Contents/Versions/9.3/bin/:$PATH

Make sure that the version is correct.

确保版本正确。