How do I install Nokogiri gem for web scraping in Ruby?

To install the Nokogiri gem for web scraping in Ruby, you need to have Ruby and RubyGems installed on your system. Nokogiri is a very popular and powerful parsing library that can parse both HTML and XML in Ruby.

Here's how to install Nokogiri:

  1. Open your terminal or command prompt.

  2. Install the Nokogiri gem by running the following command:

gem install nokogiri

This command will fetch the latest version of Nokogiri and install it. If you need a specific version of Nokogiri, you can specify it by appending -v VERSION_NUMBER to the command.

  1. Confirm the installation. After the installation is complete, you can check if Nokogiri was installed successfully by querying the installed gems:
gem list nokogiri

If Nokogiri is installed, you should see it listed along with its version number.

Installing on Windows

Windows users might encounter issues due to Nokogiri's native extensions, which require a proper build environment. To avoid these issues, the Nokogiri team provides pre-compiled native gems for Windows users. Running gem install nokogiri on Windows should automatically fetch and install these pre-compiled versions.

Installing on macOS

macOS users might need to install additional libraries like libxml2, libxslt, and libiconv. However, Nokogiri bundles these libraries, and it should work out of the box. If you encounter any issues, it could be due to the presence of these libraries installed via Homebrew or MacPorts. In such cases, you might need to specify the location of these libraries during the gem installation:

gem install nokogiri -- --use-system-libraries \
    --with-xml2-config=/path/to/xml2-config \
    --with-xslt-config=/path/to/xslt-config

Make sure to replace /path/to/xml2-config and /path/to/xslt-config with the actual paths to your system libraries.

Installing on Linux

On Linux, you may need to install the development packages for libxml2 and libxslt before installing Nokogiri. For example, on Ubuntu/Debian systems, you can install these packages using apt-get:

sudo apt-get install build-essential patch ruby-dev zlib1g-dev liblzma-dev libxml2-dev libxslt1-dev

After installing the necessary dependencies, you should be able to install Nokogiri using the gem install nokogiri command.

Using Bundler

If you are using Bundler for managing your Ruby project's dependencies, add Nokogiri to your Gemfile:

gem 'nokogiri'

And then run:

bundle install

This will install Nokogiri along with other gems specified in your Gemfile.

Following these steps should help you successfully install Nokogiri on your system. Once installed, you can use Nokogiri to parse HTML/XML and perform web scraping tasks in your Ruby applications.

Related Questions

Get Started Now

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