unable to install mysql2 Gem on Bigsur
Asked Answered
A

5

6

I am using ruby 3.0.1p64 and using macOS bigsur 11.04 mysql installed and running

Bundle install asks me to install mysql2

I am trying to install mysql2 gem like this:

sudo gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'

and I am getting this:

 Building native extensions. This could take a while...
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

    current directory: /Users/henri/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/mysql2-0.5.3/ext/mysql2
/Users/henri/.rbenv/versions/3.0.1/bin/ruby -I /Users/henri/.rbenv/versions/3.0.1/lib/ruby/3.0.0 -r ./siteconf20210617-53969-y4phap.rb extconf.rb
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=.
    --curdier
    --ruby=/Users/henri/.rbenv/versions/3.0.1/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
    --without-mysql-config
    --with-mysqlclient-dir
    --without-mysqlclient-dir
    --with-mysqlclient-include
    --without-mysqlclient-include=${mysqlclient-dir}/include
    --with-mysqlclient-lib
    --without-mysqlclient-lib=${mysqlclient-dir}/lib
    --with-mysqlclientlib
    --without-mysqlclientlib
/Users/henri/.rbenv/versions/3.0.1/lib/ruby/3.0.0/mkmf.rb:1050:in `block in find_library': undefined method `split' for nil:NilClass (NoMethodError)
    from /Users/henri/.rbenv/versions/3.0.1/lib/ruby/3.0.0/mkmf.rb:1050:in `collect'
    from /Users/henri/.rbenv/versions/3.0.1/lib/ruby/3.0.0/mkmf.rb:1050:in `find_library'
    from extconf.rb:87:in `<main>'

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

  /Users/henri/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/extensions/arm64-darwin-20/3.0.0/mysql2-0.5.3/mkmf.log

extconf failed, exit code 1

and my mkmf.log looks like this:

    --------------------

have_func: checking for rb_wait_for_single_fd()... -------------------- yes

"clang -fdeclspec -o conftest -I/Users/henriknafo/.rbenv/versions/3.0.1/include/ru$
conftest.c:14:57: error: use of undeclared identifier 'rb_wait_for_single_fd'
int t(void) { void ((*volatile p)()); p = (void ((*)()))rb_wait_for_single_fd; ret$
                                                        ^
1 error generated.
checked program was:
/* begin */
 1: #include "ruby.h"
 2:
 3: /*top*/
 4: extern int t(void);
 5: int main(int argc, char **argv)
 6: {
 7:   if (argc > 1000000) {
 8:     int (* volatile tp)(void)=(int (*)(void))&t;
 9:     printf("%d", (*tp)());
10:   }
11:
12:   return !!argv[argc];
13: }
14: int t(void) { void ((*volatile p)()); p = (void ((*)()))rb_wait_for_single_fd;$
/* end */

Could this be incompatible with ruby 3.0.1? what library should I install so that mysql2 gem get's install

anyone having a suggestion?

Antimasque answered 18/6, 2021 at 11:50 Comment(3)
Don't install gems with sudo. In addition to the security concerns you're just setting yourself up for future issues with file permissions. moncefbelyamani.com/…Refugee
Save issue, although not using sudo, of course.Ably
If you're on a M1 / arm64 Mac, check out this Github Comment - it worked for me: github.com/brianmario/mysql2/issues/1175#issuecomment-910236422Steamheated
A
17

I installed mysql2 prior to bundle install and deleted the folder called .bundle prior to install:

gem install mysql2 -v '0.5.3' -- \
 --with-mysql-lib=/opt/homebrew/Cellar/mysql/8.0.25_1/lib \
 --with-mysql-dir=/opt/homebrew/Cellar/mysql/8.0.25_1 \
 --with-mysql-config=/opt/homebrew/Cellar/mysql/8.0.25_1/bin/mysql_config \
 --with-mysql-include=/opt/homebrew/Cellar/mysql/8.0.25_1/include

Now I am good.

Antimasque answered 29/6, 2021 at 15:39 Comment(2)
You might need to update your paths to point towards the version you have installed, but after that it works like a charm!Trinidadtrinitarian
For intel Mac change paths to /usr/local/Cellar/ After many months my issue is finally resolved thanks so muchIsoleucine
J
4
  1. Check if openssl installed.
  2. install it if is not installed. brew install openssl
  3. link it. brew link openssl

you will get something like this.

 ➜ brew link openssl
Warning: Refusing to link macOS provided/shadowed software: [email protected]
If you need to have [email protected] first in your PATH, run:
echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.zshrc

For compilers to find [email protected] you may need to set:
export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
export CPPFLAGS="-I/usr/local/opt/[email protected]/include"

For pkg-config to find [email protected] you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/[email protected]/lib/pkgconfig"
  1. Copy
  export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
  export CPPFLAGS="-I/usr/local/opt/[email protected]/include"

and

  export PKG_CONFIG_PATH="/usr/local/opt/[email protected]/lib/pkgconfig"

then run it.

  1. bundle install again

It works in on my projects.

UPDATE:

  1. export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/
  2. bundle install

might works.

Johanna answered 18/6, 2021 at 16:57 Comment(4)
Didn't work in my case. I still get the same error ld: library not found for -lsslAntimasque
Ediited answer.Johanna
This worked for me doing an install on Monterrey.Proximo
This solution worked perfectly for me. I am using macOS Monterey.Reflexive
S
1

Im using Macbook OS Big Sur and this worked for me:

brew install openssl
brew link openssl

  export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
  export CPPFLAGS="-I/usr/local/opt/[email protected]/include"
  export PKG_CONFIG_PATH="/usr/local/opt/[email protected]/lib/pkgconfig"

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

bundle install
Smoker answered 10/9, 2021 at 19:53 Comment(0)
A
0

here is what worked for me:

brew install openssl

brew link mysql

Antimasque answered 18/6, 2021 at 11:55 Comment(1)
Now when I start rails s... I get an error saying: An error occurred while installing mysql2 (0.5.3), and Bundler cannot continue. Make sure that gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/' succeeds before bundling.Antimasque
M
0

You probably need libmysqlclient-dev Take a look at last comment (https://mcmap.net/q/1174311/-error-installing-mysql2-error-failed-to-build-gem-native-extension) on Error installing mysql2: ERROR: Failed to build gem native extension thread

Maunder answered 28/6, 2021 at 12:24 Comment(1)
I tried to brew install libmysqlclient-dev and it did not find the library. I even installed mysql-connector-c and not working.Antimasque

© 2022 - 2025 — McMap. All rights reserved.