RubyInstaller 2.2.1 and Rails – Rake cannot load Nokogiri

I think I got it working on my system:

enter image description here

I don’t know what was done to get this working, but I’ll share what I did:

  1. The problem for Windows with Ruby 2.2+ & Nokogiri is that the gem doesn’t compile. Nokogiri requires libXML, libxslt & libiconv in order for it to work. These are nominally compiled at gem install, but for whatever reason, this does not occur in Ruby 2.2+.

  2. Therefore, in order to install the gem (this is exactly the same situation with mysql2 and rmagick), you need the system dependencies (mentioned above).

  3. From Ruby 2.2+, it seems that gems will “install” even if they don’t have their dependencies on the system (as opposed to refusing the install by not building native extensions in previous versions). The new runtime errors which appear include cannot load such file -- mysql2/2.2/mysql2 (LoadError) and the corresponding one for nokogiri (cannot load such file -- nokogiri/nokogiri).

  4. With this in mind, you have to appreciate how the gems are installed & work. A good example is the mysql2 gem – to install it, you need to download the MYSQL C-Connector plugin and then link to the dependency with the following code: gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:\mysql-connector-path"'

  5. With Nokogiri, you need to have libxml, libiconv and libxslt on your system. I learnt that from this post:

enter image description here

  1. The problem arises here.
    I am not 100% sure what I did here to get this working (even temporarily). I know that I installed the Nokogiri gem, and then set about compiling the gem using ruby extconf.rb (which is what the gem does anyway). Considering this what I feel worked, I’ll explain how this was performed.

  2. The gem will typically download the libraries through install. It keeps these in the ext/tmp/ports folder. For my system, the download of libiconv was what prevented the install from completing (error about CPPFLAGS). With this in mind, I figured that if the gem installed, and if it was trying to build, it would be prudent to get the dependencies installed.

  3. Thus, I worked on the ruby ext/extconf.rb process using the --use-system-libraries switch:

"...\nokogiri>ruby extconf.rb --platform=ruby -N --use-system-libraries --w
ith-xml2-dir=C:\Users\Richard\Downloads\Ruby\libxml2-2.7.8.win32 --with-xml2-include=C:\Users
\Richard\Downloads\Ruby\libxml2-2.7.8.win32\include --with-xml2-lib=C:\Users\Ric
hard\Downloads\Ruby\libxml2-2.7.8.win32\lib --with-iconv-dir=C:\Users\Richard\Do
wnloads\Ruby\iconv-1.9.2.win32 --with-iconv-include=C:\Users\Richard\Downloads\R
uby\iconv-1.9.2.win32\include --with-iconv-lib=C:\Users\Richard\Downloads\Ruby\i
conv-1.9.2.win32\lib --with-zlib-dir=C:\Users\Richard\Downloads\Ruby\zlib-1.2.5"

I coupled this with downloading the aforementioned libraries (and some which didn’t work):

enter image description here

  1. I don’t have a record of the output of the above command, but I am pretty sure it built the extensions as required, ending in saying a “Makefile” had been compiled. When a Makefile is available, you should be able to use nmake (Windows 7.1 SDK) or make (MingW) to get it to run. I did this, and it seemed to work.

  2. I tried loading the server today, and it appeared to work.

That’s the best I have right now.

I am available to answer comments etc as required.

Leave a Comment