19

I'm unable to install the mysql2 gem in my Windows7 x64 system. I tried using both the 32-bit and 64-bit versions of MySQL server but none got me any further.

I installed Ruby 1.8, the development kit and and the Rails platform. I even installed minGW to allow C++/C compilation of things. MySQL Server was installed in the default location.

When running the command:

C:\Users\Arne>gem install mysql2 -- --with-mysql-include="C:\Program Files (x86)\MySQL\MySQL Server 5.1\include" --with-mysql-lib="C:\Program Files (x86)\MySQL\MySQL Server 5.1\lib\"

I receive the following output from the console:

Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
*** 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:/Ruby/bin/ruby
        --with-mysql-dir
        --without-mysql-dir
        --with-mysql-include=${mysql-dir}/include
        --with-mysql-lib=${mysql-dir}/lib
        --with-libmysqllib
        --without-libmysqllib
ERROR:  Error installing mysql2:
        ERROR: Failed to build gem native extension.

        C:/Ruby/bin/ruby.exe extconf.rb --with-mysql-include=C:\Program Files (x
86)\MySQL\MySQL Server 5.1\include --with-mysql-lib=C:\Program Files (x86)\MySQL
\MySQL Server 5.1\lib"
checking for rb_thread_blocking_region()... no
checking for main() in -llibmysql... no


Gem files will remain installed in C:/Ruby/lib/ruby/gems/1.8/gems/mysql2-0.3.2 f
or inspection.
Results logged to C:/Ruby/lib/ruby/gems/1.8/gems/mysql2-0.3.2/ext/mysql2/gem_mak
e.out

The output of my environment according to Rubygems is:

C:\Users\Arne>gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 1.7.2
  - RUBY VERSION: 1.8.7 (2011-02-18 patchlevel 334) [i386-mingw32]
  - INSTALLATION DIRECTORY: C:/Ruby/lib/ruby/gems/1.8
  - RUBY EXECUTABLE: C:/Ruby/bin/ruby.exe
  - EXECUTABLE DIRECTORY: C:/Ruby/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-mingw32
  - GEM PATHS:
     - C:/Ruby/lib/ruby/gems/1.8
     - C:/Users/Arne/.gem/ruby/1.8
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/

The following gems have been installed on the system as well:

C:\Users\Arne>gem list --local

*** LOCAL GEMS ***

abstract (1.0.0)
actionmailer (3.0.7)
actionpack (3.0.7)
activemodel (3.0.7)
activerecord (3.0.7)
activeresource (3.0.7)
activesupport (3.0.7)
arel (2.0.9)
builder (3.0.0, 2.1.2)
bundler (1.0.12)
erubis (2.7.0, 2.6.6)
fastthread (1.0.7)
i18n (0.5.0)
mail (2.3.0, 2.2.18)
mime-types (1.16)
mysql (2.8.1 x86-mingw32)
polyglot (0.3.1)
rack (1.2.2)
rack-mount (0.7.2, 0.6.14)
rack-test (0.5.7)
rails (3.0.7)
railties (3.0.7)
rake (0.8.7)
rdoc (3.5.3, 2.5.11)
rdoc-data (2.5.3)
rubygems-update (1.7.2)
thor (0.14.6)
treetop (1.4.9)
tzinfo (0.3.27, 0.3.26)

I don't know what's missing or wrong, but the output files pointed to by the script contain the following data:

mkmf.log:

have_func: checking for rb_thread_blocking_region()... -------------------- no

"gcc -o conftest -I. -IC:/Ruby/lib/ruby/1.8/i386-mingw32 -I.    -g -O2 -DFD_SETSIZE=256    conftest.c  -L. -LC:/Ruby/lib -L.      -lmsvcrt-ruby18-static  -lshell32 -lws2_32  "
conftest.c: In function 't':
conftest.c:7:53: error: 'rb_thread_blocking_region' undeclared (first use in this function)
conftest.c:7:53: note: each undeclared identifier is reported only once for each function it appears in
checked program was:
/* begin */
1: #include <ws2tcpip.h>
2: #include <winsock2.h>
3: #include <windows.h>
4: 
5: /*top*/
6: int main() { return 0; }
7: int t() { void ((*volatile p)()); p = (void ((*)()))rb_thread_blocking_region; return 0; }
/* end */

"gcc -o conftest -I. -IC:/Ruby/lib/ruby/1.8/i386-mingw32 -I.    -g -O2 -DFD_SETSIZE=256    conftest.c  -L. -LC:/Ruby/lib -L.      -lmsvcrt-ruby18-static  -lshell32 -lws2_32  "
C:\Users\Arne\AppData\Local\Temp\ccCicaNu.o: In function `t':
C:\Ruby\lib\ruby\gems\1.8\gems\mysql2-0.3.2\ext\mysql2/conftest.c:3: undefined reference to `rb_thread_blocking_region'
collect2: ld returned 1 exit status
checked program was:
/* begin */
1: /*top*/
2: int main() { return 0; }
3: int t() { rb_thread_blocking_region(); return 0; }
/* end */

--------------------

have_library: checking for main() in -llibmysql... -------------------- no

"gcc -o conftest -I. -IC:/Ruby/lib/ruby/1.8/i386-mingw32 -I. -IC:\Program    -g -O2 -DFD_SETSIZE=256    conftest.c  -L. -LC:/Ruby/lib -LC:\Program -L.      -lmsvcrt-ruby18-static -llibmysql  -lshell32 -lws2_32  "
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe: cannot find -llibmysql
collect2: ld returned 1 exit status
checked program was:
/* begin */
1: #include <ws2tcpip.h>
2: #include <winsock2.h>
3: #include <windows.h>
4: 
5: /*top*/
6: int main() { return 0; }
7: int t() { void ((*volatile p)()); p = (void ((*)()))main; return 0; }
/* end */

"gcc -o conftest -I. -IC:/Ruby/lib/ruby/1.8/i386-mingw32 -I. -IC:\Program    -g -O2 -DFD_SETSIZE=256    conftest.c  -L. -LC:/Ruby/lib -LC:\Program -L.      -lmsvcrt-ruby18-static -llibmysql  -lshell32 -lws2_32  "
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe: cannot find -llibmysql
collect2: ld returned 1 exit status
checked program was:
/* begin */
1: /*top*/
2: int main() { return 0; }
3: int t() { main(); return 0; }
/* end */

--------------------

gem_make.out:

C:/Ruby/bin/ruby.exe extconf.rb --with-mysql-include=C:\Program Files (x86)\MySQL\MySQL Server 5.1\include --with-mysql-lib=C:\Program Files (x86)\MySQL\MySQL Server 5.1\lib"
checking for rb_thread_blocking_region()... no
checking for main() in -llibmysql... no

I hope someone can point what I'm doing wrong, or what is actually missing on my development system to get this gem properly installed and working.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
codingbunny
  • 3,989
  • 6
  • 29
  • 54

9 Answers9

41

I finally solved it:

  1. Downloaded and installed MySQL 64bit version 5.5
  2. Downloaded the zip archive for the 32bit version 5.5.
  3. Extracted the 32-bit include and lib folders to C:\MySQL.
  4. Installed Ruby 1.9.2.
  5. Installed the Ruby Devkit.
  6. Installed the gems:
    1. gem install mysql
    2. gem install mysql2 -- --with-mysql-lib=C:\MySQL\lib --with-mysql-include=C:\MySQL\include
    3. gem install rails
    4. gem install fastthread
    5. gem install haml

It's working as a charm now. It seems the MySQL gem has issues finding the data when dealing with 64-bit.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
codingbunny
  • 3,989
  • 6
  • 29
  • 54
  • 6
    Step 2 and 3 was the key for me, too. I was running mingw32 window and I had to run `gem install mysql2 -- --with_mysql_lib=/c/MySQL/lib --with-mysql-include=/c/MySQL/include` – seehad Jul 07 '11 at 21:55
  • can NOT thank you enough. life saving post right here... if i could upvote more than once i honestly would. – Damien-Wright Oct 13 '11 at 00:41
  • Thank you, It worked for me too. All I need was step #7 I'm using [RailsInstaller](http://railsinstaller.org/) (devkit included) and standard xampp. – Elvan Oct 22 '11 at 16:40
  • I'll add that it may be better to use cmd.exe instead of powershell for these commands. For some reason under powershell gem kept throwing an error that said "invalid option". – Segfault Oct 27 '11 at 15:31
  • It's actually better to use the git command line or the msys one, because they understand some of the commands such as ls, which is often used by the configure script. – codingbunny Oct 28 '11 at 14:02
  • Instead of 32 bit mysql in step 2, I used http://dev.mysql.com/get/Downloads/Connector-C/mysql-connector-c-noinstall-6.0.2-win32.zip/from/pick – Douglas Jan 03 '12 at 12:13
  • With Ruby 64 bit, I had to get the dev kit, get the SQL connector (64 bit) and run gem install mysql2 -- '--with-mysql-dir="x:"' where x: is the location of the downloaded connector using the --with-mysql-lib and --with-mysql-include options did not work for me – slugslog Feb 27 '14 at 12:05
19

Here's what worked for me using: Win7 64/MySQL 5.5 64-bit/Ruby 1.9.2:

  1. Download libmysql.dll.
  2. Copy the above libmysql.dll to C:\Ruby192\bin AND C:\Ruby192\lib, or wherever your lib and bin are.
  3. Run:
gem install mysql2 -- '--with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.5\lib" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.5\include" --with-mysql-dir="c:\Program Files\MySQL\MySQL Server 5.5"'

Good luck.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
JohnF
  • 341
  • 3
  • 4
  • 2
    This deserves to be upvoted a million times reddi... I mean slashdotters. – Dirk Oct 10 '11 at 19:52
  • Thanks, had to do this instead: gem install mysql2 -v 0.2.7 -- '--with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.5\lib" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.5\include" --with-mysql-dir="c:\Program Files\MySQL\MySQL Server 5.5"' Had to use a specfic version. – LearningRoR Nov 06 '11 at 18:43
  • 1
    @Railslearner I did this but when i do the `bundle update` its reverting back to the version `0.3.11`. How do deal with this situation? – uday Feb 22 '12 at 03:17
  • Excellent. Works with Win8/MySQL 5.6/Ruby 2.0 as well. – Ben Fulton Sep 17 '14 at 20:12
6

If still getting error then cross check the steps of installation on -

http://rorguide.blogspot.com/2011/03/installing-mysql2-gem-on-ruby-192-and.html

where most of the user were able to install mysql2 gem after following the described steps.

Ritesh Kumar
  • 2,183
  • 2
  • 20
  • 32
3

I know the thread is related to Windows but I reached it searching for the same issue on Linux and I fixed it by installing the lib "libmysqlclient-dev"

PedroSena
  • 645
  • 6
  • 14
2

The accepted answer did not work for me, and neither did the the other 2. However, a comment by MG on the link from Ritesh did the trick for me. I had to specify version 0.2.6, and include the parameter --platform=ruby. (Note: I also had to download the "noinstall" version of MySQL to get the /lib/opt)

Here is how I got the gem installed on Windows

gem install mysql2 -v 0.2.6 -- --platform=ruby --with-mysql-lib=C:\Ruby192\MySQL\lib\opt --with-mysql-dir=C:\Ruby192\MySQL
MM.
  • 1,966
  • 4
  • 20
  • 24
  • I did not require the lib/opt to properly install mysql2 gem, nor did I had to specify the platform. It also helps if you run these commands from the minGW or git console and not the default windows console. – codingbunny Jun 15 '11 at 05:38
  • This is the only thing that worked for me, platfor=ruby wasnt needed. Im using windows 7 – Zasz Sep 03 '11 at 14:52
1

I had similar problems with MySQL 5.6 under 64-bit Windows 7. The main solution given here worked. (the solution suggested by kobalz of just copying the dll didn't work. No idea why it works for kobalz but not for me!)

I then ran into another problem which is described in this post:

mysql2 gem compiled for wrong mysql client library

The solution described there avoids downloading a complete 32-bit MySQL and fixed both problems. Essentially, download the mysql connector (a much smaller download) and use that to compile the mysql2 gem. You have to download exactly the right version, as described in the post. In my case:

http://dev.mysql.com/get/Downloads/Connector-C/mysql-connector-c-noinstall-6.0.2-win32.zip/from/pick

then copy the lib\libmysql.dll file from there into your Ruby bin directory as described above.

I put my connector into c:\mysql-connector-c-noinstall-6.0.2-win32

so the command to build and install the gem was:

gem install mysql2 -- --with-mysql-lib="c:\mysql-connector-c-noinstall-6.0.2-win32\lib" --with-mysql-include="c:\mysql-connector-c-noinstall-6.0.2-win32\include" --with-mysql-dir="c:\mysql-connector-c-noinstall-6.0.2-win32"

I then ran into further problems when I ran rake to do a migration, but that was because I had been using the mysql gem (not mysql2) and I had forgotten to update my database.yml. With the mysql2 gem, it needs to say:

development: adapter: mysql2

rather than

development: adapter: mysql

which is obvious once you've figured it out, but the error messages I got were similar to the ones I got before, so it looked as if there was still something wrong with my gem.

Community
  • 1
  • 1
user1208639
  • 303
  • 3
  • 4
1

This one worked for me for a MariaDB 5.5 installation:

gem install mysql2 -- '--with-mysql-lib="c:\Program Files (x86)\MariaDB 5.5\lib" --with-mysql-include="c:\Program Files (x86)\MariaDB 5.5\include\mysql"'

Building native extensions with: '--with-mysql-lib="c:\Program Files (x86)\Maria DB 5.5\lib" --with-mysql-include="c:\Program Files (x86)\MariaDB 5.5\include\mys ql"'

Be sure to use x64 MariaDB packages if you plan to use Ruby x64. And of course, don't forget to copy C:\Program Files (x86)\MariaDB 5.5\lib\libmysql.dll to c:\Ruby200\bin in my case.

Orsiris de Jong
  • 2,819
  • 1
  • 26
  • 48
0

this is what worked for me on windows 8 64bit and using ruby 64bit

download and install MYSQL Server 5.6 64bit

then run the 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

Omar M.
  • 331
  • 2
  • 6
0

Copy the libmysql.dll located in the bin directory of your MySQL install to the bin directory of your Ruby install. Mine were located in C:\Program Files\MySQL and C:\Ruby.

kobaltz
  • 6,980
  • 1
  • 35
  • 52
  • 1
    Copied over the DLL as you suggested, made no difference however. Still receiving the same errors. – codingbunny Apr 30 '11 at 11:46
  • I've installed the win32-thread gem for ruby, and this seems to have some effect on the entire build progress. Still receiving the same errors now, but some of the "no" responses have now turned into "yes" – codingbunny Apr 30 '11 at 15:03