【问题标题】:Error message: Make sure that `gem install pg -v '0.18.1'` succeeds before bundling错误消息:在捆绑之前确保 `gem install pg -v '0.18.1'` 成功
【发布时间】:2015-07-21 15:45:25
【问题描述】:

我对红宝石有疑问。我尝试了很多,但没有什么对我有用。

当我想启动 Rails 服务器时,我收到以下错误消息:

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

这是我已经尝试过的:

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

当我尝试gem install pg -v '0.18.1'我得到这个错误信息:

在您的 Gemfile 中列出的或安装在这台机器上的任何 gem 源中都找不到 gem 'pg (>= 0) ruby​​'。 运行 bundle install 来安装缺失的 gem。

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

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

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

没有任何帮助..

感谢您的帮助!

这些是我在 Gemfile 中所做的更改:

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

group :development do
   gem 'sqlite3'
 end

【问题讨论】:

  • 添加完整的控制台日志
  • 你的操作系统是什么?在安装 gem pg 之前,你是否安装了客户端包和它的 postgresql 开发包?您是通过包管理器(如 apt 或 yum)安装它们还是只是下载了二进制版本并解压缩?
  • 我为 os x 下载了一个开发包。我的控制台日志是:$ rails server Warning: You're using Rubygems 2.0.14 with Spring. Upgrade to at least Rubygems 2.1.0 and run gem pristine --all for better startup performance. Could not find gem 'pg (>= 0) ruby' in any of the gem sources listed in your Gemfile or installed on this machine. Run bundle install to install missing gems.
  • 我很确定你没有安装 postgres
  • 我真的不知道为什么,它工作了一个星期......

标签: ruby-on-rails ruby


【解决方案1】:

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

sudo apt-get install libpq-dev

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

  • 安装 Xcode 命令行工具(Apple 开发者网站)。如果你有 已安装,请使用 brew update 进行更新。
  • brew uninstall postgresql
  • brew install postgresql
  • gem install pg

【讨论】:

  • 我这样做了,效果很好,但是当我尝试启动服务器时,我又得到了:Warning: You're using Rubygems 2.0.14 with Spring. Upgrade to at least Rubygems 2.1.0 and run gem pristine --all for better startup performance. Could not find gem 'pg (>= 0) ruby' in any of the gem sources listed in your Gemfile or installed on this machine.
  • 确保在项目使用的 ruby​​ 包中安装 pg gem。
  • 在 El Capitan 上为我工作。谢谢!
  • 我使用的是 Ubuntu 14.04,这个技巧对我有用。谢谢
  • 我花了很长时间才找到这个答案,但它确实有效(与我尝试过的所有其他解决方案不同)。谢谢!
【解决方案2】:

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

sudo apt-get install libpq-dev

然后执行gem install pg -v '0.18.1' 或只执行bundle install,如果您在 GEMFILE 中有您的宝石。

【讨论】:

    【解决方案3】:

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

    brew install libpqxx
    

    这个命令应该可以做到。

    【讨论】:

      【解决方案4】:

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

      which pg_config
      

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

      现在使用这个路径

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

      现在完成bundle install

      【讨论】:

        【解决方案5】:

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

        1. spring stop
        2. gem uninstall pg

        3. bundle install --without production

        这解决了我的问题。

        【讨论】:

          【解决方案6】:

          ARCHFLAGS="-arch x86_64" bundle install 为我工作,正如 here 所讨论的那样。

          【讨论】:

          【解决方案7】:

          任何使用 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;
          

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

          或者您也可以使用像 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.
          

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

          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
          

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

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

          【讨论】:

            【解决方案8】:

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

            apk add --update postgresql-dev
            

            【讨论】:

              【解决方案9】:

              尝试像这样安装pg

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

              如果不行,试试

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

              【讨论】:

              • 我得到了这个:ERROR: Error installing pg: ERROR: Failed to build gem native extension.
              【解决方案10】:

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

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

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

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

              确保版本正确。

              【讨论】:

              • 这也行不通。我再次收到此错误:Warning: You're using Rubygems 2.0.14 with Spring. Upgrade to at least Rubygems 2.1.0 and run gem pristine --all for better startup performance. Could not find gem 'pg (>= 0) ruby' in any of the gem sources listed in your Gemfile or installed on this machine. Run bundle install to install missing gems. Patricks-MacBook-Pro:first-app-new patrickhofer$
              • bundle install 的结果是什么?是否圆满结束?
              • 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.
              • 很难知道。导出到路径后,您是否在终端中关闭并打开新会话?
              • 你的 gemfile 中有 pg 吗?
              【解决方案11】:

              CentOS 6 上的 PostgreSQL 设置

              [root@git2 ~]# yum install postgresql-server
              [root@git2 ~]# psql 
              psql: could not connect to server: No such file or directory
                      Is the server running locally and accepting
                      connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
              [root@git2 ~]# /etc/init.d/postgresql 
              Usage: /etc/init.d/postgresql {start|stop|status|restart|condrestart|try-restart|reload|force-reload|initdb}
              [root@git2 ~]# /etc/init.d/postgresql start
              
              /var/lib/pgsql/data is missing. Use "service postgresql initdb" to initialize the cluster first.
                                                                         [FAILED]
              [root@git2 ~]# service postgresql initdb
              Initializing database:                                     [  OK  ]
              [root@git2 ~]# 
              [root@git2 ~]# /etc/init.d/postgresql start
              Starting postgresql service:                               [  OK  ]
              [root@git2 ~]# psql 
              psql: FATAL:  Ident authentication failed for user "root"
              [root@git2 ~]# 
              [root@git2 ~]# su - postgres
              -bash-4.1$ psql
              psql (8.4.13)
              Type "help" for help.
              
              postgres=# \l
                                                List of databases
                 Name    |  Owner   | Encoding |  Collation  |    Ctype    |   Access privilege
              s   
              -----------+----------+----------+-------------+-------------+-------------------
              ----
               postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
               template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
                                                                           : postgres=CTc/postg
              res
               template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
                                                                           : postgres=CTc/postg
              res
              (3 rows)
              
              postgres=# 
              postgres=# \q
              -bash-4.1$ 
              -bash-4.1$ pwd
              /var/lib/pgsql
              -bash-4.1$ wget -q http://www.commandprompt.com/ppbook/booktown.sql
              -bash-4.1$ ls -lh booktown.sql 
              -rw-r--r-- 1 postgres postgres 42K Jan 11  2005 booktown.sql
              -bash-4.1$ psql -f booktown.sql 
              CREATE DATABASE
              (snip)
              -bash-4.1$ psql
              psql (8.4.13)
              Type "help" for help.
              
              postgres=# \l
                                                List of databases
                 Name    |  Owner   | Encoding |  Collation  |    Ctype    |   Access privilege
              s   
              -----------+----------+----------+-------------+-------------+-------------------
              ----
               booktown  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
               postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
               template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
                                                                           : postgres=CTc/postg
              res
               template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
                                                                           : postgres=CTc/postg
              res
              (4 rows)
              
              postgres=# 
              postgres=# \q
              -bash-4.1$ 
              -bash-4.1$ pg_dumpall > /tmp/pg_dumpall.`date +%s`.sql
              -bash-4.1$ ls -lh /tmp/pg_dumpall.1349195444.sql 
              -rw-r--r-- 1 postgres postgres 44K Oct  2 12:30 /tmp/pg_dumpall.1349195444.sql
              -bash-4.1$ 
              -bash-4.1$ pg_dump --clean booktown > /tmp/pg_dump-booktown.`date +%s`.sql 
              -bash-4.1$ ls -lh /tmp/pg_dump-booktown.1349196164.sql 
              -rw-r--r-- 1 postgres postgres 46K Oct  2 12:42 /tmp/pg_dump-booktown.1349196164.sql
              -bash-4.1$ 
              -bash-4.1$ psql booktown
              psql (8.4.13)
              Type "help" for help.
              
              booktown=# 
              booktown=# \d
              (snip)
               public | subjects              | table    | postgres
               public | text_sorting          | table    | postgres
              
              booktown=# SELECT * FROM subjects;
                0 | Arts             | Creativity St
                1 | Business         | Productivity Ave
              (snip)
              

              Fedora 20 上的 PostgreSQL 设置

              [root@localhost ~]# yum install postgresql-server
              (snip)
              [root@localhost ~]# systemctl start postgresql.service
              Job for postgresql.service failed. See 'systemctl status postgresql.service' and 'journalctl -xn' for details.
              [root@localhost ~]# 
              [root@localhost ~]# systemctl status postgresql.service
              postgresql.service - PostgreSQL database server
                 Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled)
                 Active: failed (Result: exit-code) since Sat 2014-01-18 08:08:38 EST; 12s ago
                Process: 4921 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA} (code=exited, status=1/FAILURE)
              
              Jan 18 08:08:38 localhost.localdomain systemd[1]: Starting PostgreSQL database server...
              Jan 18 08:08:38 localhost.localdomain postgresql-check-db-dir[4921]: "/var/lib/pgsql/data" is missing or empty.
              Jan 18 08:08:38 localhost.localdomain systemd[1]: postgresql.service: control process exited, code=exited status=1
              Jan 18 08:08:38 localhost.localdomain systemd[1]: Failed to start PostgreSQL database server.
              Jan 18 08:08:38 localhost.localdomain systemd[1]: Unit postgresql.service entered failed state.
              [root@localhost ~]# 
              [root@localhost ~]# postgresql-setup initdb
              Initializing database ... OK
              [root@localhost ~]# 
              [root@localhost ~]# systemctl start postgresql.service
              [root@localhost ~]# 
              [root@localhost ~]# systemctl stop postgresql.service
              [root@localhost ~]# 
              [root@localhost ~]# cp -a /var/lib/pgsql/data/pg_hba.conf /var/lib/pgsql/data/pg_hba.conf.orig
              [root@localhost ~]# # insecure... just for dev...
              [root@localhost ~]# vim /var/lib/pgsql/data/pg_hba.conf
              [root@localhost ~]# diff /var/lib/pgsql/data/pg_hba.conf.orig /var/lib/pgsql/data/pg_hba.conf
              80c80
              < local   all             all                                     peer
              ---
              > local   all             all                                     trust
              82c82
              < host    all             all             127.0.0.1/32            ident
              ---
              > host    all             all             127.0.0.1/32            trust
              [root@localhost ~]# 
              [root@localhost ~]# systemctl start postgresql.service
              [root@localhost ~]# 
              [root@localhost ~]# su - postgres
              -bash-4.2$ psql -c '\du'
                                           List of roles
               Role name |                   Attributes                   | Member of 
              -----------+------------------------------------------------+-----------
               postgres  | Superuser, Create role, Create DB, Replication | {}
              
              -bash-4.2$ psql -c "CREATE ROLE pguser1 UNENCRYPTED PASSWORD 'secret1' NOSUPERUSER CREATEDB CREATEROLE NOINHERIT LOGIN"
              CREATE ROLE
              -bash-4.2$ psql -c '\du'
                                           List of roles
               Role name |                   Attributes                   | Member of 
              -----------+------------------------------------------------+-----------
               pguser1   | No inheritance, Create role, Create DB         | {}
               postgres  | Superuser, Create role, Create DB, Replication | {}
              
              -bash-4.2$ 
              -bash-4.2$ psql -c '\l'
                                                List of databases
                 Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
              -----------+----------+----------+-------------+-------------+-----------------------
               postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
               template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
                         |          |          |             |             | postgres=CTc/postgres
               template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
                         |          |          |             |             | postgres=CTc/postgres
              (3 rows)
              
              -bash-4.2$ 
              -bash-4.2$ psql -c "CREATE DATABASE pgdatabase1 WITH OWNER = pguser1"
              CREATE DATABASE
              -bash-4.2$ psql -c '\l'
                                                 List of databases
                  Name     |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
              -------------+----------+----------+-------------+-------------+-----------------------
               pgdatabase1 | pguser1  | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
               postgres    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
               template0   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
                           |          |          |             |             | postgres=CTc/postgres
               template1   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
                           |          |          |             |             | postgres=CTc/postgres
              (4 rows)
              
              -bash-4.2$ 
              

              【讨论】:

                猜你喜欢
                • 2017-08-27
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-07-19
                • 2016-08-28
                • 1970-01-01
                相关资源
                最近更新 更多