installing nokogiri ruby gem with a native extension - ruby

I want to install a ruby gem which tries to build a native extension. The gem in this case is nokogiri. If I do gem install nokogiri, the native extension dynamically links against libxml, libxslt libs. I want to statically link against those libs. How should I go about this?

Here are some pointers, but's it's not easy unless nokogiri contains build flags to support it:
If nokogiri supports it, you can pass build arguments to install gem like this
gem install nokogiri -- --with-static-libxml
If there is no built in support you can try tweaking linkflags used to install the gem with:
gem install nokogiri -- --with-ldflags='-static'
It's likely that the build will fail, since --with-ldflags overrides all LDFLAGS, and also '-static' tries to link everything as static, so you need to examine mkmf.log, and treat it accordingly.
If you want to do it manually, one way to do it is making the gem install fail by invoking with invalid option like:
gem install nokogiri -- --with-ldflags
This will cause the installation to fail with a message like this:
Building native extensions. This could take a while...
ERROR: Error installing nokogiri:
ERROR: Failed to build gem native extension.
ruby extconf.rb --with-ldflags
So you should be able to build the gem yourself then after it's done finish the installation with (see gem help install):
gem spec ../../cache/nokogiri-1.4.1.gem --ruby > \
../../specifications/nokogiri-1.4.1.gemspec

Try:
sudo apt-get install libxslt-dev libxml2-dev
I resolve this problem.

To install nokogiri gem first you need to install libxslt-dev and libxml2-dev
sudo apt-get install libxslt-dev libxml2-dev
And then install nokogiri gem
sudo gem install nokogiri
Afterwards you need to install the bundle
bundle install
Last but not least the installed gem needs to be defined within your Gemfile
gem 'nokogiri'
These steps worked flawlessly for me.

Error Msg : error: error installing nokogiri: error: failed to build
gem native extension.
I fixed this issue just by running the these two commands :
$ sudo apt-get install libxml2 libxml2-dev libxslt libxslt-dev?
$ gem install nokogiri
And it worked fine..!! :)

If you're using RVM, run rvm requirements and install the list of libraries at # For Ruby / Ruby HEAD.
That should work

I fix this issue by:
sudo apt-get install libxml++1.0-dev
The really issue here is because this nokogiri depends on libxml1, but default install is libxml2....

sudo apt-get install libxslt1-dev libxml2-dev works for me. As of July 31, 2013

Related

Error installing nokogiri: invalid gem: package is corrupt

I'm trying to parse a website by using jekyll on my Debian x64 and I get a problem with this following gem nokogiri , in particular I've been required on install the version 1.8.5 , but as it was not working I attempted downloading the last version nokogiri (1.11.0.rc2) Manualy from the web and now I'm stuck with this error.
bundle exec jekyll serve
Could not find nokogiri-1.8.5 in any of the sources
Run `bundle install` to install missing gems.
bundle install
Bundler::GemspecError: Could not read gem at
/var/lib/gems/2.7.0/cache/nokogiri-1.8.5.gem. It may be corrupted.
An error occurred while installing nokogiri (1.8.5), and Bundler
cannot continue.
Make sure that `gem install nokogiri -v '1.8.5' --source
'https://rubygems.org/'` succeeds before bundling.
I think you don't have it's dependancy library libxml2 try running below code.
brew install libxml2
if not worked add nokogiri gem using below command
gem install nokogiri -- --use-system-libraries
[--with-xml2-config=/path/to/xml2-config]
[--with-xslt-config=/path/to/xslt-config]

Sass installation error

I use Linux Mint and I want to install sass. I have installed ruby by "sudo apt install ruby" , version 2.3 and then , when I wan to install sass by "sudo gem install sass --no-user-install" , I have the following error:
Building native extensions. This could take a while...
ERROR: Error installing sass:
ERROR: Failed to build gem native extension.
current directory: /var/lib/gems/2.3.0/gems/ffi-1.9.18/ext/ffi_c
/usr/bin/ruby2.3 -r ./siteconf20171114-19329-157auxp.rb extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/include/ruby.h
extconf failed, exit code 1
Gem files will remain installed in /var/lib/gems/2.3.0/gems/ffi-1.9.18 for inspection.
Results logged to /var/lib/gems/2.3.0/extensions/x86_64-linux/2.3.0/ffi-1.9.18/gem_make.out
Install this package:
sudo apt install ruby-dev
The ruby-dev package contains the missing headers. You can see that the installer is looking for the headers:
/usr/lib/ruby/include/ruby.h
Installing this package will install those headers. This is a common issue that comes up for new Ruby users when installing gems with native extensions. You may find that you have to install several packages to get the installation to complete. If you see similar errors when installing this gem or other gems, try searching for "gemname native extension prerequisites", like "sass gem native extension prerequisites", to learn which packages should be installed before installing the gem.
Try this:
sudo apt install ruby-full rubygems autogen autoconf libtool make
sudo gem install sass
I had the same issue on Ubuntu 18.04, ruby-dev itself didn't help, installing g++ and make, as stated here solved my issue.

Ruby 'bundle install' error when following 'Heroku getting started': Failed to build gem native extension

I followed Heroku getting started from the beginning up to 'bundle install' (step #declare-app-dependencies) and saw an error. Some dependencies seem missing but I don't know how to find what they are. Can anybody tell what I should do?
$ bundle install
Fetching gem metadata from https://rubygems.org/...........
Resolving dependencies...
Using rake 10.3.2
Using i18n 0.6.9
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/usr/bin/ruby2.0 extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/include/ruby.h
Gem files will remain installed in /tmp/bundler20140919-3839-1mafrop/json-1.8.1/gems/json-1.8.1 for inspection.
Results logged to /tmp/bundler20140919-3839-1mafrop/json-1.8.1/gems/json-1.8.1/ext/json/ext/generator/gem_make.out
An error occurred while installing json (1.8.1), and Bundler cannot continue.
Make sure that `gem install json -v '1.8.1'` succeeds before bundling.
Environment =
linuxmint 64bit as VM guest
ruby 2.0.0p299 (2013-08-29) [x86_64-linux-gnu]
gem -v = 2.0.7
gem list --local = bundler (1.7.3), i18n (0.6.9), rake (10.3.2)
installed Heroku Toolbelt
using the example code as in the instructions
After hours of trial I could finally bundle install. I did
sudo apt-get purge ruby*
sudo apt-get install ruby
sudo apt-get install ruby-dev
sudo gem install bundler
After these, bundle install passed json-1.8.1 without error. Then error came
...
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
...
so I did
sudo apt-get install libpq-dev
Then bundle install again. And it read Your bundle is complete!.
For your reference, I did the following as well. I do not know if the Heroku getting started example really needs them.
sudo apt-get install postgresql postgresql-contrib
sudo apt-get install libmagickwand-dev python-dev

bundle install fails on nokogiri

I'm installing some chef dependencies following this website:
https://learnchef.opscode.com/starter-use-cases/multi-node-ec2/
I got to the bundle install part, here's what my Gemfile looks like:
source 'https://rubygems.org'
gem 'berkshelf'
gem 'chef'
gem 'knife-ec2'
I get this error when I try to run
bundle install --path vendor:
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
...
libiconv is missing. please visit http://nokogiri.org/tutorials/installing_nokogiri.html for help with installing dependencies.
...
An error occurred while installing nokogiri (1.6.0), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.6.0'` succeeds before bundling.
I went to the nokogiri site and I was able to follow the directions and successfully install nokgiri 1.6.0 with homebrew .9.5:
nokogiri --version
WARNING: Nokogiri was built against LibXML version 2.9.1, but has dynamically loaded 2.8.0
# Nokogiri (1.6.0)
I get the same message when I then try running the bundle install again. I'm told that the bundle installer doesn't care about installs done outside of it. How to I get around this and install these dependencies?
After a little digging, I figured it out. This is specifically for OSX Mountain Lion.
The rbenv bundler needs to know the same paths specified using these switches given by the nokogiri site:
http://nokogiri.org/tutorials/installing_nokogiri.html
This is done using the bundler config command:
bundle config build.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.13.1/include --with-iconv-lib=/usr/local/Cellar/libiconv/1.13.1/lib
I still ran into troulbe because the config was only picking up the first line of that config setting. I had to edit $HOME/.bundle/config and take out some newlnes before it would take all of the switches. I hope this will save someone else some time.

Why isn't Nokogiri installing?

I'm having trouble installing Nokogiri. When I run bundle install or gem install nokogiri the installation fails.
The error I'm getting is:
(Note: This failure is from using the installation command on nokogiri.org)
Building native extensions. This could take a while...
ERROR: Error installing nokogiri:
ERROR: Failed to build gem native extension.
/Users/roneesh/.rbenv/versions/1.9.3-p194/bin/ruby extconf.rb --with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28/ --with-iconv-include=/usr/local/Cellar/libiconv/1.14/include --with-iconv-lib=/usr/local/Cellar/libiconv/1.14/lib
Extracting libxml2-2.8.0.tar.gz into tmp//ports/libxml2/2.8.0... OK
Running 'configure' for libxml2 2.8.0... ERROR, review 'tmp//ports/libxml2/2.8.0/configure.log' to see what happened.
*** 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.
The command I'm trying to use is:
gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.9.1/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.9.1/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28/ --with-iconv-include=/usr/local/Cellar/libiconv/1.14/include --with-iconv-lib=/usr/local/Cellar/libiconv/1.14/lib
I have installed xml2, xslt and libiconv all with brew, and put in their proper versions above. Still no resolution. The only thing I haven't done is libiconv from source (my wget command isn't working for some reason).
You need to specify nokogiri to use the system libraries instead, so it doesn't try to build them itself.
NOKOGIRI_USE_SYSTEM_LIBRARIES=1 bundle install
Answer found here: Error installing nokogiri 1.6.0 on mac (libxml2).
Setting NOKOGIRI_USE_SYSTEM_LIBRARIES=1 also did the trick for me, but I had to install the native libraries the gem depends on first (I did it with Homebrew) and then tell the gem to use them.
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"

Resources