6

I try to install the mysql2 gem on Windows 7 32-bit.

I run:

gem install mysql2

and I get the following error:

ERROR:  Error installing mysql2:
        The 'mysql2' native gem requires installed build tools.

Please update your PATH to include build tools or download the DevKit
from 'http://rubyinstaller.org/downloads' and follow the instructions
at 'http://github.com/oneclick/rubyinstaller/wiki/Development-Kit'

Any ideas what could cause this error, and how to solve this ?


UPDATE

I did everything as described here, but I can't figure out the last step:

gem install mysql2 -- 
     '--with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.1\lib\opt" 
      --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.1\include"'

I understand that I should change the paths to the correct paths in my system, but I can't find the lib\opt and the include directories in my WAMP MySQL installation. I use WAMP 2.1.

Please advise.

Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746

6 Answers6

25

Here is a proper solution for anyone interested, that doesn't mess up your current installation of mysql server -

EDIT : Make sure you download the right version on MySQL server. :

if you intend to connect to a 6.0 or 5.5 server - download those binaries. I'm connecting to mysql 5.1 - hence my example. Also keep in mind latest mysql2 gem ( v 0.3.7) was tested against mysql server 6 - hence it keeps failing to build on windows for earlier versions. also make sure you use the right libmysql.dll in your ruby folder.

  1. Download a zip file with mysql server 5.1 NOT the msi one. Make sure it's 32-bit NOT 64-bit. (From here)

  2. Since there is no installer file with this, create a folder c:\mysql-gem-install - you can remove it once you finish.

  3. Extract all the files from the zip file into the folder you just created.

  4. now run this command

     gem install mysql2 --  '--with-mysql-lib="c:\mysql-gem-install\lib\opt"  --with-mysql-include="c:\mysql-gem-install\include"'
    

I just installed mysql2 gem v. 0.3.7

EDIT 1 One more thing: make sure you run the command in Command Prompt directly. As in not PowerShell or Consol2 - for some reason if you try that it will give you and error " invalid option" - has to do with the way -- is parsed.

EDIT 2 I just was doing a blank windows 7 install and one thing I ran into is that you need to add one more step to this: check to see if there is libmysql.dll in your ruby bin directory, if not copy one over from your mysql install. It should be of the same version as your mysql2 build.

Community
  • 1
  • 1
konung
  • 6,908
  • 6
  • 54
  • 79
  • Be careful when posting copy and paste boilerplate/verbatim answers to multiple questions, these tend to be flagged as "spammy" by the community. – Kev Sep 06 '11 at 00:00
  • Compiled but don't know how to include in GemFile – Travis Pessetto Sep 07 '11 at 05:54
  • NM was using `bundle update` from the wrong file. I know just to include it now. – Travis Pessetto Sep 07 '11 at 05:59
  • @Kev - It's my answer - I just typed it up for one answer - and the pasted it for another one - that was pretty much the same question - but was coming up higher on google search result - this way more people will see it and avoid frustration - this mysql2 gem question is one of the most common ones for ruby on windows right now. My intention wasn't to spam :-) – konung Sep 07 '11 at 16:31
  • 1
    Thanks for pointing out not to use PowerShell. I was getting invalid option all the time. Worked on standard cmd. – MorioBoncz Aug 11 '13 at 21:22
  • 1
    This is by far the best answer to this question and saved me a lot of time. If you are using MySQL 5.5, use --with-mysql-lib="c:\mysql-gem-install\lib" since there is no opt directory in that install. I used that to install version 0.3.14 of the mysql2 gem with no problems. – furman87 Mar 07 '14 at 12:51
4

THANK YOU! One note: I had to include the --platform=ruby option as well. Thus, the final entire command line was:

gem install mysql2 --platform=ruby -- '--with-mysql-lib="c:\mysql-gem-install\lib\opt" --with-mysql-include="c:\mysql-gem-install\include"'

When I tried it without the platform=ruby option, DevKit didn't run and it just installed the normal mysql2 gem - it even gave a warning message about mysql2 being built for 6.0.2. I uninstalled it (gem uninstall mysql2), then ran it again with --platform=ruby and it worked awesome!

Me: Windows 7 64bit, Ruby 1.9.3p125, Rails 3.2.1, MySQL 5.1.56

Ed Birm
  • 657
  • 6
  • 6
  • FYI: The minute I did bundle update, bundler installs the existing mysql2 gem and starts using it in my app. Here is the process for getting things to work again: 1) gem uninstall mysql2, select ALL. 2) run the gem install code again (my prev post, above). Done. – Ed Birm Mar 07 '12 at 17:44
  • This did the trick for me. Another thing to note is to download the "no-install" zip file from the above link. For me it was "mysql-no-install-5.1.62-win32.zip". – mhoofman Apr 30 '12 at 20:12
2

Well, i don't really use Windows, but what about downloading the DevKit from 'http://rubyinstaller.org/downloads' and following the instructions at 'http://github.com/oneclick/rubyinstaller/wiki/Development-Kit' ?

Spyros
  • 46,820
  • 25
  • 86
  • 129
  • Previously, when I installed `mysql2` on Windows XP, I didn't have this problem. So I wonder whether the installation on Windows 7 should be so compicated. Is this a temporary woraround ? – Misha Moroshko Apr 25 '11 at 05:11
  • 1
    That probably happened because you had the devkit installed on winxp beforehand. This one is essential for it to work. It's not a temporary solution, but something that you must do to have it work. – Spyros Apr 25 '11 at 05:12
  • SpyrosP's answer will get you going. I recently used rubyinstaller & DevKit to get RoR 3 with Ruby 1.9.2 & MySql running without issue. Be sure to follow the devkit instructions religously. – nathan Apr 25 '11 at 13:58
  • @nathan: Have you used `mysql` or `mysql2` ? – Misha Moroshko Apr 25 '11 at 13:59
  • @nathan: Moreover, I used the instructions and the installation test passed successfully! – Misha Moroshko Apr 25 '11 at 14:21
  • @Misha mysql2. With DevKit environment variables are very important. Also, if you have other msys based programs like git, mingw, etc. they can interfere with DevKit's environment variables. Be sure when you drop to your command line you're using Devkit's binaries. – nathan Apr 25 '11 at 14:32
  • @nathan: I don't have things like git and mingw installed. What command did you use to install `mysql2` ? `gem install mysql2 ` What environment variables should I check ? – Misha Moroshko Apr 25 '11 at 14:38
  • @nathan: Have you used WAMP's MySQL ? – Misha Moroshko Apr 25 '11 at 14:42
0

Not all mysql2 versions come with windows binaries.

Easiest way to install gem mysql2 on windows is to pick a version that includes win binaries.

Link to list of versions

Choose the latest version number that includes “x86-mingw32”

For example 0.3.11

To install it:

gem install mysql2 -v 0.3.11

or in the gemfile:

gem „mysql2“, „0.3.11“
BBQ Chef
  • 696
  • 4
  • 11
  • 21
0

You should execute the setup file again,and choose the modify option,then check the Developer Components.When it finish,you will found the include folder and the opt folder.

Kitt
  • 1
  • 1
0

Try XAMPP from apachefriends.org, be sure to download the full version and you will find "lib", "bin", "include" and other MySQL directories. For further details, refer to -

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

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