Error "...cannot load such file -- mysql2/2.0/mysql2 (LoadError)". On Windows XP with Ruby 2.0.0

Green picture Green · Mar 24, 2013 · Viewed 14.8k times · Source

The command rails server throws this error.

C:/Ruby200/lib/ruby/gems/2.0.0/gems/mysql2-0.3.11-x86-mingw32/lib/mysql2/mysql2.rb:2:in `require': cannot load
 such file -- mysql2/2.0/mysql2 (LoadError)

I use Ruby 2.0.0 from RubyInstaller on Windows XP box.

I figured out what is the problem but I don't know how to solve it. The problem is that there is no any 2.0/ directory in the mysql2-0.3.11-x86-mingw32 gem. This is the gem that rails installs as its dependency from Gemfile:

GEM
  remote: https://rubygems.org/
  specs:
     ... many gems here
     mysql2 (0.3.11-x86-mingw32)
     ... many gems here
DEPENDENCIES
  ...
  mysql2
  ...

This is what is in that mysql2.rb file:

# C:\Ruby200\lib\ruby\gems\2.0.0\gems\mysql2-0.3.11-x86-mingw32\lib\mysql2\mysql2.rb

RUBY_VERSION =~ /(\d+.\d+)/
require "mysql2/#{$1}/mysql2"  # <<-- this is that #2 line that throws an error

It is obvious that it takes the current Ruby version number and uses it as the path segment to reach some mysql2 file. In fact it is mysql2.so file. As I use Ruby 2.0.0 the path segment is 2.0:

mysql2/2.0/mysql2

Ok, now lets see how that directory of mysql2-0.3.11-x86-mingw32 gem looks like:

dir: C:\Ruby200\lib\ruby\gems\2.0.0\gems\mysql2-0.3.11-x86-mingw32\lib\mysql2\

enter image description here

No any 2.0/ directory.

I know the issue about libmysql.dll. I have it in my C:\Ruby200\bin. It doesn't help.

I read this answer https://stackoverflow.com/a/5368767/1114926 from the creator of RubyInstaller. I tried but it didn't help. It works for Ruby 1.9.3 because there is 1.9/ directory. But it doesn't work for Ruby 2.0.0.

How to solve it?


UPD 1:

Thank you for your answer. I've tried. Unfortunately I have ERROR: Failed to build gem native extension. error:

C:\>gem install mysql2 --platform=ruby
Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
ERROR:  Error installing mysql2:
        ERROR: Failed to build gem native extension.

    C:/Ruby200/bin/ruby.exe extconf.rb
checking for rb_thread_blocking_region()... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=C:/Ruby200/bin/ruby
C:/Ruby200/lib/ruby/2.0.0/mkmf.rb:431:in `try_do': The compiler failed to generate an executable file. (Runtim
eError)
You have to install development tools first.
... other code follows here...

It says:

You have to install development tools first.

But I have full DevKit installed, RubyInstaller installs it. Can't understand what else does it need.

I've already posted an issue to mysql2 GitHub page https://github.com/brianmario/mysql2/issues/364. No answers yet.

Answer

odiszapc picture odiszapc · Apr 6, 2013

Had the absolutely same issue on Windows 7 x64 with Ruby 2.0.0 and DevKit 4.7.

The following steps helped me.

  1. gem uninstall mysql2

  2. Download last MySQL connector from http://cdn.mysql.com/Downloads/Connector-C/mysql-connector-c-noinstall-6.0.2-win32.zip

  3. Extract it to C:\connector-6.0.2

  4. gem install mysql2 --platform=ruby -- '--with-mysql-lib="C:\connector-6.0.2\lib" --with-mysql-include="C:\connector-6.0.2\include" --with-mysql-dir="C:\connector-6.0.2"'

Or even shorter:

gem install mysql2 --platform=ruby -- --with-opt-dir="C:\connector-6.0.2"