brianmario / mysql2

A modern, simple and very fast Mysql library for Ruby - binding to libmysql
http://github.com/brianmario/mysql2
MIT License
2.24k stars 550 forks source link

Cannot install mysql2 gem on Windows 10 #1210

Open SuperSajuuk opened 3 years ago

SuperSajuuk commented 3 years ago

Hi there. I'm trying to install this gem into my Ruby installation (Ruby 2.7.4, via msys2, x64-mingw32: Windows 10 v2004) but no matter what I attempt to do, I get this error consistently:

Using msys2 packages: mingw-w64-x86_64-libmariadbclient
Building native extensions with: '--with-mysql-dir D:\Programs\MariaDB 10.4'
This could take a while...
ERROR:  Error installing mysql2:
        ERROR: Failed to build gem native extension.

    current directory: D:/Programs/Ruby/27-x64/lib/ruby/gems/2.7.0/gems/mysql2-0.5.3/ext/mysql2
D:/Programs/Ruby/27-x64/bin/ruby.exe -I D:/Programs/Ruby/27-x64/lib/ruby/2.7.0 -r ./siteconf20210907-1876-1n6w2x.rb extconf.rb --with-mysql-dir D:\\Programs\\MariaDB\ 10.4
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... 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=D:/Programs/Ruby/27-x64/bin/$(RUBY_BASE_NAME)
        --with-mysql-dir
D:/Programs/Ruby/27-x64/lib/ruby/2.7.0/mkmf.rb:1771:in `dir_config': undefined method `split' for true:TrueClass (NoMethodError)
        from extconf.rb:53:in `<main>'

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  D:/Programs/Ruby/27-x64/lib/ruby/gems/2.7.0/extensions/x64-mingw32/2.7.0/mysql2-0.5.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in D:/Programs/Ruby/27-x64/lib/ruby/gems/2.7.0/gems/mysql2-0.5.3 for inspection.
Results logged to D:/Programs/Ruby/27-x64/lib/ruby/gems/2.7.0/extensions/x64-mingw32/2.7.0/mysql2-0.5.3/gem_make.out

Any ideas on how to fix, or is the latest gem version non functional at the moment? I've confirmed that I have the required system libraries present and I've even tried, as per the error message, to point the gem to use the locally installed MariaDB 10.4, but nothing works. Help would be appreciated.

SuperSajuuk commented 3 years ago

For anyone who is interested, i was able to get this to install from the rubygems.org directly after passing the path to the c bindings but this won’t install via bundler specifying a git url with this error. I can’t tell if the rubygems site matches the github repo but seems to work now,

idriscahyono commented 3 years ago

Hi, Try with this https://medium.com/ruby-on-rails-web-application-development/installing-the-mysql2-rubyonrails-gem-on-windows-7-8-a028f44d87f3

SuperSajuuk commented 3 years ago

Hi, Try with this https://medium.com/ruby-on-rails-web-application-development/installing-the-mysql2-rubyonrails-gem-on-windows-7-8-a028f44d87f3

Hi yeah I found that guide but it wasn't working in Bundler as it refused to take the arguments from the .bundle file. Worked fine installing directly outside of Bundler though, which I was able to do. 👍

pcopissa commented 2 years ago

I got this exact same error in the same line 1771. After an entire day of trial and error, I found that I could install a working mysql2 gem on Ruby 2.7.3 on Windows 10 (working: at least for basic queries).

  1. Start a cmd terminal and run ridk explicitely (c:\your\path\to\Ruby27-x64\ridk.cmd enable). This gives you the right environment to do the rest.
  2. From that cmd, run gem install mysql2 --platform=ruby -- --with-mysql-dir=c:/your/path/to/Ruby27-x64/msys64/mingw64 (no particular quote was neeeded and I used forward slashes. This was adapted from this answer )

FWIW, I could get a non functional gem to build with the C Connector 6.1.11 by doing so:

  1. Downloaded the zip version from https://downloads.mysql.com/archives/c-c/
  2. Unzipped in c:\your\path\to\mysql-connector-c-6.1.11-winx64 (no spaces. May or may not be important)
  3. Start a cmd terminal and run ridk explicitely as step 1 above.
  4. From that cmd, run gem install mysql2 --platform=ruby -- --with-mysql-dir=c:/your/path/to/mysql-connector-c-6.1.11-winx64 (no particular quote was needed and I used forward slashes)
  5. However, that gem is not usable: I get a runtime error when require 'mysql2' is executed: Incorrect MySQL client library version! This gem was compiled for 6.1.11 but the client library is 10.5.5. (RuntimeError). I believe this is because MariaDB stuff gets linked to the resulting mysql2.so no matter what (it gets forcibly installed too if missing). I have not found how to link the DLL shipped with the C connector. (I tried various --with-xxxx switches without success)

Also note that I tried to build with gem install mysql2 --platform=ruby -- --use-system-libraries as suggested in the article linked above. That ended up with a different build failure (in line 1050 ofmkmf.rb rather than 1771).

I have also noticed to that a consistent predictor of a failed build is the second line that starts with Building native extensions with:. When the subsequent text --with-xxxx is followed by an equal sign then the build succeed. When it is followed by a space, the build fails. I suspect there are some quoting and/or parsing issues somewhere (maybe paths containing spaces are part of the problem ?)

Hope this helps

MaffooClock commented 2 years ago

@pcopissa's solution put me on the right track, except I wanted to use the MariaDB library.

  1. Download MariaDB Connector/C 3.1.13 and unzip
  2. Inside the unzip'd folder, rename the libmariadb folder to lib
  3. Start a cmd terminal and run ridk
  4. Then run gem install mysql2 --platform=ruby -- --with-mysql-dir=C:/Users/clark/Downloads/mariadb-connector-c-3.1.13-src (or wherever your unzip'd it)

The gem compiled, although I haven't tested it yet, so I have yet to see if it works ¯\_(ツ)_/¯

briri commented 2 years ago

I was able to get this working on OSX by (note this assumes Homebrew is installed):

Note that I happened to do the uninstall/reinstall of mariadb but a brew upgrade might be enough.

I'm also not sure if there's a better way to specify the 'mysql_dir' when in installing the gem. Brew creates symlinks to the executables but I didn't find any links to the root 'Cellar/mariadb/[version]' dir. I suppose one could be added manually (and updated as needed).

Chrissiku commented 2 years ago

Only these Two commands works for me.

  1. Start cmd terminal and run ridk
  2. From that cmd, run gem install mysql2 --platform=ruby -- --with-mysql-dir=c:/your/path/to/Ruby27-x64/msys64/mingw64
    • Make sure that you specify the right path to Ruby installed in your computer

Thank you @pcopissa

DolezalDavid commented 1 year ago

Only these Two commands works for me.

1. Start  `cmd` terminal and run `ridk`

2. From that cmd, run `gem install mysql2 --platform=ruby -- --with-mysql-dir=c:/your/path/to/Ruby27-x64/msys64/mingw64`

   * Make sure that you specify the right path to Ruby installed in your computer

Thank you @pcopissa

Same for me on W2016 with Ruby v2.7.7

TerritoryTrader commented 1 year ago

Only these Two commands works for me.

  1. Start cmd terminal and run ridk
  2. From that cmd, run gem install mysql2 --platform=ruby -- --with-mysql-dir=c:/your/path/to/Ruby27-x64/msys64/mingw64

    • Make sure that you specify the right path to Ruby installed in your computer

Thank you @pcopissa

Thanks @pcopissa I tried many other methods over the last 2 days. This is what worked for me

Open cmd

C:\Windows\System32>ridk

large printout of Ruby Installer 2 for Windows

C:\Windows\System32>gem install mysql2 --platform=ruby -- --with-mysql-dir=c:/Ruby32/msys32/mingw32

Temporarily enhancing PATH for MSYS/MINGW... Using msys2 packages: mingw-w64-i686-libmariadbclient Building native extensions with: '--with-mysql-dir=c:/Ruby32/msys32/mingw32' This could take a while... Successfully installed mysql2-0.5.5 Parsing documentation for mysql2-0.5.5 Installing ri documentation for mysql2-0.5.5 Done installing documentation for mysql2 after 2 seconds 1 gem installed

Nearly fell off my chair when it worked. Please note that DevKit needs to be down loaded and need to ensure that all 3 options are installed

gersonmax commented 1 year ago

C:\Windows\System32>gem install mysql2 --platform=ruby -- --with-mysql-dir=c:/Ruby32/msys32/mingw32

This works but does anybody know why? Pointing the mysql dir to Ruby dir is the lasts thing I would have thought would work. Am I misinterpreting what that flag is for? I thought it was the pat to MySQL

HoaiNam-2023 commented 1 year ago

Start cmd terminal and run ridk

From that cmd, run gem install mysql2 --platform=ruby -- --with-mysql-dir=c:/Ruby32-x64/msys64/mingw64 Thanks a lot! These cmd lines did help me solve the problem, I'm using Windows 11

Paiman-Rasoli commented 1 year ago

@pcopissa Thanks a lot. it works for me having windows-10 and ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x64-mingw-ucrt]

rioda78 commented 1 year ago

Start cmd terminal and run ridk

From that cmd, run gem install mysql2 --platform=ruby -- --with-mysql-dir=c:/Ruby32-x64/msys64/mingw64 Thanks a lot! These cmd lines did help me solve the problem, I'm using Windows 11

work using this command on windows 11 and ruby 3.1.4. thanks

pcopissa commented 11 months ago

@gersonmax:

This works but does anybody know why? Pointing the mysql dir to Ruby dir is the lasts thing I would have thought would work. Am I misinterpreting what that flag is for? I thought it was the pat to MySQL

The answer is twofold, I think:

First, MSYS2 does not have a MySQL package, but a MariaDB one and they are sufficiently similar header-wise and symbol-wise (I assume that's deliberate ?) that you can use use MariaDB's stuff instead of MySQL's. Hence I just installed the MariaDB package for MSYS2 (that ends up in the standard folders under Unix /). Second, I believe --with-mysql-dir points where the installer will pick source files from (i.e. headers and shared libraries/DLL), expecting the classic Unix hierarchy starting at / (ie: /lib and /include mostly). Since we are executing the build with a Unix-ish toolchain but in a cmd prompt, we have to specify a place that looks like Unix /. Indeed, if you look at bullet 4 of my post, you will see that it did manage to build a gem, although a non functional one, using the path to the source for the MySQL connector. And that connector's folder is indeed structured as Unix /. Putting those two considerations together, you end up passing in --with-mysql-dir the place which MSYS2 considers as / and that is c:/your/path/to/Ruby27-x64/msys64/mingw64 when using Ruby 2.7.x (or c:/your/path/to/Ruby32-x64/msys64/ucrt64 for Ruby 3.2.x since you asked...). Other posts above used other paths possibly because they had a 32-bit Ruby which relies on MSYS2 having its / in yet some other place.

taozuhong commented 7 months ago

I tried my best to contact MySQL devs to support MINGW package, yet done it: https://github.com/msys2/MINGW-packages/pull/16085

klues commented 5 months ago

I've just managed to install mysql2-0.5.6 on Windows 10 with Ruby 3.2.3 this way:

jnavarrete96 commented 5 months ago

Thanks, it works for me