11

I am using Mac High Sierra. I am using Ruby 2.2.1 with RVM. I need to use mysql2 with this Ruby on Rails app. I do have mysql 5.7 installed. I do have xcode installed along with the command-line tools.

I am simply trying to get this company app working on my local machine. I know that I am very close but I have one final error when I do bundle install I receive the following error when I get to the mysql2 gem:

Fetching mysql2 0.3.18
Installing mysql2 0.3.18 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/ahayden/.rvm/gems/ruby-2.2.1/gems/mysql2-0.3.18/ext/mysql2
/Users/ahayden/.rvm/rubies/ruby-2.2.1/bin/ruby -r ./siteconf20180710-52515-2hjll8.rb extconf.rb
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
-----
Using mysql_config at /usr/local/bin/mysql_config
-----
checking for 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-connector-c/6.1.11/lib
-----
creating Makefile

current directory: /Users/ahayden/.rvm/gems/ruby-2.2.1/gems/mysql2-0.3.18/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/ahayden/.rvm/gems/ruby-2.2.1/gems/mysql2-0.3.18/ext/mysql2
make "DESTDIR="
compiling infile.c
compiling client.c
client.c:359:33: warning: implicit conversion loses integer precision: 'long' to 'unsigned int' [-Wshorten-64-to-32]
        elapsed_time = end_time - start_time;
                     ~ ~~~~~~~~~^~~~~~~~~~~~
client.c:1163:56: warning: implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'unsigned int' [-Wshorten-64-to-32]
  mysql2rb = mysql2_mysql_enc_name_to_rb(charset_name, charset_name_len);
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~               ^~~~~~~~~~~~~~~~
2 warnings generated.
compiling mysql2_ext.c
compiling result.c
result.c:186:17: warning: comparison of integers of different signs: 'int' and 'unsigned long' [-Wsign-compare]
  for (i = 0; i < (len - 1); i++) {
              ~ ^  ~~~~~~~
1 warning generated.
linking shared-object mysql2/mysql2.bundle
ld: library not found for -l-lpthread
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/ahayden/.rvm/gems/ruby-2.2.1/gems/mysql2-0.3.18 for inspection.
Results logged to /Users/ahayden/.rvm/gems/ruby-2.2.1/extensions/x86_64-darwin-17/2.2.0/mysql2-0.3.18/gem_make.out

An error occurred while installing mysql2 (0.3.18), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.3.18'` succeeds before bundling.

In Gemfile:
  mysql2

I've tried just installing mysql2 without the specific gem version and it works, but it did not fix my problem. It seems to boil down to the bottom of the traceback where it cannot find library -l-lpthread. Please help, I've been trying to do this all day.

Alec Hayden
  • 191
  • 2
  • 12
  • which command you used to install mysql. $brew install mysql. if you are using rails 4, i would suggest to use mysql2 gem with 0.4 series. specifically enter the version in your gemfile and then do bundle – Bijendra Jul 11 '18 at 05:12
  • For what it's worth, I ran into this error when installing a NEW version of MySQL (8.0.15) on macOS (10.14.3) via Homebrew. Regardless, Korayem's solution below worked for me. – CFitz Mar 21 '19 at 18:45

2 Answers2

48

This fixed it for me

gem install mysql2 -v '0.3.21' -- --with-cflags=\"-I/usr/local/opt/openssl/include\" --with-ldflags=\"-L/usr/local/opt/openssl/lib\"

after installing openssl

brew install openssl
Korayem
  • 12,108
  • 5
  • 69
  • 56
  • 3
    This works for me on OSX 10.14.2 with MySQL 8.0.13 installed through brew. Wasted 3 hrs before finding this command. Thanks! – bxiong Jan 11 '19 at 14:05
  • 3
    @bxiong same here! I wasted countless hours traversing the corners of the internet for a solution. I told myself I must get back to SO to share this to save other people's time. Glad it helped. Happy coding :) – Korayem Jan 12 '19 at 00:05
  • Worked for me on macOS 10.14.3 with MySQL 8.0.15 installed through Homebrew (ultimately for a Rails project utilizing the mysql2 gem v0.5.2). Recommended if you are on recent macOS and MySQL versions and you also installed openssl via Homebrew. Otherwise, I don't believe this command would do much for you. – CFitz Mar 21 '19 at 18:37
  • 5
    Another one to try: `gem install mysql2 -- --with-opt-dir="$(brew --prefix openssl)"` worked for me when the compiler error was `ld: library not found for -lssl` – ASabourin Apr 05 '19 at 04:48
  • @ASabourin you should post that as an answer – Reidsy Apr 06 '19 at 13:28
  • @ASabourin Agreed! This was the solution for my `library not found for -lssl` error as well after updating XCode on Mojave – bryanus Apr 10 '19 at 20:51
8

As per the description shared it seems like the mysql dependencies are unable to install.

First we need to ‘locate mysql_config’ and then replace the path in the following command with where that file is.

gem install mysql2 -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
phil pirozhkov
  • 4,740
  • 2
  • 33
  • 40
Rohan
  • 2,681
  • 1
  • 12
  • 18