Fail to install pg gem in Rails (using PostgreSQL 9.6) - ruby-on-rails

Hi could someone please help me! Appreciate it a lot!
I have some problem bundle install because the terminal says I fail to install gem pg v 0.20.0
When I tried to install the gem pg this error occurs. I'm currently using PostgreSQL 9.6 I have tried through all the solutions in the similar question but nothing works, including trying to reconfigure pg.
198-195:Review minhvu$ gem install pg -- --with-pg-config=/usr/pgsql-9.6/bin/pg_config
Building native extensions with: '--with-pg-config=/usr/pgsql-9.6/bin/pg_config'
This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
current directory: /Users/minhvu/.rvm/gems/ruby-2.3.0/gems/pg-0.20.0/ext
/Users/minhvu/.rvm/rubies/ruby-2.3.0/bin/ruby -r ./siteconf20170313-7016-1if947v.rb extconf.rb --with-pg-config=/usr/pgsql-9.6/bin/pg_config
Using config values from /usr/pgsql-9.6/bin/pg_config
sh: /usr/pgsql-9.6/bin/pg_config: No such file or directory
sh: /usr/pgsql-9.6/bin/pg_config: No such file or directory
* extconf.rb failed *
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/Users/minhvu/.rvm/rubies/ruby-2.3.0/bin/$(RUBY_BASE_NAME)
--with-pg
--without-pg
--enable-windows-cross
--disable-windows-cross
--with-pg-config
--with-pg-dir
--without-pg-dir
--with-pg-include
--without-pg-include=${pg-dir}/include
--with-pg-lib
--without-pg-lib=${pg-dir}/lib
/Users/minhvu/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/mkmf.rb:456:in try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /Users/minhvu/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/mkmf.rb:541:intry_link0'
from /Users/minhvu/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/mkmf.rb:556:in try_link'
from extconf.rb:40:in'
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/Users/minhvu/.rvm/gems/ruby-2.3.0/extensions/x86_64-darwin-16/2.3.0/pg-0.20.0/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /Users/minhvu/.rvm/gems/ruby-2.3.0/gems/pg-0.20.0 for inspection.
Results logged to /Users/minhvu/.rvm/gems/ruby-2.3.0/extensions/x86_64-darwin-16/2.3.0/pg-0.20.0/gem_make.out

It seems to me, that you are trying to installing pg gem, but first you should install PostgreSQL in your system. Depending of what system you have you should follow the instructions in this post. I am quoting the answer from addicted Addicted user at the following post
Rails Error Installing PG
Steps to install
Install PostgreSQL and its libraries
sudo apt-get install postgresql postgresql-contrib libpq-dev
After the installation create a user for postgresql
sudo -u postgres createuser --superuser $USER
or
sudo -u postgres createuser pgs_root
Set user password for the postgresql user
sudo -u postgres psql postgres ( For psql prompt) postgres=# \passsword for ex.- postgres=# \passsword pgs_root
Configure the postgresql.conf file to make PostgreSQL listen to localhost or listen on an external IP or something, change this line to either the IP or 'localhost'
gedit /etc/postgresql/8.4/main/postgresql.conf listen_addresses = 'localhost

Related

AWS CodeBuild process for Rails fails to install pg gem

I am trying to deploy Rails to Elastic Beanstalk using a simple 3-step CodePipeline process. Step 1 takes the source from GitHub, gives it to CodeBuild, which then should create the build and pass it to EB.
For some reason, when the CodeBuild process reaches the pg gem, it fails with the following error:
Fetching pg 1.3.5
Installing pg 1.3.5 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory:
/root/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/pg-1.3.5/ext
/root/.rbenv/versions/2.7.2/bin/ruby -I
/root/.rbenv/versions/2.7.2/lib/ruby/2.7.0 -r ./siteconf20220419-56-10cqcfm.rb
extconf.rb
Calling libpq with GVL unlocked
checking for pg_config... yes
Using config values from /usr/bin/pg_config
Using libpq from /usr/lib64
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for pg_config_manual.h... yes
checking for PQconnectdb() in -lpq... yes
checking for PQconninfo() in libpq-fe.h... no
Your PostgreSQL is too old. Either install an older version of this gem or
upgrade your database to at least PostgreSQL-9.3.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/root/.rbenv/versions/2.7.2/bin/$(RUBY_BASE_NAME)
--with-pg
--without-pg
--enable-gvl-unlock
--disable-gvl-unlock
--enable-windows-cross
--disable-windows-cross
--with-pg-config
--without-pg-config
--with-pg_config
--without-pg_config
--with-pg-dir
--without-pg-dir
--with-pg-include
--without-pg-include=${pg-dir}/include
--with-pg-lib
--without-pg-lib=${pg-dir}/lib
--with-pq-dir
--without-pq-dir
--with-pq-include
--without-pq-include=${pq-dir}/include
--with-pq-lib
--without-pq-lib=${pq-dir}/lib
--with-pqlib
--without-pqlib
To see why this extension failed to compile, please check the mkmf.log which can
be found here:
/root/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/extensions/x86_64-linux/2.7.0/pg-1.3.5/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in
/root/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/pg-1.3.5 for inspection.
Results logged to
/root/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/extensions/x86_64-linux/2.7.0/pg-1.3.5/gem_make.out
An error occurred while installing pg (1.3.5), and Bundler cannot continue.
Make sure that `gem install pg -v '1.3.5' --source 'https://rubygems.org/'`
succeeds before bundling.
In Gemfile:
pg
[Container] 2022/04/19 14:56:37 Command did not exit successfully bundle install exit status 5
[Container] 2022/04/19 14:56:37 Phase complete: INSTALL State: FAILED
[Container] 2022/04/19 14:56:37 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: bundle install. Reason: exit status 5
I have the following barebones buildspec.yml:
version: 0.2
phases:
install:
commands:
- bundle install
artifacts:
files:
- '**/*'
The bundle process runs without error on my local machine.
I had the same problem you had above too (except without using CodeBuild). The issue is that you're trying to install the pg gem at version 1.3.5.
At the time of writing, Elastic Beanstalk on Amazon Linux 2 (AL2) ships with Postgres version 9.2 by default (incredibly old 😔). Whereas the pg gem dropped support for 9.2 after version 1.2.3. See here.
So your 2 options are either:
Enhance EB to install a more recent version of Postgres during the build & deploy process, for example as described here: https://stackoverflow.com/a/63204453/1852005.
Or you could just use version 1.2.3 of the pg gem by configuring that version in your Gemfile. That version shipped in 2020-03-18.
Either of those options should get you past that error.
Note that your application may also connect to Amazon's RDS which has a more recent Postgres version. However that RDS lives on another server, so don't get the two confused! You can see the difference if you eb ssh to the server and run e.g: this on the shell directly:
[root#ip-XXX-XX-XX-XXX current]# psql --version
psql (PostgreSQL) 9.2.24
compared with running this on a Rails console which shows the RDS version:
irb(main):001:0> ActiveRecord::Base.connection.select_value('SELECT version()')
=> "PostgreSQL 11.13 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 7.3.1 20180712 (Red Hat 7.3.1-12), 64-bit"

Getting PostgreSQL set up on Ubuntu bash running on Windows

I recently installed and set-up rails on an Ubuntu bash running on Windows as a Linux subsystem. I was able to get rails up and going without a problem and create a new rails application on my Windows C:/ drive. I tried downloading PSQL directly from the postgresql.org/downloads/windows website but had some issues so I canceled, then followed the Installing PostgresQL section of this tutorial. The only issue is that the previous download which was canceled was set to listen to the default Port 5432, and it appears to have kept the port occupied because the second installation said it had to use Port 5433.
EDIT: It appears that the original PSQL had been successfully installed as it appeared in my program list, I was able to uninstall it but did get an error warning that the data folder was not removed. After uninstalling and having only one installed copy of PSQL, I still get the same error when running the below sudo service postgresql start.
EDIT EDIT: I uninstalled both versions of PSQL completely and re-installed the version in the linked tutorial and it got rid of the second error I was having. However, my 'pg' gem is still not bundle installing.
Once that was complete, I tried switching over my existing rails application to postgresql. I updated the database.yml file and added the 'pg' gem to my Gemfile. I cannot, however, bundle install. When I try, I get this error:
Fetching pg 1.0.0
Installing pg 1.0.0 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /home/<user_profile>/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/pg-1.0.0/ext
/home/<user_profile>/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20180717-4727-vex5p2.rb extconf.rb
checking for pg_config... yes
Using config values from /usr/bin/pg_config
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a
client-side application.
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a
client-side application.
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/home/<user_profile>/.rbenv/versions/2.5.1/bin/$(RUBY_BASE_NAME)
--with-pg
--without-pg
--enable-windows-cross
--disable-windows-cross
--with-pg-config
--without-pg-config
--with-pg_config
--without-pg_config
--with-pg-dir
--without-pg-dir
--with-pg-include
--without-pg-include=${pg-dir}/include
--with-pg-lib
--without-pg-lib=${pg-dir}/lib
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/home/<user_profile>/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.0-static/pg-1.0.0/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /home/<user_profile>/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/pg-1.0.0 for
inspection.
Results logged to
/home/<user_profile>/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.0-static/pg-1.0.0/gem_make.out
An error occurred while installing pg (1.0.0), and Bundler cannot continue.
Make sure that `gem install pg -v '1.0.0' --source 'https://rubygems.org/'` succeeds before bundling.
In Gemfile:
pg
EDIT: Below is solved, refer to second edit above.
Similarly, when I try to start postgresql from the command line using sudo service postgresql start, I get the following:
$ sudo service postgresql start
* Starting PostgreSQL 9.5 database server * The PostgreSQL server failed to start. Please check the log output:
2018-07-17 14:59:26 DST [4858-1] LOG: could not bind IPv4 socket: Permission denied
2018-07-17 14:59:26 DST [4858-2] HINT: Is another postmaster already running on port 5433? If not, wait a few seconds and retry.
2018-07-17 14:59:26 DST [4858-3] WARNING: could not create listen socket for "localhost"
2018-07-17 14:59:26 DST [4858-4] FATAL: could not create any TCP/IP sockets
Does anybody know what is going wrong with the installation of PostgresQL in my environment and how I can get it successfully running on my Ubuntu shell?
Found the solution to these problems.
Issue #1 - 'pg' gem not installing
Execute the following in bash:
sudo apt-get install libpq-dev
gem install pg
bundle install
Issue #2 - postgresql cannot start without crashing
Uninstall all versions on PSQL on computer and reinstall one. The failed installation and the second version were sharing their data folder and caused errors with the installation I was using.

Azure: Rails and PostgreSQL on the same VM?

I'm trying to combine a Rails app and PostgreSQL on the same Azure virtual machine.
I installed PostgreSQL 9.5 following this guide and it seems to give no problems by itself. Now the problem is, I cannot install the pg gem. Specifically, running the command gem install pg -v '0.19.0', it gives me the following error:
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
current directory: /var/lib/gems/2.3.0/gems/pg-0.19.0/ext
/usr/bin/ruby2.3 -r ./siteconf20170122-18144-1ee3id1.rb extconf.rb
checking for pg_config... yes
Using config values from /usr/bin/pg_config
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/usr/bin/$(RUBY_BASE_NAME)2.3
--with-pg
--without-pg
--enable-windows-cross
--disable-windows-cross
--with-pg-config
--without-pg-config
--with-pg_config
--without-pg_config
--with-pg-dir
--without-pg-dir
--with-pg-include
--without-pg-include=${pg-dir}/include
--with-pg-lib
--without-pg-lib=${pg-dir}/lib
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/var/lib/gems/2.3.0/extensions/x86_64-linux/2.3.0/pg-0.19.0/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /var/lib/gems/2.3.0/gems/pg-0.19.0 for inspection.
Results logged to /var/lib/gems/2.3.0/extensions/x86_64-linux/2.3.0/pg-0.19.0/gem_make.out
Any help is really appreciated. Cheers
What is the response of which pg_config
If it is something like:
/usr/bin/which: no pg_config in...
Try installing again with below command:
sudo apt-get install postgresql-devel
As simple as it was, I just needed to install postgresql-server-dev-9.5 on my Azure Ubuntu VM. I just overlooked the error log :D

Can't install pg gem on Rails

I am trying to bundle install, but, for some strange reason, the pg gem is returning the following error on install:
$ gem install pg -v '0.18.4'
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/Users/username/.rvm/rubies/ruby-2.2.1/bin/ruby -r ./siteconf20151221-23315-1tkv3fd.rb extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/Users/username/.rvm/rubies/ruby-2.2.1/bin/$(RUBY_BASE_NAME)
--with-pg
--without-pg
--enable-windows-cross
--disable-windows-cross
--with-pg-config
--without-pg-config
--with-pg_config
--without-pg_config
--with-pg-dir
--without-pg-dir
--with-pg-include
--without-pg-include=${pg-dir}/include
--with-pg-lib
--without-pg-lib=${pg-dir}/lib
extconf failed, exit code 1
Gem files will remain installed in /Users/username/.rvm/gems/ruby-2.2.1/gems/pg-0.18.4 for inspection.
Results logged to /Users/username/.rvm/gems/ruby-2.2.1/extensions/x86_64-darwin-15/2.2.0/pg-0.18.4/gem_make.out
Can anyone please help me install this gem and make my bundle finish successfully?
You should probably install the PostgreSQL development libraries.
If you're on Ubuntu this will help:
sudo apt-get install libpq-dev
On a Mac:
brew install postgresql
You need to configure pg correctly.
Run:
bundle config build.pg --with-pg-config=/Applications/Postgres.app/Contents/Versions/(YOUR POSTGRES VERSION)/bin/pg_config
Then try to run bundle:
bundle install
If the PostgreSQL client library is installed, then you need to tell gem where to look for the include files, which it does by asking pg_config. I use a little shell file to make it easier for gem to find that utility:
#!/bin/sh -x
PATH=/Library/PostgreSQL/9.4/bin:$PATH
gem install pg $#
You could also manually adjust your PATH to always include the PostgreSQL bin directory, which should fix the problem permanently.
If you upgrade your PostgreSQL that path will change, so you might need to reinstall pg and/or change your PATH setting.
Once pg is installed, further upgrades to the gem will be able to install without rerunning the script.

Error with 'pg gem' installation when running 'bundle install'

I'm suddenly running into this issue when running 'bundle install'. Everything seems to be installing correctly, but then I run into an issue with 'pg', as so many others seem to do. As the message says, I try running 'gem install pg - '0.12.2'' but it still fails.
I'm on Snow Leopard 10.6 and this is the error I'm getting:
Installing pg (0.12.2) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/Users/thomaskim/.rvm/rubies/ruby-1.9.3-p327/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.
Provided configuration options:
--with-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/Users/thomaskim/.rvm/rubies/ruby-1.9.3-p327/bin/ruby
--with-pg
--without-pg
--with-pg-dir
--without-pg-dir
--with-pg-include
--without-pg-include=${pg-dir}/include
--with-pg-lib
--without-pg-lib=${pg-dir}/lib
--with-pg-config
--without-pg-config
--with-pg_config
--without-pg_config
Gem files will remain installed in /Users/thomaskim/.bundler/tmp/1336/gems/pg-0.12.2 for inspection.
Results logged to /Users/thomaskim/.bundler/tmp/1336/gems/pg-0.12.2/ext/gem_make.out
An error occurred while installing pg (0.12.2), and Bundler cannot continue.
Make sure that `gem install pg -v '0.12.2'` succeeds before bundling.
First you need to make sure you have command line tools (package for Xcode) installed. Since you're on old version of OSX - you will have to research how to do that.
Than, using homebrew install postgress
brew install postgres
After that, everything should bundle with no issues.
It appears that you do not have postgres installed properly. I got a similar error when I was trying to install a pg module with npm. It disappeared after installing postgres properly.
Here's a link to download: http://www.postgresql.org/download/macosx/
If you run rails on your machine only in dev mode and use sql lite for that, you need don't need to install the production bundles.
So if your gemfile looke like that:
group :production do
gem 'pg'
end
Try installing the bundles with the following command:
gem install --without production

Resources