mysql2 gem fails on bundle install, despite installing using `gem install...` command
Asked Answered
H

3

7

Here is the log from my bundle install

Fetching mysql2 0.5.2
Installing mysql2 0.5.2 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
...
...
...
Cannot find mysql_config at /usr/local/opt/[email protected]/bin/mysql_config
--with-ldflags=-L/usr/local/opt/openssl/lib
--with-cppflags=-I/usr/local/opt/openssl/include
-----
*** 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=/Users/admin/.asdf/installs/ruby/2.6.3/bin/$(RUBY_BASE_NAME)
    --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-mysql-config

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

/Users/admin/development/locari/vendor/bundle/ruby/2.6.0/extensions/x86_64-darwin-18/2.6.0-static/mysql2-0.5.2/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/Users/admin/development/locari/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.2 for
inspection.
Results logged to
/Users/admin/development/locari/vendor/bundle/ruby/2.6.0/extensions/x86_64-darwin-18/2.6.0-static/mysql2-0.5.2/gem_make.out

An error occurred while installing mysql2 (0.5.2), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'` succeeds
before bundling.

So I do what it says, and run sudo gem install mysql2 -v '0.5.2' --source

$ sudo gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'
Building native extensions. This could take a while...
Successfully installed mysql2-0.5.2
Parsing documentation for mysql2-0.5.2
Done installing documentation for mysql2 after 0 seconds
1 gem installed

And it installs fine! But when I run bundle install again, I get the same error. I've tried uninstalling, unlinking, and reinstalling mysql in brew.

What can I try next??

Hescock answered 27/6, 2019 at 8:29 Comment(6)
are you using windows? or LinuX?Alika
@kishorecheruku MacOSHescock
https://mcmap.net/q/399734/-mysql2-gem-fails-to-compile-with-mysql-5-6-12-on-os-x-with-homebrewPlascencia
first try to install mysql in local. then you can try bundle installAlika
@Plascencia Nothing from that page helps @kishorecheruku I already said, it works when I install locally, just not when i run it as part of bundle installHescock
This one $bundle config build.mysql2 --with-mysql-config=/usr/local/Cellar/mysql/5.6.10/bin/mysql_configPlascencia
H
6

Run these commands first:

bundle config --local build.mysql2 "--with-cppflags=-I/usr/local/opt/openssl/include --with-ldflags=-L/usr/local/opt/openssl/lib"

Then run bundle install it works.

Hescock answered 27/6, 2019 at 9:27 Comment(2)
Running both of these commands separately just overwrites the first one. You need to put both of these install options into a single string on the first command.Ilocano
This may depend on the bundler version, but when I put both in a single string it passes the entire string as a single parameter which then gets silently ignored when building. The thing that worked for me is to just set bundle config --local build.mysql2 --with-ldflags=-L/usr/local/opt/openssl/libStendhal
P
6

Installation on M1 macs

This method helped me on ruby v2.7.2 and macOS Big Sur 11.4.

Install dependencies

brew install mysql zstd openssl

Install the gem

gem install mysql2 -- --with-ldflags="-L $(brew --cellar zstd)/1.5.0/lib -L $(brew --prefix openssl)/lib" --with-ldlibs="-lzstd -lssl" --with-cppflags="-I $(brew --prefix openssl)/include"

As an alternative, you can modify bundler config:

bundle config --local build.mysql2 --with-ldflags="-L $(brew --cellar zstd)/1.5.0/lib -L $(brew --prefix openssl)/lib" --with-ldlibs="-lzstd -lssl" --with-cppflags="-I $(brew --prefix openssl)/include"

and run bundle install.

Plassey answered 28/8, 2021 at 19:55 Comment(1)
I don't have an M1 mac, but this worked for me!Cecrops
I
3

Use build options with bundler.

Try running this in your application directory:

bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"

This sets it just for your application, which can be handy for checking into your repo if others need a similar configuration. But you can also make it a global configuration on your computer by replacing --local with --global and it will apply to all your bundle install commands on your User account.

Update

This actually didn't end up working for me but I'll leave it here because it should have worked. I basically just had to run:

gem install mysql2 -v '0.5.3' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include

Instead of using bundler, even though setting this build config should have done this.

Ilocano answered 9/9, 2020 at 1:51 Comment(2)
thanks, this helped me!Raynell
@Raynell You're welcome! You should upvote it if it helped you. ;)Ilocano

© 2022 - 2025 — McMap. All rights reserved.