Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot install mysql2 gem on Windows 10 #1210

Open
SuperSajuuk opened this issue Sep 7, 2021 · 17 comments
Open

Cannot install mysql2 gem on Windows 10 #1210

SuperSajuuk opened this issue Sep 7, 2021 · 17 comments

Comments

@SuperSajuuk
Copy link

SuperSajuuk commented Sep 7, 2021

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
Copy link
Author

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
Copy link

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

@SuperSajuuk
Copy link
Author

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
Copy link

pcopissa commented Nov 11, 2021

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 of mkmf.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
Copy link

MaffooClock commented Mar 18, 2022

@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
Copy link

briri commented Jun 14, 2022

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

  • Clearing out any old gems: gem uninstall mysql2
  • Removing any old mariadb installs (also remove any mysql installs): brew uninstall mariadb
  • Reinstalling mariadb: brew install mariadb
  • Install the mysqld gem: gem install mysql2 --platform=ruby -- --with-mysql-dir=/usr/local/Cellar/mariadb/10.8.3_1
  • Then run bundle install in your Rails project

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

@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
Copy link

rioda78 commented Sep 17, 2023

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
Copy link

pcopissa commented Oct 19, 2023

@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
Copy link

I tried my best to contact MySQL devs to support MINGW package, yet done it:
msys2/MINGW-packages#16085

@klues
Copy link

klues commented Apr 10, 2024

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

  • installed MySQLConnector/C++ 8.3.0
  • run gem install mysql2 --platform=ruby -- --with-mysql-lib="C:\MySQL\MySQL Connector C++ 8.3\lib64"

@jnavarrete96
Copy link

Thanks, it works for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests