Ruby on Rails - cannot load such file -- mysql2/2.2/mysql2 (LoadError)

In step 3 above I had to use an extra pair of '-' and no quotes around the path.

gem install mysql2 -- --with-mysql-dir=C:\mysql-connector-dir

I'm using Win 7


I had the same problem so I switched to using RubyInstaller. I no longer have the problem.


I just got them same problem after updating from Ruby 2.1.5 to 2.2.1. The problem seems to be the mysql2 gem that's used by Rails as a binding to libmysql. Apparently it does not support the Ruby 2.2 branch but only 2.0 and 2.1.


The problem is that the mysql2 gem is not installing with its native dependencies. In previous versions of Ruby, this would have triggered the unable to build native gem extension error:

enter image description here

Something has changed in Ruby 2.2 so that you can install the gem without it building the native extension. This does not mean the problem is resolved however - and so you have to ensure you install the gem with the appropriate dependencies.

Tutorial:

  1. Download C-Connector from MYSQL (note your version of Ruby -- if you're using 64 bit, download the 64 bit version)

  2. Unzip / Install the MYSQL C-Connector folder onto your HDD (don't have any spaces in the path)

  3. Run the following command: gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:/your-mysql-connector-path"'

  4. This should install the gem with the files it needs

  5. Then you can copy libmysql.dll from C:/your-mysql-connector-path -> c:/your-ruby-install/bin`

The difference between this install and the previous one will be that this should mention that the native gem extensions are being installed (This could take a while), which suggests that Ruby is looking at the library.

This time, if it installs correctly, it should work for you.

--

If you're still seeing an error, you'll have to uninstall any existing versions of the mysql2 gem on your system:

How to install mysql2.

If you're getting an error, you just need to type gem uninstall mysql2, select all and then repeat the steps above.