'require': cannot load such file -- 'nokogiri\nokogiri' (LoadError) when running `rails server`
Asked Answered
S

4

71

I'm running a clean install of Ruby 2.2.1 on Windows 8.1 with DevKit. After the installation I run:

gem install rails
rails new testapp
cd testapp
rails server

leaving everything else at default.

The process fails at the last line when, instead of running the server, I get the error message

in 'require': cannot load such file -- 'nokogiri\nokogiri' (LoadError)

It happens every time and I've looked around and tried everything I found to fix it, but nothing so far has worked.

What is the problem here and how do I get a simple test Rails app to work?

Surtout answered 12/3, 2015 at 0:24 Comment(3)
have you run gem install nokogiri ? This will at least tell you why it wasn't installed as a dependency of Rails.Godred
run bundle install on rails directoryCreamery
It's a problem with Ruby2.2Quadruple
R
64

Nokogiri doesn't support Ruby 2.2 on Windows yet. The next release will. See https://github.com/sparklemotion/nokogiri/issues/1256

Nokogiri doesn't support native builds (e.g. with devkit) on Windows. Instead it provides gems containing prebuilt DLLs.

There's a discussion which you may want to join or watch on the topic of devkit build support here: https://github.com/sparklemotion/nokogiri/issues/1190

Respect answered 12/3, 2015 at 10:50 Comment(2)
Thanks for the info, that clears it up. For now I'll just move to linux to work on rails.Surtout
Even though I am on MacOS and had an issue with nokogiri on a completely different version, this made enough sense to try and upgrade my Ruby. It worked for me, so I'm leaving this comment to say to anyone not on Windows: Try upgrading ruby if you are spinning up a new project like I was.Papyrus
W
39
  1. First, uninstall the version of Nokogiri you currently have with:

    gem uninstall nokogiri
    
  2. Download Nokogiri 1.6.6.2 (x64) or Nokogiri 1.6.6.2 (x86)

  3. Install this version locally using:

    gem install --local C:\Users\$user$\Downloads\nokogiri-1.6.6.2-x64-mingw32.gem
    

    or if you're running 32bit Ruby:

    gem install --local C:\Users\$user$\Downloads\nokogiri-1.6.6.2-x86-mingw32.gem
    

    The path may differ depending on where you downloaded the file to.

Try to start the server again using ruby bin\rails server, and it should work.

Westphalia answered 12/10, 2015 at 4:17 Comment(1)
Beautiful, it works! Still stupid that Nokogiri doesn't support 2.2.x or above on Windows though...Orme
A
36

I got Nokogiri running with Ruby 2.2 on Windows 10 with a mix of Mike Dalessios and Julios answer:

  1. Look for the latest version of Nokogiri in Nokogiri's github repo.
  2. Run gem uninstall nokogiri.
  3. Add gem "nokogiri", ">= 1.6.7.rc" to your Gemfile.
  4. Run bundle install.
  5. Run bundle update nokogiri if bundle has locked Nokogiri at some version.
Aperient answered 29/10, 2015 at 9:20 Comment(0)
Q
6

enter image description here

Fix

  1. Bundle install (gets Nokogiri files)
  2. Browse to ruby_dir\lib\ruby\gems\2.2.0\gems\nokogiri-1.6.6.2\ext\nokogiri
  3. Open extconf.rb
  4. Add dir_config('iconv').any? or pkg_config('libiconv') to #376
  5. Download MinGW64 & MSYS folders from Mega
  6. Add them to PATH in Windows (remove Devkit path refs - it doesn't work)
  7. Download libxml2,libxslt, iconv libraries (or here)
  8. Run ruby extconf.rb --platform=ruby --n --use-system-libraries referencing downloaded libraries
  9. Run make
  10. Run make install

Steps

Bundle Install

First step is to bundle.

This will put the nokogiri gem on your machine without running the pre-packaged compiler (which mostly doesn't work in Windows).

This will show Nokogiri as installed:

enter image description here

Browse

Browse to the nokogiri folder, to find ext/nokogiri/extconf.rb:

enter image description here

Open extconf.rb

... and add dir_config('iconv').any? or pkg_config('libiconv') to #376

enter image description here

Standard Nokogiri installs "rely" on the libxml2 inclusion of iconv - we need to explicitly define it, otherwise iconv.h is missing errors will occur.

Add Toolchain

Don't use devkit for this - it doesn't work.

You need MinGW:

enter image description here

I have zipped my exact MinGW64 and MSYS64 folders on Mega (key: !FJtcq25l-QMsNltCxllMhc1IGqORvap8xv8gWxSUbDA):

enter image description here

Add to PATH

This gives access to gcc & make (both required):

enter image description here

Remove the devkit ref from your path, and add the following:

  • MINGW64_PATH/bin
  • MSYS64_PATH/bin

Download Libs

I have added the libs to Mega:

enter image description here

You will unzip them here:

enter image description here

All the libs are from this source.

Run extconf.rb

Once libs are on your system, you can run ruby extconf.rb to configure the build:

enter image description here

32bit

ruby extconf.rb --platform=ruby -N -- --use-system-libraries --with-xml2-dir=C:/Dev/Dependencies/Ruby/lib/nokogiri/32bit/libxml2-2.9.2-win32-x86 --with-xml2-include=C:/Dev/Dependencies/Ruby/lib/nokogiri/32bit/libxml2-2.9.2-win32-x86/include/libxml2 --with-iconv-dir=C:/Dev/Dependencies/Ruby/lib/nokogiri/32bit/iconv-1.14-win32-x86 --with-xslt-dir=C:/Dev/Dependencies/Ruby/lib/nokogiri/32bit/libxslt-1.1.28-win32-x86

64bit

#64 ruby extconf.rb --platform=ruby -N -- --use-system-libraries --with-xml2-dir=C:/Dev/Dependencies/Ruby/lib/nokogiri/64bit/libxml2-2.9.2-win32-x86_64 --with-xml2-include=C:/Dev/Dependencies/Ruby/lib/nokogiri/64bit/libxml2-2.9.2-win32-x86_64/include/libxml2 --with-iconv-dir=C:/Dev/Dependencies/Ruby/lib/nokogiri/64bit/iconv-1.14-win32-x86_64 --with-xslt-dir=C:/Dev/Dependencies/Ruby/lib/nokogiri/64bit/libxslt-1.1.28-win32-x86_64

make

enter image description here

This may create errors / warnings, as long as it says "Error 1 (ignored)", it should be okay.

Following that, use make install:

enter image description here

Then browse to your Rails installation and run rails s:

enter image description here


Explanation

To give context:

Ruby 2.2+ on Windows doesn't compile the extensions Nokogiri requires.

The extensions of a gem are the extra dependencies (libraries) it uses.

They are built when you install the gem:

enter image description here


Extensions

Lack of extensions is preventing Nokogiri from running.

Extensions exist in the ext folder of a gem (you can read about them here):

enter image description here

Mysql2,RMagick,PGSQL, Nokogiri etc all use extensions/libraries.

This is why - on Windows - you have to use custom switches (--with-opt-dir) when installing the gem. This gives Ruby / the shell / (cmd) the required lib / include directories required to build the gem's files (it's the equivalent of how PATH works).

On Linux/Mac, these directories are managed with the respective package managers (brew/apt-get). Windows does not have this, so you have to install the extensions manually.

Because Windows does not have a standard set of libraries, you have to download them yourself. You also have to build them yourself (which is tricky).

The fix for Nokogiri install is to use the right libraries and build tools to get the gem installed.


Build

The difference with Ruby 2.2+ is the gem will "install" without showing any exceptions. You think it has installed, only to find Rails does not load (hence the nokogiri/nokogiri.so error).

This means you have to make sure you have the files on your system, and run the compiler to install them.

The above documentation should show you how to do that.

Quadruple answered 8/11, 2015 at 12:19 Comment(11)
I tried logging in with the key you provided but it said the url was no longer :( Any chance of getting the necessary components from you?Enchanting
Sure, let me fix up the repoQuadruple
MINGW64 / MSYS64 - mega.nz/#!c4tGRSTb!FJtcq25l-QMsNltCxllMhc1IGqORvap8xv8gWxSUbDAQuadruple
Just getting the libs sorted, I think Mega deleted themQuadruple
Regardless, the gem should work with the later versions of ruby. I was just interested in getting it working properly (no hacks) on Windows, hence my post. If you're using the later versions of Ruby, you should be able to install Nokogiri with no issuesQuadruple
Thanks - I am working on a friends project using Ruby 2.3.0 and they are mostly using OSX but have had nokogiri issues... I'm using win7 on an old 2009 i3 HP lappy and from the answer above I got something functional but I much prefer not having to modify the Gemfile - thanks!!Enchanting
NP If you want, I have some time and we could try and get it working over chat or somethingQuadruple
It's really a compilation issue; Windows can't compile anything without the correct librariesQuadruple
Also, your explanation of what's involved in getting it to work properly really helped me understand what is going on with rails and gem management. I'm new to dev w/rails and it's taking some getting used to!Enchanting
Yeah I'd almost given up with rails on windows!Enchanting
Let us continue this discussion in chat.Enchanting

© 2022 - 2024 — McMap. All rights reserved.