7

I tried every solution similar to the question:

Recently , I moved from ubuntu to Mac and I'm trying to install mysql gem on Sierra and after I had installed Ruby , Rails , Mysql,

also I type brew install mysql and it worked for download mysql but not the gem , so my question is not similar.

I typed this mysql --version

and I got mysql Ver 14.14 Distrib 5.7.16, for osx10.12 (x86_64) using EditLine wrapper

I tried to install mysql2 gem for rails to build a new app

I typed this sudo gem install mysql2 and got this error:

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

current directory: /Users/mohammed.elias/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5/ext/mysql2
/Users/mohammed.elias/.rbenv/versions/2.4.0/bin/ruby -r ./siteconf20170102-2045-18gcs95.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
checking for rb_big_cmp()... yes
-----
Using mysql_config at /usr/local/bin/mysql_config
-----
checking for mysql.h... yes
checking for SSL_MODE_DISABLED in mysql.h... yes
checking for SSL_MODE_PREFERRED in mysql.h... yes
checking for SSL_MODE_REQUIRED in mysql.h... yes
checking for SSL_MODE_VERIFY_CA in mysql.h... yes
checking for SSL_MODE_VERIFY_IDENTITY in mysql.h... yes
checking for errmsg.h... yes
checking for mysqld_error.h... yes
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/Cellar/mysql/5.7.16/lib
-----
creating Makefile

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

  /Users/mohammed.elias/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/extensions/x86_64-darwin-16/2.4.0-static/mysql2-0.4.5/mkmf.log

current directory:     /Users/mohammed.elias/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5/ext/mysql2
make "DESTDIR=" clean

current directory:     /Users/mohammed.elias/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1

make failed, exit code 2

Gem files will remain installed in     /Users/mohammed.elias/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/mysq l2-0.4.5 for inspection.
Results logged to     /Users/mohammed.elias/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/extensions/x86_64-darwin-16/2.4.0-static/mysql2-0.4.5/gem_make.out
Chidi Ekuma
  • 2,811
  • 2
  • 19
  • 30
MEH
  • 1,777
  • 3
  • 15
  • 34
  • Possible duplicate of [Error installing mysql2: Failed to build gem native extension](http://stackoverflow.com/questions/3608287/error-installing-mysql2-failed-to-build-gem-native-extension) – Ilya Jan 02 '17 at 10:55
  • 1
    From the traces, we can see that the build system find the mysql headers but fails on loading the ssl files (from OpenSSL). You could try the solution offered in an [issue on github](https://github.com/brianmario/mysql2/issues/795) on this topic. If that fails, you *might* succeed by running `brew install openssl`. – Holger Just Jan 02 '17 at 11:01
  • For what it's worth I just installed the gem successfully on a fresh install of Ruby 2.4.0. What's inside the error log `/Users/mohammed.elias/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/extensions/x86_64-darwin-16/2.4.0-static/mysql2-0.4.5/mkmf.log`? –  Jan 02 '17 at 11:08

4 Answers4

7

A good answer to my question and thanks to @Holger Just to refer to this issue on githup ld: library not found for -lssl` after Mac OS Sierra upgrade

MEH
  • 1,777
  • 3
  • 15
  • 34
6

Had this exact same problem when installing MySQL2 gem on a fresh Macbook Pro Sierra.

You need to alter your environment variables to include the following (just replace the 0.0.00 with the desired version of MySQL2 gem:

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

Also, your $PATH should be set up with mysql with bash profile, a list of settings that will be run every time bash starts. Your bash profile can be found in your home directory under the filename .bash_profile. If it is not created, you can create the text file using the command nano .bash_profile (you should be in your home directory (~)). Add this to your bash profile:

export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"

This line will add the above directories to your path, including the mysql path. Make sure you restart your terminal, as .bash_profile only runs when the terminal is initially loaded.

the12
  • 2,395
  • 5
  • 19
  • 37
1

Running xcode-select --install for some reason, solved the issue for me.

FutoRicky
  • 903
  • 2
  • 9
  • 22
1

For anybody still experiencing the issue:

Run xcode-select --install will set up your command line tools

then set these build flags (for the local application) by running the following:

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

and then finally: bundle install to install the mysql2 gem and other gems worked for me.

Chidi Ekuma
  • 2,811
  • 2
  • 19
  • 30