Question

I'm trying to installing the mysql2 gem on windows 7 I downloaded the connector from the mysql site and placed the libmysql.dll in ruby200\bin

then do gem install mysql2

These are the results am I being dim here?

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()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
*** 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
        --with-mysql-dir
        --without-mysql-dir
        --with-mysql-include
        --without-mysql-include=${mysql-dir}/include
        --with-mysql-lib
        --without-mysql-lib=${mysql-dir}/
extconf.rb:37:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError
)


Gem files will remain installed in C:/Ruby200/lib/ruby/gems/2.0.0/gems/mysql2-0.
3.13 for inspection.
Results logged to C:/Ruby200/lib/ruby/gems/2.0.0/gems/mysql2-0.3.13/ext/mysql2/g
em_make.out
Was it helpful?

Solution

EDIT 30/09/2014

When this answer was posted the 64 bit rails installer wasn't the recommended version - it now seems people are starting to use it more. It should be noted when you download the MySQL Connector you need to download either 64 or 32bit to correspond to the version of rails you installed.

Amazingly I lucked upon an answer very early this morning as I happened to be looking for something else of a similar nature. I'm not quite sure why there isn't a single simple guide for this as it appears to be very straight forward!

For some reason just specifying the mysql-dir when you install the gem doesn't pick up with other sub directories so you need to set the parameters manually.

For anyone else experiencing the same problem, I did the following:

1) Download the MySql C Connector from: http://dev.mysql.com/downloads/connector/c/

NOTE Don't download the installer, download the ARCHIVE for your OS

Download either the 32bit or 64 bit ARCHIVE to correspond with the version of rails you installed.

2) Extract the file to C:\mysql-connector

3) Then ran:

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

Voila everything is working fine.

EDIT 30/01/2014

I just had to do a fresh install on a bricked machine and the command in step 3 didn't work, what did work was:

gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:\mysql-connector"'

I'm not quite sure what the difference is but this time it seems to be picking up the directories ok, so if the first one doesn't work try this one!

I think this has to do with how you go about installing rails, this time round I used the railsinstaller which seems to set the paths up correctly.

A lot of the outcome here seems to depend on the shell your using, a lot of people are having problems with powershell so I wouldn't advise using it. I did this in an elevated command prompt.

Oh and lastly if you get an error regarding the mysql2 gem when you do RAILS S you need to copy the libmysql.dll from the LIB directory of the mysql connector to the bin directory where rails has been installed.

OTHER TIPS

I've tried the solution of @Mrk Fldig but it didn't work... So what solved the problem was:

  1. Downloaded the lastest MySQL Installer for windows 7 32 bits
  2. Installed the gem with the following command: gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:/Program Files/MySQL/MySQL Connector C 6.1 6.1.2/"'

One pitfall to be aware of is that I changed the backslashes (\) to normal slashes (/). I've tried the same procedure with backslashes and it didn't work.

The installer already includes the C connectors for MySQL at MySQL Connector C 6.1 6.1.2 directory. Therefore, passing only the --with-mysql-dir parameter without the --with-mysql-lib or --with-mysql-include parameters, makes the gem to look at the same directory for the lib and include directories

Steps for Ruby 2.1.3, Windows 8.1 and MySQL Server 5.7. All x64 bit.

  1. Download Development Kit
  2. Extracted Development Kit.
  3. Run CMD and go to dir where is Development Kit extracted
  4. Run ruby dk.rb init
  5. Run ruby dk.rb install
  6. Run gem install mysql2 -- '--with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.7\lib" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.7\include" --with-mysql-dir="c:\Program Files\MySQL\MySQL Server 5.7"'

This is what worked for me for the same error on Windows 8 64-bit and using ruby 64-bit

  1. Download and install MySQL Server 5.6 64-bit
  2. Run this command:

    gem install mysql2 -v '0.3.16' -- '--with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.6\lib" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.6\include"'
    

Hope this helps

I have tried all provided methods but the same error appears again and again :(

Fortunately, older version installed without any errors!

gem uninstall mysql2
gem install mysql2 -v 0.2.6

Try to follow those steps:

I assume:

  • a) you use windows7
  • b) ruby 2+ on C:\Ruby200-x64
  • c) rails 4+ installed on ruby
  • d) WAMP installed on C:\wamp and running

1) uninstall your myslq2 gem:

$ gem uninstall mysql2

2) reinstall the mysql2 gem with path option:

$ gem install mysql2 -- '--with-mysql-lib="C:\wamp\bin\mysql\mysql5.6.12\lib" --with-mysql-include="C:\wamp\bin\mysql\mysql5.6.12\include"'

3) copy the libmysql.dll you find in C:\wamp\bin\mysql\mysql5.6.12\lib into C:\Ruby200-x64\bin

4) open in your rails app the file "Gemfile" and edit it enabling the mysql2, by adding:

gem 'mysql2'

5) start your rails server:

$ rails server

6) open your browser and go to localhost:3000/

I am still having problem with mysql2 gem but below approach helped me install mysql with RoR on Windows Env.

Step 1 : Gem File changes : ({app}/Gemfile)

Update mysql2 --> mysql

Step 2 : Updates in database.yml file ({app}/config/database.yml)

Change adapter: mysql2 --> adapter: mysql

host: localhost --> host: 127.0.0.1

Step 3 : Downoad mysql-connector-c-noinstall-6.0.2-win32

Step 4 : Copy mysql-connector-c-noinstall-6.0.2-win32\lib\libmysql.dll file to C:\RailsInstaller\Ruby1.9.3\bin

Step 5 :bundle update && Start rails server

Here's what worked for me:

gem install mysql2 -- '--with-mysql-dir="C:\wamp\bin\mysql\mysql5.5.xx"'

Been searching for a few hours and not seen this variation suggested, so hopefully it will help someone!

Also try disabling your antivirus before installing the gem. Mine got installed after I ran following command :

gem install mysql2 --platform=ruby -- '--with-mysql-lib=
"C:\mysql-connector\lib" --with-mysql-include="C:\mysql-connector\include" --wit
h-mysql-dir="C:\mysql-connector"'

Same command was not working when my antivirus was enabled, as it was pushing some ruby file to chest. Hope this helps someone.

It worked for me after adding some tweaks

gem install mysql2 --platform=ruby -- '--with-mysql-lib="C:\Program Files\MySQL\MySQL Connector C 6.1.5\lib" --with-mysql-include="C:\Program Files\MySQL\MySQL Connector C 6.1.5\include" --with-mysql-dir="C:\Program Files\MySQL\MySQL Connector C 6.1.5"

For Cygwin, I could consolidate various steps.

  1. Use cygwin setup.exe to download gcc, g++, make, cmake and libmysqlclient-devel (from cygwin ports)
  2. Download c/connector src for windows from mysql site. I downloaded 6.x version. OR download libmysqlclient-devel's version (not sure from where)
  3. unzip
  4. cd dir_connector
  5. Not required and RISKY, but if cygwin libmysqlclient-devel version is 5.5.40 , then change dir_connector/VERSION file's versions to 5.5.40
  6. mkdir build
  7. cd build
  8. comment dtoa in stdlib.h, else compilation is failing
  9. cmake ..
  10. make
  11. make install
  12. gem install mysql2 -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

Ruby installer rubyinstaller-devkit-2.5.3-1-x64

Mysql connector: mysql-connector-c-6.1.11-win32 https://downloads.mysql.com/archives/c-c/?version=6.1.2&os=src

Gem install: gem install mysql2 --platform=ruby -- --with-mysql-lib="c:\mysql-connector-c-6.1.11-win32"

I downloaded the devkit 32-bit, with my 64-bit pc, however I realize that railsinstaller apparently installs a 32-bit version by default, so I have directed all facilities 32bit.

Initialize the ruby devkit dk.rb init

Download the mysql connector 32-bit, and place it in the mysql-connector C: \ mysql-connector

You should be aware, that when you unzip the file, it creates a subfolder with the same name and can give errors.

Then use the second command listed above:

gem install mysql2 - platform = ruby - '- with-mysql-dir = "C: \ mysql-connector"' 

This was the solution I found, as I had the same problem.

On Windows 7 I found that the existing answers provided a partial solution, but I still couldn't get it to install.

The problem I had was that the MySQL Community Edition Windows installer I used insisted on putting the connectors in folders called things like C:\MySQL\MySQL Connector C 6.1.3. The build process in the gem was reporting that it could not find "C:\MySQL\MySQL" ( and before that when I had it in Program Files, it complained it couldn't find "C:\Program\includes" ) so clearly the spaces were a problem and I couldn't find a way to escape them that it could understand- possibly someone will be able to suggest one of those.

Because I wasn't sure whether changing these would break the installation, once I realised why the problem was arising I just created a symlink from the command line ( something that is easier from cmd than powershell ) like this:

C:\MySQL> mklink /D ConnectorC ".\MySQL Connector C 6.1.3"

Then I could use the following install line:

 gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:/MySQL/ConnectorC/"'

This worked correctly.

Also, when I tried to use it, I ran into this problem so it may be useful to be aware that moving the MySQL lib file from the C Connector lib folder could be helpful.

I managed to succeed at this out of pure luck, but after an hour of hell I feel like I should share my solution, obviously very shortened but a lot of the steps I think you can figure out how to do on your own.

My machine is Win 7 64bit. I was able to install version 0.3.16

  1. Install ruby version 2.0.0p481 (?)
  2. Devkit blah blah
  3. Install rails version 4.0.0
  4. Download Mysql Connector C 64 bit zip file, put in C drive as mysqlc
  5. Take DLL from that bin, put it in lib of ruby
  6. gem install mysql2 -- -- with-mysql-dir="C:\mysqlc"

None of that worked for me, until I realized that I have the 64-bit version of the DevKit installed. So I downloaded the 64-bit MySQL Connector/C from MySQL website

and installed the gem with the "with-mysql-dir" option

Here is what I did for windows server 2012 sp2. Nothing else worked...

After receiving error 'while installing mysql2 (0.3.17), and bundler cannot continue' Make sure that 'gem install mysql2 -v '0.3.17' succeeds...

1) Download the MySql C Connector from: http://dev.mysql.com/downloads/connector/c/

2) Extract the file to C:\mysql-connector

3) gem install mysql2 -v '0.3.17' --platform=ruby -- '--with-mysql-dir="C:\mysql-connector"'

Note: the -v '0.3.17' above, it would not work without this.

4) bundle install --> Finally, Success.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top