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

Ruby on-RailsRuby

Ruby on-Rails Problem Overview


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:

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

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:

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

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

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

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

Nothing helps..

Thanks for your help!

These are my changes in my Gemfile:

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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

sudo apt-get install libpq-dev

If you are on OS X, try these steps

  • 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

Solution 2 - Ruby on-Rails

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

sudo apt-get install libpq-dev

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

Solution 3 - Ruby on-Rails

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

brew install libpqxx

This command should do it.

Solution 4 - Ruby on-Rails

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

which pg_config

this will return ==> /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

Solution 5 - Ruby on-Rails

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

  1. spring stop

  2. gem uninstall pg

  3. bundle install --without production

This solved my problem.

Solution 6 - Ruby on-Rails

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

Solution 7 - Ruby on-Rails

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).

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 DBeaver to do the same.

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.

Gemfile rails 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.

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

Solution 8 - Ruby on-Rails

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

apk add --update postgresql-dev

Solution 9 - Ruby on-Rails

Try install pg like this:

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

Solution 10 - Ruby on-Rails

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 PATH variable. Example command:

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.app and execute command as below:

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

Make sure that the version is correct.

Solution 11 - Ruby on-Rails

PostgreSQL setup on CentOS 6

[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)

PostgreSQL setup on Fedora 20

[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$ 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionphdView Question on Stackoverflow
Solution 1 - Ruby on-RailsMihai DinculescuView Answer on Stackoverflow
Solution 2 - Ruby on-RailskartikView Answer on Stackoverflow
Solution 3 - Ruby on-RailsMaged MakledView Answer on Stackoverflow
Solution 4 - Ruby on-RailsJai Kumar RajputView Answer on Stackoverflow
Solution 5 - Ruby on-RailsphdView Answer on Stackoverflow
Solution 6 - Ruby on-RailsnoctrnalView Answer on Stackoverflow
Solution 7 - Ruby on-RailsminimallinuxView Answer on Stackoverflow
Solution 8 - Ruby on-RailsAlexander KarmesView Answer on Stackoverflow
Solution 9 - Ruby on-RailsAetherusView Answer on Stackoverflow
Solution 10 - Ruby on-RailsKuba NiechciaƂView Answer on Stackoverflow
Solution 11 - Ruby on-RailsMilindView Answer on Stackoverflow