When I was setting up a Rails 3.2.1 project with Ruby 1.8.7 on a Linux WHM server, I ran into a frustrating issue while trying to install the Nokogiri gem. Nokogiri is essential because it helps with parsing XML and HTML, and in my case, I needed it for working with the Authorize.net gem.
I’ll walk you through what went wrong, how I figured it out, and how I tested Nokogiri with some practice coding once it was installed.
The First Code I Ran
The very first step I tried was installing Nokogiri with:
gem install nokogiri -v '1.5.0'
I expected it to just work. Instead, I got hit with this error:
checking for libxslt/xslt.h... no
libxslt is missing.
And later, when I tried to install libxslt-ruby
, I ran into another error:
error: ruby/io.h: No such file or directory
Understanding the Error
At first glance, these errors seemed cryptic. But breaking them down made the root cause clear:
- libxslt missing: Nokogiri depends on two system libraries
libxml2
andlibxslt
. My Linux server didn’t have the development headers forlibxslt
installed, so the gem couldn’t compile its native C extensions. - ruby/io.h missing: This header file belongs to Ruby’s core development package. While Ruby was installed, the “-devel” package (which provides compilation headers like
ruby/io.h
) wasn’t. Without it, building gems with native extensions is impossible.
In short: my system was missing the development libraries Nokogiri needed.
Installing the Right Dependencies
The fix turned out to be installing the missing system packages. Depending on the Linux distribution, here’s how I did it:
For CentOS/RedHat:
yum install -y libxml2-devel libxslt-devel ruby-devel make gcc
For Ubuntu/Debian:
sudo apt-get install -y build-essential libxml2-dev libxslt1-dev ruby-dev zlib1g-dev
Once I had these installed, running:
gem install nokogiri -v '1.5.0'
finally worked.
Practicing with Nokogiri
After resolving the installation problem, I wanted to test Nokogiri right away. I wrote a small Ruby script to practice parsing an HTML page.
# test_nokogiri.rb
require 'nokogiri'
require 'open-uri'
# Fetch and parse an HTML document
url = "https://www.ruby-lang.org/en/"
doc = Nokogiri::HTML(URI.open(url))
puts "Page title: #{doc.at('title').text}"
# Extract all links
puts "\nLinks found on the page:"
doc.css('a').each_with_index do |link, i|
puts "#{i + 1}. #{link.text.strip} -> #{link['href']}"
end
# Count paragraphs
paragraphs = doc.css('p')
puts "\nTotal paragraphs: #{paragraphs.count}"
# Show the first paragraph
if paragraphs.any?
puts "\nFirst paragraph:\n#{paragraphs.first.text.strip}"
end
Running the script:
ruby test_nokogiri.rb
What it does:
- Reads the Ruby website homepage
- Prints the page
<title>
- Lists all links with their text and URLs
- Counts how many
<p>
tags there are - Displays the first paragraph
This gave me confidence that Nokogiri was working as expected.
Final Thoughts
At first, I thought the Nokogiri installation error was something wrong with RubyGems or the gem itself. But in reality, it was simply missing system dependencies. Once I installed libxml2
, libxslt
, and Ruby development headers, everything fell into place. The key takeaway for me is this: whenever a Ruby gem fails to build native extensions, check for missing development libraries. It saves hours of frustration.