How do I install Nokogiri on Windows?

Nokogiri is a popular Ruby gem used for parsing HTML, XML, SAX, and Reader. Installing it on Windows can be a bit tricky because it requires native extensions that need to be compiled. However, the Nokogiri team has worked hard to mitigate these challenges by providing pre-compiled versions of the gem for Windows users.

Here are the steps to install Nokogiri on Windows:

Step 1: Install Ruby

If you haven't already, you need to install Ruby. The easiest way to install Ruby on Windows is by using RubyInstaller, which you can download from https://rubyinstaller.org/. Choose a version with Devkit, as it includes the necessary build tools required for compiling native extensions.

Step 2: Install Nokogiri

Once Ruby and the Devkit are installed, you can install Nokogiri using the gem command in your console or command prompt.

Here is the command:

gem install nokogiri

This command will automatically download and install the precompiled version of Nokogiri for your platform.

In case you encounter issues with the automatic installation, you can try the following command:

gem install nokogiri --platform=ruby -- --use-system-libraries

This command tells RubyGems to download and install the platform-specific version of Nokogiri and use system libraries (like libxml2 and libxslt) if they are available.

Step 3: Verify Installation

To check if Nokogiri was installed properly, you can run the following command:

gem list nokogiri

This will list the Nokogiri gem if it is installed. You can also run a Ruby script that requires Nokogiri to ensure that it works:

require 'nokogiri'

puts Nokogiri::VERSION

If you see the version number without any errors, Nokogiri is installed and ready to use.

Troubleshooting

If you face any issues during the installation, here are some common troubleshooting steps:

  1. Ensure that Ruby is added to PATH: When installing Ruby via RubyInstaller, make sure to check the option that says "Add Ruby executables to your PATH."

  2. Install the required development tools: Make sure that you install MSYS2 and the development toolchain via RubyInstaller.

  3. Install dependencies manually: In some cases, you might need to install the dependencies manually using MSYS2. You can open the MSYS2 console and install the required libraries:

pacman -Syu
pacman -S mingw-w64-x86_64-libxml2 mingw-w64-x86_64-libxslt
  1. Check for error messages: If the gem install command fails, the error message will often provide clues about what went wrong. Search online for the error message; chances are, someone else has faced the same issue and found a solution.

Following these steps should help you successfully install Nokogiri on your Windows system. If you continue to have difficulties, consider looking for help on the Nokogiri mailing list or checking their documentation for Windows-specific installation advice.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon