mysql2: Cannot install mysql2 gem on Windows 10
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.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 4
- Comments: 19
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*
mysql2gem on Ruby 2.7.3 on Windows 10 (*working: at least for basic queries).cmdterminal and runridkexplicitely (c:\your\path\to\Ruby27-x64\ridk.cmd enable). This gives you the right environment to do the rest.cmd, rungem 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:
c:\your\path\to\mysql-connector-c-6.1.11-winx64(no spaces. May or may not be important)cmdterminal and runridkexplicitely as step 1 above.cmd, rungem 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)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 resultingmysql2.sono 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-xxxxswitches without success)Also note that I tried to build with
gem install mysql2 --platform=ruby -- --use-system-librariesas suggested in the article linked above. That ended up with a different build failure (in line 1050 ofmkmf.rbrather 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-xxxxis 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
Only these Two commands works for me.
cmdterminal and runridkgem install mysql2 --platform=ruby -- --with-mysql-dir=c:/your/path/to/Ruby27-x64/msys64/mingw64Thank you @pcopissa
Same for me on W2016 with Ruby v2.7.7
I was able to get this working on OSX by (note this assumes Homebrew is installed):
gem uninstall mysql2brew uninstall mariadbbrew install mariadbgem install mysql2 --platform=ruby -- --with-mysql-dir=/usr/local/Cellar/mariadb/10.8.3_1bundle installin your Rails projectNote that I happened to do the uninstall/reinstall of mariadb but a
brew upgrademight 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).
@pcopissa’s solution put me on the right track, except I wanted to use the MariaDB library.
libmariadbfolder tolibcmdterminal and runridkgem 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 ¯\_(ツ)_/¯
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
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
Unfortunately, this doesn’t work 🙁 (the gem install was successful but throwed error when I performed
require 'mysql2')Further check, I found following the steps on https://github.com/brianmario/mysql2/issues/1348#issuecomment-2135482056, we need to perform additional steps like this
To verify it:
I’ve just managed to install
mysql2-0.5.6on Windows 10 with Ruby3.2.3this way:gem install mysql2 --platform=ruby -- --with-mysql-lib="C:\MySQL\MySQL Connector C++ 8.3\lib64"@gersonmax:
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-dirpoints where the installer will pick source files from (i.e. headers and shared libraries/DLL), expecting the classic Unix hierarchy starting at/(ie:/liband/includemostly). Since we are executing the build with a Unix-ish toolchain but in acmdprompt, 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-dirthe place which MSYS2 considers as/and that isc:/your/path/to/Ruby27-x64/msys64/mingw64when using Ruby 2.7.x (orc:/your/path/to/Ruby32-x64/msys64/ucrt64for 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.work using this command on windows 11 and ruby 3.1.4. thanks
@pcopissa Thanks a lot. it works for me having windows-10 and ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x64-mingw-ucrt]
Hi, Try with this https://medium.com/ruby-on-rails-web-application-development/installing-the-mysql2-rubyonrails-gem-on-windows-7-8-a028f44d87f3
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,