Nokogiri includes its own updated and patched copies of libxml2 and libxslt libraries. By default, installation of Nokogiri will use these copies. Alternatively, you may install using your operating system's built-in libraries or other custom versions of these libraries.
This document also helps with common problems we've heard. If you have an issue not discussed here, please open an issue.
For earlier versions of Nokogiri, find instructions in the git history for this document.
For other platforms, please send pull requests to sparklemotion/nokogiri.org-tutorials.
Install Nokogiri on a brand new Ubuntu system with these commands:
sudo apt-get install build-essential patch ruby-dev zlib1g-dev liblzma-dev
gem install nokogiriNote for RVM users: you may require libgmp, consider running sudo apt-get install libgmp-dev.
Install Nokogiri on a brand new FreeBSD system with these commands:
gem install nokogiriUse gcc from ports in order to compile the included libraries:
pkg_add -v gcc
gem install nokogiriIf you're using RubyInstaller to install Ruby, then you can install Nokogiri's pre-compiled gem to avoid having to install a compiler toolchain:
gem install nokogiriNote that gem will download and use a version of the gem appropriate for your system and architecture (e.g., x64-mingw32, x86-mingw32, etc.).
Nokogiri source gems can also be compiled and installed using RubyInstaller plus Devkit. This might be necessary if the binary gem doesn't work with your ruby version. (However, if a brand new Ruby version has just come out, give the maintainers a week or so to cut a compatible version of the gem!)
Install Nokogiri by compiling the vendored libxml/libxslt libraries:
gem install nokogiri --platform=rubyAlternatively, install Nokogiri by linking to MSYS2 system libraries:
ridk exec pacman -S mingw-w64-x86_64-libxslt
gem install nokogiri --platform=ruby -- --use-system-librariesInstall Nokogiri on a brand new Fedora, Red Hat, or CentOS system with these commands:
dnf install -y make gcc rpm-build ruby-devel zlib-devel
gem install nokogiriAlternatively, you may install the appropriate epel-release and get the Nokogiri package from EPEL using:
sudo dnf install -y rubygem-nokogiriInstall on any Linux distribution using GNU Guix, a reproducible binary software package management and distribution system.
Use this command:
guix package -i ruby-nokogiriNote: source code is available here. A short description of how Nokogiri was packaged can be found here.
The official ruby-alpine Docker images are
stripped of their development tools to minimize size. To install nokogiri (or
other gems with native extensions) you'll need to install build tools again via
the build-base meta-package
(which includes gcc and other necessities).
FROM ruby:2.6-alpine
RUN apk add --no-cache build-base
RUN gem install nokogiriAlthough TermUX isn't fully supported right now, some people have reported success getting Nokogiri installed on it by running these commands:
pkg install ruby clang make pkg-config libxslt
gem install nokogiri -- --use-system-librariesFirst, make sure you have the latest version of RubyGems and xcode commandline tools:
gem update --system
xcode-select --install # Then agree to the terms, even if you have done this before!Agree to the Xcode license:
sudo xcodebuild -licenseThen install nokogiri:
gem install nokogiriThis is verified working on maxOS 10.9 w/ Xcode's clang compiler. (Many thanks to @allaire and others for helping!)
If, you see this dialog when you run the above commands:
Then run this command to turn off forced-authentication with Apple Software Update:
sudo defaults delete /Library/Preferences/com.apple.SoftwareUpdate CatalogURLA more recent error mentions an undeclared identifier LZMA_OK:
xmlIO.c:1450:52: error: use of undeclared identifier 'LZMA_OK'
ret = (__libxml2_xzclose((xzFile) context) == LZMA_OK ) ? 0 : -1;
^
1 error generated.
The solution for this is a little more subtle and can be fixed in a couple of ways.
-
When using Homebrew, there are several libraries that use a formula called
xz(includingthe_silver_searcherandimagemagick), which by default install a version ofliblzmathat is incompatible with most Ruby builds. (Homebrew installs only the 64-bit version of the library, but most Ruby builds are universal.) This can be fixed in a couple of ways:a. The most reliable way appears to be temporarily unlinking
xzand relinking it during an install ofnokogiri:```sh brew unlink xz gem install nokogiri # or bundle install brew link xz ```b. The third way is to use a Homebrew-installed
libxml2, as suggested in using your system libraries.```sh brew install libxml2 # If installing directly gem install nokogiri -- --use-system-libraries \ --with-xml2-include=$(brew --prefix libxml2)/include/libxml2 # If using Bundle bundle config build.nokogiri --use-system-libraries \ --with-xml2-include=$(brew --prefix libxml2)/include/libxml2 bundle install ``` When working with this, be certain to use `$(brew --prefix libxml2)` because it will use the correct location for your Homebrew install.
Xcode 10 on macOS Mojave moves the system headers out of /usr/include
and so Nokogiri will fail to build. Instead you'll see an error similar to this:
Building nokogiri using packaged libraries.
-----
libiconv 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.A temporary workaround to allow previous releases of Nokogiri to build is to install the extra headers package mentioned in the Xcode 10 release notes:
open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkgYou can also install the headers package from the command line (e.g. for a build script or a CI server):
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /- Make sure ruby is compiled with the latest clang compiler.
- Binary gems and ruby should be compiled with the same compiler/environment.
- If you have multiple versions of Xcode installed, make sure you use the
right
xcode-select. - Try installing with system libraries.
If reporting an issue about the macOS installation instructions, please mention @zenspider.
Nokogiri will refuse to build against certain versions of libxml2, libxslt
supplied with your operating system, and certain versions will cause mysterious
problems. The compile scripts will warn you if you try to do this.
On Debian/Ubuntu:
sudo apt-get install pkg-config
On FreeBSD:
sudo pkg install pkgconf
Using gem:
gem install nokogiri -- --use-system-librariesOr, use Bundler:
bundle config build.nokogiri --use-system-libraries
bundle installIf:
- you've got
libxml2and/orlibxsltinstalled in a nonstandard place, - and you don't have
pkg-configinstalled
you can use command-line parameters to the gem install command to
specify build parameters.
If you've got the proper config scripts:
gem install nokogiri -- \
--use-system-libraries \
--with-xml2-config=/path/to/xml2-config \
--with-xslt-config=/path/to/xslt-configor, you can specify the installation root directory:
gem install nokogiri -- \
--use-system-libraries \
--with-xml2-dir=/path/to/dir \
--with-xslt-dir=/path/to/diror, you can specify include and library directories separately:
gem install nokogiri -- \
--use-system-libraries \
--with-xml2-lib=/path/to/builds/lib \
--with-xml2-include=/path/to/builds/include/libxml2 \
--with-xslt-lib=/path/to/builds/lib \
--with-xslt-include=/path/to/builds/includeNote: By default, libxslt header files are installed into the
root include directory, but libxml2 header files are installed into a
subdirectory thereof named libxml2.
It's likely that you'll also need to specify the location of your
zlib and iconv (and possibly exslt) install directories as
well. In that case, you can add the options:
gem install nokogiri -- \
--use-system-libraries \
# ...
--with-iconv-dir=/path/to/dir \
--with-zlib-dir=/path/to/dir \
[--with-exslt-dir=/path/to/dir]
[--with-exslt-config=/path/to/exslt-config]bundle config build.nokogiri \
--use-system-libraries \
--with-xml2-lib=/usr/local/lib \
--with-xml2-include=/usr/local/include/libxml2/libxml \
--with-xslt-lib=/usr/local/lib \
--with-xslt-include=/usr/local/include/libxslt \
--with-iconv-lib=/usr/local/lib \
--with-iconv-include=/usr/local/include
Do not attempt Bundler installation using Bundler versions before v1.8.3. See bundler/bundler#3053. But if you really want to, see earlier git history of this file, which includes a workaround.
To compile against Alpine's own XML libraries, add the necessary development tools and libraries to the image.
FROM ruby:2.6-alpine
RUN apk add --no-cache build-base libxml2-dev libxslt-dev
RUN gem install nokogiri -- --use-system-librariesWhen optimizing the size of an Alpine image, the runtime libraries must be permanently added. Additionally, adding and removing development tooling can be chained with gem installation to ensure a small layer.
FROM ruby:2.6-alpine
RUN apk add --no-cache libxml2 libxslt && \
apk add --no-cache --virtual .gem-installdeps build-base libxml2-dev libxslt-dev && \
gem install nokogiri -- --use-system-libraries && \
rm -rf $GEM_HOME/cache && \
apk del .gem-installdepsThis approach nets an 12.1 MB layer (versus 18.1 MB without --use-system-libraries)
and saves over 170 MB in build tools.
SmartOS installation requires building and using libxml2/libxslt/libiconv in a nonstandard location. Building on the previous section, here's how to do it:
(Note: pkgsrc is included in JPC SmartOS instances)
pkgin install ruby gcc49 libxml2 libxslt zlib libiconv ruby22-rake gmake
ln -s /opt/local/gcc49/bin/gcc /opt/local/bin/gcc
gem install nokogiri -- \
--use-system-libraries \
--with-xml2-lib=/opt/local/lib \
--with-xml2-include=/opt/local/include/libxml2 \
--with-xslt-lib=/opt/local/lib \
--with-xslt-include=/opt/local/include/libxslt \
--with-iconv-lib=/opt/local/lib \
--with-iconv-include=/opt/local/include \
--with-zlib-dir=/opt/local/libSee the previous section for guidance on how to instruct Bundler to use these options.
