Unable to install MySQL2 gem on Windows 7

29,034

Solution 1

The specific version of mysql2 gem you're trying to install (0.2.4) not only lacks binaries for Windows, but have issues on Windows.

Please install mysql2 gem without indicating the version:

gem install mysql2

Which will install latest version (0.2.6 at the time of my posting this) and also provides binaries for Windows which skip the compilation step.

If you still want to force the compilation (because your version of MySQL differs from the one used to generate the binary gem, you will need to install RubyInstaller's DevKit from RubyInstaller website:

http://rubyinstaller.org/downloads

And follow the DevKit installation instructions from our wiki (that is linked from the download page)

You will need to provide the path to both headers and libraries during the gem installation process, and adjust the MySQL installation location from the following instructions:

subst X: "C:\Program Files (x86)\MySQL\MySQL Server 5.1" 
gem install mysql2 --platform=ruby -- --with-mysql-dir=X: --with-mysql-lib=X:\lib\opt 
subst X: /D

The above command uses subst to avoid issues with path with spaces, which you should avoid always.

Hope this helps.

Solution 2

After searching around for a way to make it work, I finally got it installed with the following on the command prompt:

gem install mysql2 -v 0.2.6

With the following results:

Fetching: mysql2-0.2.6-x86-mingw32.gem (100%)
Successfully installed mysql2-0.2.6-x86-minw32
1 gem installed
Installing ri documentation for mysql2-0.2.6-x86-mingw32...
Enclosing class/module 'mMysql2' for class Client not known
Installing RDoc documentation for mysql2-0.2.6-x86-mingw32...
Enclosing class/module 'mMysql2' for class Client not known

Try it. It should work.

Solution 3

If still not able to install mysql2 gem on windows7 or not very clear on the steps to follow, then please go through this post which will help you in step-by-step installation - http://rorguide.blogspot.com/2011/03/installing-mysql2-gem-on-ruby-192-and.html

And for mysql_api.so error (C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql-2.8.1-x86-mingw32/lib/1.9/mysql_api.so), copy "libmySQL.dll" file from C:\Program Files\MySQL\MySQL Server 5.1\bin to C:\Ruby192\bin

Path of installation may vary, for details refer to this post - http://rorguide.blogspot.com/2011/03/getting-error-specified-module-could.html.

Hope, this will help you in sorting out your issues.

Solution 4

Start with RailsInstaller.

This is assuming you are running the mysql server locally (local development), and your Ruby is compiled 32-bit. (Check ruby -v. x64 || i386.)

Download the mysql-community-web-installer. In the top right select 32-bit. You pretty much just need the Server. The Workbench is handy also.

The gem command needs a path without spaces, so use subst:

subst X: "C:\Program Files (x86)\MySQL\MySQL Server 5.6"
gem install mysql2 -v=0.3.13 --platform=ruby -- --with-mysql-dir=X:

You will need libmysql.dll in the PATH -- the easiest thing is just to copy it to Ruby's bin directory:

copy "C:\Program Files (x86)\MySQL\MySQL Server 5.6\lib\libmysql.dll" "C:\RailsInstaller\Ruby2.0.0\bin"

NOTES

Don't do this from PowerShell! (DevKit has trouble changing path info in powershell.)

Make sure all libraries are 32-bit, since RailsInstaller's ruby etc. are all compiled 32-bit and need to link to 32-bit libraries.

If you don't want a server locally, just install the C Connector, and do these instructions with that directory and libmysql.dll. Don't mix and match -- make sure the .dll matches the version you compiled the gem against.

Share:
29,034
Lohith MV
Author by

Lohith MV

Updated on January 25, 2020

Comments

  • Lohith MV
    Lohith MV over 4 years

    I am getting the following error message while installing, let me know if I need to post more details.

    I followed instructions from the following location: https://github.com/oneclick/rubyinstaller/wiki/Development-Kit

    I am using ruby 1.9.2p136 (2010-12-25) [i386-mingw32].

    Here is what I get:

    E:\work_desk\trunk>gem install mysql2 -v 0.2.4
    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:/Ruby192/bin/ruby.exe extconf.rb
    checking for rb_thread_blocking_region()... yes
    checking for main() in -llibmysql... no
    *** 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:/Ruby192/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}/lib
            --with-libmysqllib
            --without-libmysqllib
    
    
    Gem files will remain installed in C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.
    2.4 for inspection.
    Results logged to C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.2.4/ext/mysql2/ge
    m_make.out
    
  • Lohith MV
    Lohith MV about 13 years
    hi, i have mysql app and i installed them in there default location only,but my OS is 64 bit edition (windows 7) so all my installation goes to program files(x86) folder will that be a problem, i tried with following command line option E:\work_desk\trunk>gem install mysql2 -- '--with-mysql-lib="C:\Program Files (x8 6)\MySQL\MySQL Server 5.5\lib" --with-mysql-include="C:\Program Files (x86)\MySQ L\MySQL Server 5.5\include"' ERROR: While executing gem ... (Errno::EACCES) Permission denied - C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6-x86-min gw32/lib/mysql2/1.9/mysql2.so
  • Lohith MV
    Lohith MV about 13 years
    but still I am getting error but this time it says permission denied ?
  • PJP
    PJP about 13 years
    If your machine has an administration account you probably need to use it to run the gem installer. Also, I usually don't try to force a gem version, instead I let gem decide what to load, which should be the default/current version. And, are you sure you want mysql2 instead of the current mysql?
  • Lohith MV
    Lohith MV about 13 years
    I have administrative rights to my account and i tried using administrator also,result are same,I am using rails 3.0.5 so i need mysql2.
  • femi
    femi about 13 years
    why is ruby on rails so difficult? i have the same issue and i'm almost giving up...cant get rails to speak to mysql..this is frustrating..
  • PJP
    PJP about 13 years
    Rails isn't the difficult part. It's getting the database driver that is picky.
  • Rekin
    Rekin about 13 years
    Impressing! This worked for me as a charm. It should definitely be considered as an answer for this question.
  • Pablo Marambio
    Pablo Marambio almost 13 years
    I tried the same but failed: I just typed 'gem install mysql2' without any version whatsoever, but the same error was thrown.
  • Pablo Marambio
    Pablo Marambio almost 13 years
    Luis, I did. I installed Mysql Server, but the latest version: 5.5. However, even though I installed it with all features and libraries, there is no lib/opt folder in it.
  • Pablo Marambio
    Pablo Marambio almost 13 years
    Nope, nothing. I installed the MySQL Connector C 6.0.2, which does include the headers and the lib/opt folder, but nothing. It still crashes testing for libmysql. It seems it is ignoring the --with-mysql-dir and --with-mysql-lib parameters.
  • Pablo Marambio
    Pablo Marambio almost 13 years
    The guide mentions mySql 5.1, not 5.5. The 5.5 does not include the lib/opt folder, even if you chose to install all features included in the msi.
  • Luis Lavena
    Luis Lavena almost 13 years
    Perhaps you forgot to include the double dash to separate the options? Good luck on Ubuntu, is not as dreamland as you might think...
  • Ritesh Kumar
    Ritesh Kumar almost 13 years
    This Guide now contains mysql5.5 too :) You can install with command : 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"'
  • Stewart Johnson
    Stewart Johnson over 12 years
    This didn't work for me -- I did a plain gem install mysql2 which worked, but then a bundle install from within JetBrains RubyMine still tries to install the gem and failed. I tried to build the native extensions using the commands above, but that failed too: "ISO C90 forbids mixed declarations and code". :-/
  • Luis Lavena
    Luis Lavena over 12 years
    @StewartJohnson: when you do gem install mysql2 it will install the pre-compiled binaries, you need to specify --platform=ruby so it can compile with the provided options.
  • Luis Lavena
    Luis Lavena over 12 years
    @StewartJohnson: You can't also install from inside and IDE, you need to use the command line. The issue with C90 needs to be reported to mysql2 gem author, is not a problem with the instructions as it works perfectly with 1.8.7 and 1.9.2. 1.9.3 enforced the type of declarations.
  • Robin Kanters
    Robin Kanters about 11 years
    I know this is an old question, but please excuse me for saying you just solved my problem I've been struggling with for 2 days. Thank you so much!
  • Quv
    Quv almost 11 years
    this needs our gemfile to have gem 'mysql2', '0.2.6' though.
  • BBQ Chef
    BBQ Chef almost 11 years
    I picked a later version including win binaries from the version list. rubygems.org/gems/mysql2/versions
  • rogerdpack
    rogerdpack over 10 years
    I believe you will need/want the libmysql.dll file in your PATH see stackoverflow.com/a/5517200/32453
  • beckah
    beckah over 9 years
    @LuisLavena I apologize because I know this thread is extremely old,but I am currently trying to install the mysql2 gem manually because my connection at work does not allow me to install packages from an extl host through the cmd prompt, which has worked great for all of my other gem files. However, when I try to do this for mysql2 it does not work. I tried manually install the current version (0.3.17)and also with the version suggested here (0.2.6), but keep getting the same error as stated in the OP. I also tried seeing if it helped that I specify the ver in my gemfile with no luck :(