Related
I am using the Ruby on Rails 3.1 pre version. I like to use PostgreSQL, but the problem is installing the pg gem. It gives me the following error:
$ gem install pg
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/home/u/.rvm/rubies/ruby-1.9.2-p0/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
--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/u/.rvm/rubies/ruby-1.9.2-p0/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 /home/u/.rvm/gems/ruby-1.9.2-p0/gems/pg-0.11.0 for inspection.
Results logged to /home/u/.rvm/gems/ruby-1.9.2-p0/gems/pg-0.11.0/ext/gem_make.out
How do I solve this problem?
It looks like in Ubuntu that header is part of the libpq-dev package (at least in the following Ubuntu versions:
11.04 (Natty Narwhal), 10.04 (Lucid Lynx), 11.10 (Oneiric Ocelot), 12.04 (Precise Pangolin), 14.04 (Trusty Tahr) and 18.04 (Bionic Beaver)):
...
/usr/include/postgresql/libpq-fe.h
...
So try installing libpq-dev or its equivalent for your OS:
For Ubuntu/Debian systems: sudo apt-get install libpq-dev
On Red Hat Linux (RHEL) systems: yum install postgresql-devel
For Mac Homebrew: brew install postgresql
For Mac MacPorts PostgreSQL: gem install pg -- --with-pg-config=/opt/local/lib/postgresql[version number]/bin/pg_config
For OpenSuse: zypper in postgresql-devel
For ArchLinux: pacman -S postgresql-libs
On macOS (previously Mac OS X and OS X), use Homebrew to install the proper headers:
brew install postgresql
and then running
gem install pg
should work.
Alternatively, instead of installing the whole postgresql, you can brew install libpq and export the correct PATH and PKG_CONFIG_PATH as explained in the 'Caveats' section
I had also tried doing gem install libpq-dev, but I received this error:
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
However I found that installing with sudo apt-get (which I try to avoid using with Ruby on Rails) worked, i.e.
sudo apt-get install libpq-dev
# or
apt-get install postgres-server-dev-{pg.version}
# for postgresql 9.4 on Ubuntu 14.04
then I was able to do
gem install pg
without issues.
I could solve this in another way. I didn't find the library on my system. Thus I installed it using an app from PostgreSQL main website. In my case (OS X) I found the file under /Library/PostgreSQL/9.1/include/ once the installation was over. You may also have the file somewhere else depending on your system if you already have PostgreSQL installed.
Thanks to this link on how to add an additional path for gem installation, I could point the gem to the lib with this command:
export CONFIGURE_ARGS="with-pg-include=/Library/PostgreSQL/9.1/include/"
gem install pg
After that, it works, because it now knows where to find the missing library. Just replace the path with the right location for your libpq-fe.h
Can't find the libpq-fe.h header
i had success on CentOS 7.0.1406 running the following commands:
~ % psql --version # => psql (PostgreSQL) 9.4.1
yum install libpqxx-devel
gem install pg -- --with-pg-config=/usr/pgsql-9.4/bin/pg_config
Alternatively, you can configure bundler to always install pg with these options (helpful for running bundler in deploy environments),
bundle config build.pg --with-pg-config=/usr/pgsql-9.4/bin/pg_config
For CentOS 6.4,
yum install postgresql-devel
gem install pg
worked well!
Just for the record:
Ruby on Rails 4 application in OS X with PostgresApp (in this case 0.17.1 version needed - kind of an old project):
gem install pg -v '0.17.1' -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config
It is only issue of missing libpq-fe.h
On ubuntu we just need to run:
sudo apt-get install libpq-dev
Above command will install a NEW package libpq-dev and after that you can again execute bundle install to resume your bundle installation.
On Mac OS X run like this:
gem install pg -- --with-pg-config=***/path/to/pg_config***
***/path/to/pg_config*** is path to pg_config
In my case it was package postgresql-server-dev-8.4 (I am on Ubuntu 11.04 (Natty Narwhal), 64 bits).
For MacOS without installing PostgreSQL server:
brew install libpq
gem install pg -- --with-pg-config="/usr/local/Cellar/libpq/9.6.6/bin/pg_config"
The right answer for Mac users with Postgres.app is to build against the libpq provided with that package. For example, with the 9.4 release (current as of this writing), all you need is:
export CONFIGURE_ARGS="with-pg-include=/Applications/Postgres.app/Contents/Versions/9.4/include"
gem install pg
This will keep your pg gem in sync with exactly the version of PostgreSQL you have installed. Installing something from Homebrew is a waste in this case.
I had the same issue on Amazon Linux. I could find the header libpq-fe.h, but somehow it didn't work.
It came from the different versions of the packages that were installed through the different users on the machine. PostgreSQL 9.2 and PostgreSQL 9.3 were installed. So, make sure of your PostgreSQL version before including the libraries.
For me, the magic command line was:
sudo yum install postgresql93 postgresql93-server postgresql93-libs postgresql93-contrib postgresql93-devel
Source: An almost idiot's guide to install PostgreSQL 9.3, PostGIS 2.1 and pgRouting with Yum
My solution for Fedora 30:
sudo dnf install /usr/include/libpq-fe.h
A more general answer for any Debian-based distribution (which includes Ubuntu) is the following. First, install the apt-file package running as root:
apt-get install apt-file
This allows you to search for packages containing a file. Then, update its database using
apt-file update
(this can be run as normal user). Then, look for the missing header using:
apt-file search libpq-fe.h
On my machine, this gives:
libpq-dev: /usr/include/postgresql/libpq-fe.h
postgres-xc-server-dev: /usr/include/postgres-xc/server/gtm/libpq-fe.h
There you go !
On Ubuntu, install the "libpq-dev" to get rid of this issue.
sudo apt-get install libpq-dev
On Fedora 32, I managed to solve this same issue by installing libpq5-devel specifically.
If you install postgresql from the official repository, you will have several versions that won't fix the problem, so it's really a matter of trying out which one it needs.
I had the same problem on Mac OS, but I installed the PostgreSQL gem easily by using the following in a terminal:
ARCHFLAGS="-arch x86_64" gem install pg
(I installed PostgreSQL first with brew install postgresql.)
I found this answer, and it was the only one that worked for me (Mac OS) - after researching for about two days:
$ sudo su
$ env ARCHFLAGS="-arch x86_64" gem install pg
Building native extensions. This could take a while...
Successfully installed pg-0.11.0
1 gem installed
Installing ri documentation for pg-0.11.0...
Installing RDoc documentation for pg-0.11.0...
See Stack Overflow question Can't find the PostgreSQL client library (libpq).
I recently upgraded to Mac OS X v10.10 (Yosemite) and was having difficulty building the pg gem.
The error reported was the typical:
Using config values from /usr/local/bin/pg_config
checking for libpq-fe.h... *** extconf.rb failed ***
My solution was to gem uninstall pg and then bundle update pg to replace the gem with the latest. I did run brew update; brew upgrade after the Yosemite install to get the latest versions of packages I had installed previously.
On a Mac, I solved it using this code:
gem install pg -v '0.18.4' -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config
I had this issue with Postgresql 9.6. I was able to fix it by doing:
brew upgrade postgresql#9.6
brew link postgresql#9.6 --force
gem install pg
I am running Postgres.app on a Mac and I had to
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.4/bin
first. Then
bundle install
worked for me.
I encountered the same error with postgres installed through asdf. The pg-config solutions didn't work for me. Instead I had to locate the postgres include folder which contains the file and run the command with the --with-pg-include flag
gem install pg -- --with-pg-include=/<path>/.asdf/installs/postgres/<version>/include
On CentOS,I installed libpq-dev package using below command
yum install postgresql-devel
Executing gem install pg returned the same error as "No pg_config... trying anyway. If building fails, please try again with --with-pg-config=/path/to/pg_config".
Installing the gem as below solved my problem
gem install pg -- --with-pg-config=/usr/pgsql-x.x/bin/pg_config
On Ubuntu 20.04, I already had libpq5:amd64 version 13.2-1.pgdg20.04+1. Now when I tried to install libpq-dev from apt repository, I was receiving
The following packages have unmet dependencies:
libpq-dev : Depends: libpq5 (= 12.6-0ubuntu0.20.04.1) but 13.2-1.pgdg20.04+1 is to be installed
I grabbed the deb from http://ftp.us.debian.org/debian/pool/main/p/postgresql-13/libpq-dev_13.2-1_amd64.deb and did the following:
mkdir -p /tmp/libpq-dev
dpkg-deb -R libpq-dev_13.2-1_amd64.deb /tmp/libpq-dev
sed -i 's|= 13.2-1|= 13.2-1.pgdg20.04+1|' /tmp/libpq-dev/DEBIAN/control
dpkg-deb -b /tmp/libpq-dev /tmp/libpq-dev-new.deb
sudo dpkg -i /tmp/libpq-dev-new.deb
I have been using the gem happily after that. No additional installs or anything.
For alpine, please use below to fix the error
apk add --no-cache postgresql-dev
On debian bullseye, none of the existing answers solved the issue for me.
In case it helps someone with the same configuration as mine:
Use PostgreSQL's apt repository: https://wiki.postgresql.org/wiki/Apt
sudo apt-get update && sudo apt-get dist-upgrade (upgrade PostgreSQL from v13 to v14)
sudo apt-get install libpq-dev
gem install pg or bundle install
On FreeBSD (9.1) the necessary package is /usr/ports/database/postgresql-server* which when installed will also install the required header file that makes the gem install of "pg" fail. This answer here helped me find the solution but the difference in package names required a bit of searching.
Hopefully this helps someone avoid a bit of head scratching when searching for the "-dev" package on a FreeBSD system!
On Debian 7.0, 64-bit (Wheezy), just run:
sudo apt-get install libpq-dev
After you successfully installed libpq-dev, run:
bundle install
libyaml warning doesn't go away, even if you install libyaml
gem install bundler
/home/ec2-user/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/yaml.rb:56:in `<top (required)>':
It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.
Fetching: bundler-1.3.4.gem (100%)
Successfully installed bundler-1.3.4
First remove any previous docs of yaml by
cd .rvm/src
sudo rm -rf yaml*
Then you can do rest of the install by -
rvm pkg install libyaml
P.S You will need to reinstall ruby after this.
UPDATE: If rvm pkg seems to be deprecated. You can compile a source on your own. All you
gotta do is download the latest version of libyaml from http://pyyaml.org/download/libyaml/
tar zxf yaml-0.1.4.tar.gz
cd yaml-0.1.4
./configure
make
make install
UPDATE2: BTW, you could still use rvm pkg and/or when reinstalling ruby make sure to enbale autolibs by
\curl -L https://get.rvm.io | bash -s stable --rails --autolibs=enable
A friend of mine had a similar problem on his mac.
brew install libyaml
ended up working for us and we were able to avoid a reinstall of ruby.
If using homebrew, a temporary fix that worked for me was to run:
brew unlink libyaml && brew link libyaml
plus you may also need to run:
brew unlink openssl && brew link --force openssl
See this issue thread for more details: https://github.com/wayneeseguin/rvm/issues/2689
sudo gem install nokogiri
Building native extensions. This could take a while...
ERROR: Error installing nokogiri:
ERROR: Failed to build gem native extension.
/Users/sajeev86/.rvm/rubies/ruby-1.8.7-p352/bin/ruby extconf.rb
checking for libxml/parser.h... no
-----
libxml2 is missing. please visit http://nokogiri.org/tutorials/installing_nokogiri.html for help with installing dependencies.
-----
*** 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.
I can't get Nokogiri to install. This has been taxing me for a while now. I've installed the dependencies via MacPorts and Homebrew as well.
I've got a feeling the right directories are not pointed to each other? But haven't a clue how to fix.
In Mavericks, installing the libraries with Homebrew and setting NOKOGIRI_USE_SYSTEM_LIBRARIES=1 before installing the gem did the trick for me.
Summarising:
If previously installed, uninstall the gem:
$ gem uninstall nokogiri
Use Homebrew to install libxml2, libxslt and libiconv:
$ brew install libxml2 libxslt libiconv
Install the gem specifying the paths to the libraries to be linked against:
$ NOKOGIRI_USE_SYSTEM_LIBRARIES=1 gem install nokogiri -- --use-system-libraries --with-iconv-dir="$(brew --prefix libiconv)" --with-xml2-config="$(brew --prefix libxml2)/bin/xml2-config" --with-xslt-config="$(brew --prefix libxslt)/bin/xslt-config"
Absolutely none of the other answers here worked for me.
On the latest version of Mavericks, the only method that succeeded for me was the following:
$ brew install libxml2 libxslt libiconv
$ sudo gem install nokogiri -v '1.6.5' -- --use-system-libraries --with-xml2-include=/usr/include/libxml2 --with-xml2-lib=/usr/lib
Same issue happened to me on Lion. Took hours of reading posts, trying homebrew, macports, etc. When I found this post and read the contents of .rvm/gems/ruby-1.8.7-p358/gems/nokogiri-1.4.4/ext/nokogiri/mkmf.log, I saw:
find_header: checking for libxml/parser.h... -------------------- no
"/usr/bin/gcc-4.2 -E -I. -I/Users/me/.rvm/rubies/ruby-1.8.7-p358/lib/ruby/1.8/i686-darwin10.8.0 -I. -I-I-I/opt/local/include -I-I-I/usr/local/include -I-I-I/Users/me/.rvm/rubies/ruby-1.8.7-p358/include -I-I-I/usr/include -I-I-I/usr/include/libxml2 -I/opt/local/include/libxml2 -I/usr/local/include/libxml2 -I/Users/me/.rvm/rubies/ruby-1.8.7-p358/include/libxml2 -I-I/opt/local/include -I-I/usr/local/include -I-I/Users/me/.rvm/rubies/ruby-1.8.7-p358/include -I-I/usr/include -I-I/usr/include/libxml2 -I/opt/local/include -I/usr/local/include -I/Users/me/.rvm/rubies/ruby-1.8.7-p358/include -I/usr/include -I/usr/include/libxml2 -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -g -O2 -fno-common -pipe -fno-common -g -DXP_UNIX -O3 -Wall -Wcast-qual -Wwrite-strings -Wconversion -Wmissing-noreturn -Winline conftest.c -o conftest.i"
checked program was:
/* begin */
1: #include <libxml/parser.h>
/* end */
However, gcc-4.2 didn't exist:
$ ls /usr/bin/gcc*
/usr/bin/gcc
Hence, I was able to solve the issue and get nokogiri to install by making a gcc-4.2 a symlink to gcc.
sudo ln -s gcc gcc-4.2
I ran into same issue for Mavrick and solution was the
xcode-select --install
However that was not working as apple download page does not have that installer available publically.
So if you want to have the command-line tools for Mavricks you need to have a paid account then only you will be able to install it.
Once you install it you will not face this issue of Nokogiri.
Checking the url given in the error message (http://nokogiri.org/tutorials/installing_nokogiri.html) I think the question is what version of Homebrew are you using?
brew -v
I'd update to 0.8+
brew update
brew upgrade
and then try the installation again:
brew install libxml2 libxslt
brew link libxml2 libxslt
gem install nokogiri
As for me, it helps:
brew install libxml2 libxslt
if permission denied sudo chown -R $USER /Library/Caches/Homebrew
sudo env ARCHFLAGS="-arch x86_64"
export NOKOGIRI_USE_SYSTEM_LIBRARIES=1
NOKOGIRI_USE_SYSTEM_LIBRARIES=1 gem install nokogiri
brew install libxml2 libxslt
gem install nokogiri -v '1.6.5' -- --use-system-libraries
Late to the party, but in case anyone is still having this issue while using MacPorts:
Add the /include dir to your PATH in your .profile file so nokogiri knows where to find it.
e.g.
MacPorts writes to your .profile file and adds the following line by default:
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
First, add the /include path like so:
export PATH=/opt/local/bin:/opt/local/sbin:/opt/local/include/:$PATH
Then, run this from the same terminal window:
source ~/.profile
(this tells your terminal to recheck the .profile file for any changes)
Finally, try installing the gem from the same terminal window.
sudo gem install nokogiri
On Yosemite I used:
brew install libxml2 libxslt
sudo gem install nokogiri -v '1.6.5' -- --use-system-libraries --with-xml2-include=/usr/include/libxml2 --with-xml2-lib=/usr/lib
Very close to some of the above commands but without the libconv
For my somewhat outdated system encountering the issues above, specifically:
FENG-SHUI:demo Eric$ brew install libxml2
==> Downloading ftp://xmlsoft.org/libxml2/libxml2-2.9.0.tar.gz
######################################################################## 100.0%
==> Downloading patches
######################################################################## 100.0%
######################################################################## 100.0%
==> Patching patching file threads.c patching file xpath.c
==> ./configure --prefix=/usr/local/Cellar/libxml2/2.9.0 --without-python
==> make
==> make install
==> Caveats This formula is keg-only: so it was not symlinked into /usr/local.
Mac OS X already provides this software and installing another version in parallel can cause all kinds of trouble.
Generally there are no consequences of this for you. If you build your own software and it requires this formula, you'll need to add to your build variables:
LDFLAGS: -L/usr/local/opt/libxml2/lib
CPPFLAGS: -I/usr/local/opt/libxml2/include
Error: Failed to create: /usr/local/opt/libxml2
Things that depend on libxml2 will probably not build.
==> Summary /usr/local/Cellar/libxml2/2.9.0: 273 files, 11M, built in 2.0 minutes
FENG-SHUI:demo Eric$ brew install libxml2 libxslt
Error: libxml2-2.9.0 already installed
Error: /usr/local/opt/libxml2 not present or broken Please reinstall libxml2. Sorry :(
This was the magic formula for getting libxml2 and libxslt happily running:
# latest version 2.9.0
brew install libxml2
# installing libxslt from source code
# get latest libxslt from ftp://xmlsoft.org/libxml2/libxslt-git-snapshot.tar.gz..
# then
./configure --prefix=/usr/local/Cellar/libxslt/1.1.28 --with-libxml-prefix=/usr/local/Cellar/libxml2/2.9.0
make
sudo make install
# installing nokogiri with this new compiled libs
gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.9.0/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.0/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28
Thanks to this guy: https://gist.github.com/fabioyamate/443160
Hope this helps someone!
Try adding --with-xml2-include=/opt/local/include/libxml2 --with-xml2-lib=/opt/local/lib to your command, assuming that's where MacPorts libxml2.
I struggled with this for hours until I found this blogpost: http://www.markhneedham.com/blog/2010/07/08/installing-ruby-1-9-2-with-rvm-on-snow-leopard
Running the command
file /usr/local/lib/libxml2.2.dylib
in the terminal returned
/usr/local/lib/libxml2.2.dylib: Mach-O dynamically linked shared library i386
which means my libxml2.2 was compiled for a 32 bit system, so I had to recompile libxml2 for 64 bit OS (Lion)
First I downloaded LATEST_LIBXML2 from ftp://xmlsoft.org/libxml2 and ran the following commands:
tar xzvf LATEST_LIBXML2
cd libxml2-2.8.0
./configure --with-python=/System/Library/Frameworks/Python.framework/Versions/2.3/
make
Then I downloaded LATEST_LIBXSLT from the same site and ran these commands:
tar xzvf LATEST_LIBXSLT
cd libxslt-1.1.26
./configure
make
sudo make install
Running
file /usr/local/lib/libxml2.2.dylib
again returned
/usr/local/lib/libxml2.2.dylib: Mach-O 64-bit dynamically linked shared library x86_64
And I could finally install nokogiri!
This worked for me on OSX - the same as #remeberer's answer, but with a full path.
sudo ln -s /usr/bin/gcc /usr/bin/gcc-4.2
I had to use this command for it to work :
sudo gem install nokogiri -- --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/libxml2 --use-system-libraries
(source : http://www.virtlab.cx/ruby-fix-error-install-nokogiri-yosemite/ , many thanks to the author)
ran into a similar install issue on mountain lion except the make command was looking for gcc-4.2 in /usr/local/opt/apple-gcc42/bin
this resolved the issue for me; no need to brew install libxml2 or libxslt or add a --with option:
mkdir -p /usr/local/opt/apple-gcc42/bin
ln -s /usr/bin/gcc /usr/local/opt/apple-gcc42/bin/gcc-4.2
If you're installing an older version of Nokogiri, i.e. pre 1.6, this may help:
In the Nokogiri installation guide, they mention:
Because Nokogiri needs to be compiled and dynamically linked against
both libxml2 and libxslt, it has gained a reputation for being
complicated to install.
As of Nokogiri 1.6, libxml2 and libxslt source code is bundled with
Nokogiri, and compiled at gem-install-time. This document should work
for all versions 1.6.4 and later.
(If you need support for installing earlier versions of Nokogiri, you
may want to take a look at the git history for this document.)
Indeed, it is difficult to install, but there is a way. I went digging through the history of the readme, and decided on this one, with the well-written commit message:
"Homebrew, I straight hate you."
From that message and from the other answers here, I gleaned that I needed to be very specific about libxml2's lib, dir and include directories when installing Nokogiri. The following worked for me, but please update to match your version of the libxml2 software (mine was 2.9.2 and I was installing Nokogiri 1.5.0):
NOKOGIRI_USE_SYSTEM_LIBRARIES=1 gem install nokogiri -v '1.5.0' --
--use-system-libraries
--with-xml2-lib=/usr/local/Cellar/libxml2/2.9.2/lib
--with-xml2-config="$(brew --prefix libxml2)/bin/xml2-config"
--with-xslt-config="$(brew --prefix libxslt)/bin/xslt-config"
--with-xml2-dir=/usr/local/Cellar/libxml2/2.9.2/
--with-xml2-include=/usr/local/Cellar/libxml2/2.9.2/include/libxml2
For me, I simply did the following
xcode-select --install
gem install nokogiri
For more in-depth troubleshooting, checkout Installing Nokogiri
I always worked my way around Nokogiri installation issues by following the documentation in the "Installing Nokogiri" tutorial.
But this time, even after installing all the dependencies, Nokogiri hasn't been installed. I get the following error:
libxml2 is missing. please visit <http://nokogiri.org/tutorials/installing_nokogiri.html>
I tried installing it by specifying the libxml2 and libxslt directories:
sudo gem install nokogiri -- --with-xml2-include=/usr/include/libxml2 --with-xml2-lib=/usr/lib --with-xslt-dir=/usr/
but it returned the same error.
I followed all the other related Stack Overflow articles and none helped. Does anyone have a solution?
You may actually need to install both of these packages
sudo apt-get install libxslt-dev libxml2-dev
First, install the dependencies:
sudo apt-get install libxslt-dev libxml2-dev
If you still receive the error, you may be missing a compiler toolchain:
sudo apt-get install build-essential
You'll get the "libxml2 is missing" error if you're missing a build toolchain (at least I ran into this issue on Debian Lenny).
The Nokogiri build test-compiles a libxml2 header file to verify that it is present, however, it doesn't differentiate between "libxml2 is missing" and "a compiler to test libxml2 is missing".
In Mac OS X (Mavericks), installing the libraries with brew and setting NOKOGIRI_USE_SYSTEM_LIBRARIES=1 before installing the gem did the trick for me.
Summarising:
If previously installed, uninstall the gem:
gem uninstall nokogiri
Use Homebrew to install libxml2, libxslt and libiconv:
brew install libxml2 libxslt libiconv
Install the gem specifying the paths to the libraries to be linked against:
NOKOGIRI_USE_SYSTEM_LIBRARIES=1 gem install nokogiri -- --use-system-libraries --with-iconv-dir="$(brew --prefix libiconv)" --with-xml2-config="$(brew --prefix libxml2)/bin/xml2-config" --with-xslt-config="$(brew --prefix libxslt)/bin/xslt-config"
I just had the same issue on Fedora 13. After a frustrating and unsuccessful search to make
gem install nokogiri
work for me, I was able to install it and get around the libxml2 error via yum.
Simply install the gem via yum instead of the gem command:
su
yum search rubygem-nokogiri #this find the proper package name
yum install rubygem-nokogiri.i686
This helped me find the right answer for Fedora and, as I am using RVM for Ruby package management,
yum install rubygem-nokogiri
will pull in all the Ruby gems and dependencies into the system, not into my RVM environment, and in my experience that leads to a very frustrating and humbling experience.
So, taking your find of the Nokogiri yum gem you can use:
yum provides rubygem-nokogiri
and get a list of the dependencies for rubygem-Nokogiri which showed me the libraries that were missing. After that I ran:
yum install libxml2-devel libxslt libxslt-devel
Now Nokogiri compiles in Fedora and Nokogiri installs. D'oh!, we need the headers to compile Nokogiri from the devel libraries.
In Mac OS X (Mavericks) if none of these solutions work, try:
ARCHFLAGS="-arch x86_64" gem install nokogiri
or
ARCHFLAGS="-arch i386" gem install nokogiri
depending on your system's architecture.
You usually need development files for building gems. Try:
sudo apt-get install libxslt-dev libxml2-dev
(I just saw that Eric suggested the same in a comment.)
I was able to get this installed with Chocolatey, Windows 8.1 x64, and DevKit x64.
cinst libxml2
cinst libxslt
cinst libiconv
gem install nokogiri --
--with-xml2-include=C:\Chocolatey\lib\libxml2.2.7.8.7\build\native\include
--with-xml2-lib=C:\Chocolatey\lib\libxml2.redist.2.7.8.7\build\native\bin\v110\x64\Release\dynamic\cdecl
--with-iconv-include=C:\Chocolatey\lib\libiconv.1.14.0.11\build\native\include
--with-iconv-lib=C:\Chocolatey\lib\libiconv.redist.1.14.0.11\build\native\bin\v110\x64\Release\dynamic\cdecl
--with-xslt-include=C:\Chocolatey\lib\libxslt.1.1.28.0\build\native\include
--with-xslt-lib=C:\Chocolatey\lib\libxslt.redist.1.1.28.0\build\native\bin\v110\x64\Release\dynamic
You'll have to verify the version number in the paths are correct.
You may possibly need to add Microsoft's NuGet repository:
-Source "https://go.microsoft.com/fwlink/?LinkID=230477"
It will be:
sudo yum install -y libxml2 libxml2-devel
on RHEL servers.
At macOS none of above/below had really worked for me until I explicitly provided XCode libxml2 path to --with-xml2-include.
gem install nokogiri -- --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/libxml2 --use-system-libraries
I was able to install Nokogiri 1.6.5 on Fedora 20 by doing:
export NOKOGIRI_USE_SYSTEM_LIBRARIES=true
Then running:
gem install nokogiri
For anyone else experiencing this issue, I solved it by running this command within the project directory
gem install nokogiri -- --use-system-libraries
Update: I ran into this again and tried just updating the gem which worked for me...
gem update nokogiri
Have you tried installing libxml2? Not from rubygems, but via the standard install process for your operating system. On Ubuntu/Debian for example:
sudo apt-get install libxml2
On any recent version of OS X it should already be installed.
For OSX users, if you've had success installing Nokogiri before, yet are getting errors installing it on, say, using a new version of Ruby that you've added and that error that includes a message like:
The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
This could be indicative of a XCode upgrade having happened via the App Store, either manually or automatically, and you not having opened it since that time.
If so, you should be able to open XCode, agree to the new license, and then install Nokogiri successfully.
On Mac OS X Yosemite my mistake was that I tried to use sudo gem install when it's a rule of thumb to not use superuser privileges when installing gems.
In my case it tried to modify the system installation of Ruby, and that's not a good idea. I installed rbenv, installed Ruby 2.2.2 thru it and set it as global, which is a term from rbenv's documentation. After that Nokogiri could install itself with a simple
gem install nokogiri
No hacks or workarounds were necessary, just a properly set environment; Nokogiri does the rest perfectly.
Was able to install vagrant-awe by following the above post but using the command as follow:
NOKOGIRI_USE_SYSTEM_LIBRARIES=1 vagrant plugin install vagrant-aws
For Windows x64:
gem inst nokogiri --pre --platform ruby
For more information check this thread: https://github.com/sparklemotion/nokogiri/issues/864
I was able to install Nokogiri by running the following packages:
$ dnf group install "C Development Tools and Libraries"
$ dnf install ruby-devel libxml2-devel patch
=== TLDR ===
As the instructions state, build Ruby with the latest clang compiler, or at least the same version that Rubygems will use to compile Nokogiri's native extensions libxml and libxsl.
If using RVM, building from source with the --with-ggc=clang flag was what did it for me:
rvm install 2.2 --with-gcc=clang
Replace 2.2 with whatever version you want. --with-gcc=clang ensures RVM builds from source and uses clang to do so; otherwise RVM may a pre-built binary Ruby, which is what tripped me up.
Then, install Nokogiri as normal using Bundler or Rubygems.
=== IN DEPTH ===
I struggled with this for a while. gem install nokogiri gave me:
checking for xmlParseDoc() in libxml/parser.h... no
checking for xmlParseDoc() in -lxml2... no
checking for xmlParseDoc() in -llibxml2... no
Digging into mkmf.log I saw:
conftest.c:15:27: error: too few arguments to function call, single argument 'cur' was not specified
int t(void) { xmlParseDoc(); return 0; }
Nokogiri supplies its own libxml and libxsl (as of 1.6.4). The signature defined in Nokogiri's local copy of parser.h (found under the gem install directory) is:
xmlParseDoc (const xmlChar *cur);
So I was at a loss as to how the method call in the hermetically sealed conftest.c file usage couldn't match up with the header file for parser.h.
When I realized I had probably installed a binary Ruby I removed and reinstalled using --with-gcc=clang (to force compilation and use clang) and the problem was solved:
rvm uninstall 2.2
rvm install 2.2 --with-gcc=clang
gem install nokogiri
I'm not exactly sure why that works as the system libxml header /usr/include/libxml2/libxml/parser.h has the same signature as Nokogiri's local copy.
It's weird, but it worked. Just make sure you compile a Ruby with clang.
gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.7.8/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.7.8/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26 --with-iconv-include=/usr/local/Cellar/libiconv/1.14/include --with-iconv-lib=/usr/local/Cellar/libiconv/1.14/lib
Change your version with it.
I am using the Ruby on Rails 3.1 pre version. I like to use PostgreSQL, but the problem is installing the pg gem. It gives me the following error:
$ gem install pg
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/home/u/.rvm/rubies/ruby-1.9.2-p0/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
--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/u/.rvm/rubies/ruby-1.9.2-p0/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 /home/u/.rvm/gems/ruby-1.9.2-p0/gems/pg-0.11.0 for inspection.
Results logged to /home/u/.rvm/gems/ruby-1.9.2-p0/gems/pg-0.11.0/ext/gem_make.out
How do I solve this problem?
It looks like in Ubuntu that header is part of the libpq-dev package (at least in the following Ubuntu versions:
11.04 (Natty Narwhal), 10.04 (Lucid Lynx), 11.10 (Oneiric Ocelot), 12.04 (Precise Pangolin), 14.04 (Trusty Tahr) and 18.04 (Bionic Beaver)):
...
/usr/include/postgresql/libpq-fe.h
...
So try installing libpq-dev or its equivalent for your OS:
For Ubuntu/Debian systems: sudo apt-get install libpq-dev
On Red Hat Linux (RHEL) systems: yum install postgresql-devel
For Mac Homebrew: brew install postgresql
For Mac MacPorts PostgreSQL: gem install pg -- --with-pg-config=/opt/local/lib/postgresql[version number]/bin/pg_config
For OpenSuse: zypper in postgresql-devel
For ArchLinux: pacman -S postgresql-libs
On macOS (previously Mac OS X and OS X), use Homebrew to install the proper headers:
brew install postgresql
and then running
gem install pg
should work.
Alternatively, instead of installing the whole postgresql, you can brew install libpq and export the correct PATH and PKG_CONFIG_PATH as explained in the 'Caveats' section
I had also tried doing gem install libpq-dev, but I received this error:
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
However I found that installing with sudo apt-get (which I try to avoid using with Ruby on Rails) worked, i.e.
sudo apt-get install libpq-dev
# or
apt-get install postgres-server-dev-{pg.version}
# for postgresql 9.4 on Ubuntu 14.04
then I was able to do
gem install pg
without issues.
I could solve this in another way. I didn't find the library on my system. Thus I installed it using an app from PostgreSQL main website. In my case (OS X) I found the file under /Library/PostgreSQL/9.1/include/ once the installation was over. You may also have the file somewhere else depending on your system if you already have PostgreSQL installed.
Thanks to this link on how to add an additional path for gem installation, I could point the gem to the lib with this command:
export CONFIGURE_ARGS="with-pg-include=/Library/PostgreSQL/9.1/include/"
gem install pg
After that, it works, because it now knows where to find the missing library. Just replace the path with the right location for your libpq-fe.h
Can't find the libpq-fe.h header
i had success on CentOS 7.0.1406 running the following commands:
~ % psql --version # => psql (PostgreSQL) 9.4.1
yum install libpqxx-devel
gem install pg -- --with-pg-config=/usr/pgsql-9.4/bin/pg_config
Alternatively, you can configure bundler to always install pg with these options (helpful for running bundler in deploy environments),
bundle config build.pg --with-pg-config=/usr/pgsql-9.4/bin/pg_config
For CentOS 6.4,
yum install postgresql-devel
gem install pg
worked well!
Just for the record:
Ruby on Rails 4 application in OS X with PostgresApp (in this case 0.17.1 version needed - kind of an old project):
gem install pg -v '0.17.1' -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config
It is only issue of missing libpq-fe.h
On ubuntu we just need to run:
sudo apt-get install libpq-dev
Above command will install a NEW package libpq-dev and after that you can again execute bundle install to resume your bundle installation.
On Mac OS X run like this:
gem install pg -- --with-pg-config=***/path/to/pg_config***
***/path/to/pg_config*** is path to pg_config
In my case it was package postgresql-server-dev-8.4 (I am on Ubuntu 11.04 (Natty Narwhal), 64 bits).
For MacOS without installing PostgreSQL server:
brew install libpq
gem install pg -- --with-pg-config="/usr/local/Cellar/libpq/9.6.6/bin/pg_config"
The right answer for Mac users with Postgres.app is to build against the libpq provided with that package. For example, with the 9.4 release (current as of this writing), all you need is:
export CONFIGURE_ARGS="with-pg-include=/Applications/Postgres.app/Contents/Versions/9.4/include"
gem install pg
This will keep your pg gem in sync with exactly the version of PostgreSQL you have installed. Installing something from Homebrew is a waste in this case.
I had the same issue on Amazon Linux. I could find the header libpq-fe.h, but somehow it didn't work.
It came from the different versions of the packages that were installed through the different users on the machine. PostgreSQL 9.2 and PostgreSQL 9.3 were installed. So, make sure of your PostgreSQL version before including the libraries.
For me, the magic command line was:
sudo yum install postgresql93 postgresql93-server postgresql93-libs postgresql93-contrib postgresql93-devel
Source: An almost idiot's guide to install PostgreSQL 9.3, PostGIS 2.1 and pgRouting with Yum
My solution for Fedora 30:
sudo dnf install /usr/include/libpq-fe.h
A more general answer for any Debian-based distribution (which includes Ubuntu) is the following. First, install the apt-file package running as root:
apt-get install apt-file
This allows you to search for packages containing a file. Then, update its database using
apt-file update
(this can be run as normal user). Then, look for the missing header using:
apt-file search libpq-fe.h
On my machine, this gives:
libpq-dev: /usr/include/postgresql/libpq-fe.h
postgres-xc-server-dev: /usr/include/postgres-xc/server/gtm/libpq-fe.h
There you go !
On Ubuntu, install the "libpq-dev" to get rid of this issue.
sudo apt-get install libpq-dev
On Fedora 32, I managed to solve this same issue by installing libpq5-devel specifically.
If you install postgresql from the official repository, you will have several versions that won't fix the problem, so it's really a matter of trying out which one it needs.
I had the same problem on Mac OS, but I installed the PostgreSQL gem easily by using the following in a terminal:
ARCHFLAGS="-arch x86_64" gem install pg
(I installed PostgreSQL first with brew install postgresql.)
I found this answer, and it was the only one that worked for me (Mac OS) - after researching for about two days:
$ sudo su
$ env ARCHFLAGS="-arch x86_64" gem install pg
Building native extensions. This could take a while...
Successfully installed pg-0.11.0
1 gem installed
Installing ri documentation for pg-0.11.0...
Installing RDoc documentation for pg-0.11.0...
See Stack Overflow question Can't find the PostgreSQL client library (libpq).
I recently upgraded to Mac OS X v10.10 (Yosemite) and was having difficulty building the pg gem.
The error reported was the typical:
Using config values from /usr/local/bin/pg_config
checking for libpq-fe.h... *** extconf.rb failed ***
My solution was to gem uninstall pg and then bundle update pg to replace the gem with the latest. I did run brew update; brew upgrade after the Yosemite install to get the latest versions of packages I had installed previously.
On a Mac, I solved it using this code:
gem install pg -v '0.18.4' -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config
I had this issue with Postgresql 9.6. I was able to fix it by doing:
brew upgrade postgresql#9.6
brew link postgresql#9.6 --force
gem install pg
I am running Postgres.app on a Mac and I had to
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.4/bin
first. Then
bundle install
worked for me.
I encountered the same error with postgres installed through asdf. The pg-config solutions didn't work for me. Instead I had to locate the postgres include folder which contains the file and run the command with the --with-pg-include flag
gem install pg -- --with-pg-include=/<path>/.asdf/installs/postgres/<version>/include
On CentOS,I installed libpq-dev package using below command
yum install postgresql-devel
Executing gem install pg returned the same error as "No pg_config... trying anyway. If building fails, please try again with --with-pg-config=/path/to/pg_config".
Installing the gem as below solved my problem
gem install pg -- --with-pg-config=/usr/pgsql-x.x/bin/pg_config
On Ubuntu 20.04, I already had libpq5:amd64 version 13.2-1.pgdg20.04+1. Now when I tried to install libpq-dev from apt repository, I was receiving
The following packages have unmet dependencies:
libpq-dev : Depends: libpq5 (= 12.6-0ubuntu0.20.04.1) but 13.2-1.pgdg20.04+1 is to be installed
I grabbed the deb from http://ftp.us.debian.org/debian/pool/main/p/postgresql-13/libpq-dev_13.2-1_amd64.deb and did the following:
mkdir -p /tmp/libpq-dev
dpkg-deb -R libpq-dev_13.2-1_amd64.deb /tmp/libpq-dev
sed -i 's|= 13.2-1|= 13.2-1.pgdg20.04+1|' /tmp/libpq-dev/DEBIAN/control
dpkg-deb -b /tmp/libpq-dev /tmp/libpq-dev-new.deb
sudo dpkg -i /tmp/libpq-dev-new.deb
I have been using the gem happily after that. No additional installs or anything.
For alpine, please use below to fix the error
apk add --no-cache postgresql-dev
On debian bullseye, none of the existing answers solved the issue for me.
In case it helps someone with the same configuration as mine:
Use PostgreSQL's apt repository: https://wiki.postgresql.org/wiki/Apt
sudo apt-get update && sudo apt-get dist-upgrade (upgrade PostgreSQL from v13 to v14)
sudo apt-get install libpq-dev
gem install pg or bundle install
On FreeBSD (9.1) the necessary package is /usr/ports/database/postgresql-server* which when installed will also install the required header file that makes the gem install of "pg" fail. This answer here helped me find the solution but the difference in package names required a bit of searching.
Hopefully this helps someone avoid a bit of head scratching when searching for the "-dev" package on a FreeBSD system!
On Debian 7.0, 64-bit (Wheezy), just run:
sudo apt-get install libpq-dev
After you successfully installed libpq-dev, run:
bundle install