Help installing Ruby gem "nokogiri"

Can anyone help me install the Ruby gem “nokogiri”?

When I do gem install nokogiri, I get:

Building native extensions. This could take a while...
ERROR:  Error installing nokogiri:
	ERROR: Failed to build gem native extension.

    current directory: /root/.gem/ruby/2.5.0/gems/nokogiri-1.10.1/ext/nokogiri
/nix/store/77i8a6rqnaqsc9av37xfw4sw4p61nfka-ruby-2.5.3/bin/ruby -r ./siteconf20190309-13334-rsa6rp.rb extconf.rb
checking if the C compiler accepts ... yes
Building nokogiri using packaged libraries.
Using mini_portile version 2.4.0
checking for gzdopen() in -lz... no
zlib is missing; necessary for building libxml2
*** 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:

To see why this extension failed to compile, please check the mkmf.log which can be found here:


extconf failed, exit code 1

Gem files will remain installed in /root/.gem/ruby/2.5.0/gems/nokogiri-1.10.1 for inspection.
Results logged to /root/.gem/ruby/2.5.0/extensions/x86_64-linux/2.5.0/nokogiri-1.10.1/gem_make.out

mkmf.log contains the lines:

conftest.c:3:10: fatal error: zlib.h: No such file or directory
compilation terminated.

It looks like zlib can’t be found. But I installed zlib with nix-env -iA nixos.zlib, and I am still getting this error.

Did you try running this in a nix-shell -p zlib shell?

1 Like

Libraries installed either as systemPackages or through nix-env probably won’t work as expected, by design. The NixOS Wiki has the FAQ/Libraries entry explaining how to use them.

@erictapen’s suggestion should work, that is, using a nix-shell -p zlib (which brings in stdenv and gcc). You probably also want to uninstall any globally installed gcc.

1 Like

Using nix-shell -p zlib worked. Thanks!

Would this be solved by using --pure?

That was a generic tip, where in globally installing compilers is likely not to work entirely as expected. AFAIK it is assumed that compilers won’t be installed globally on NixOS. Though, yeah, using --pure would help if it caused issues.