気ままに

プログラム関連で困ったことを調べて気ままに投稿

【Rails】bundle installでMySQLでエラーになり、インストールできない

不用意にbrew upgradeしたからか、OSをHighSierraにした影響か、最近bundle installでコケることが増えた気がします、、、

Ruby 2.3.0 Mac OS:High Sierra

bundle install すると以下のようなOpenSSLのエラーが

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/rails_app/vendor/bundle/ruby/2.3.0/gems/mysql2-0.3.21/ext/mysql2
/Users/rails_app/.rbenv/versions/2.3.1/bin/ruby -r ./siteconf20180118-22670-1twmgh2.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/5.7.20_1/lib
-----
creating Makefile

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

  /Users/rails_app/vendor/bundle/ruby/2.3.0/extensions/x86_64-darwin-16/2.3.0-static/mysql2-0.3.21/mkmf.log

current directory: /Users/rails_app/vendor/bundle/ruby/2.3.0/gems/mysql2-0.3.21/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/rails_app/vendor/bundle/ruby/2.3.0/gems/mysql2-0.3.21/ext/mysql2
make "DESTDIR="
compiling infile.c
compiling client.c
compiling mysql2_ext.c
compiling result.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/rails_app/vendor/bundle/ruby/2.3.0/gems/mysql2-0.3.21 for inspection.
have_func: checking for rb_thread_blocking_region()... -------------------- no

"clang -o conftest -I/Users/rails_app/.rbenv/versions/2.3.1/include/ruby-2.3.0/x86_64-darwin16 -I/Users/rails_app/.rbenv/versions/2.3.1/include/ruby-2.3.0/ruby/backward -I/Users/rails_app/.rbenv/versions/2.3.1/include/ruby-2.3.0 -I. -I/Users/rails_app/.rbenv/versions/2.3.1/include  -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT    -O3 -Wno-error=shorten-64-to-32  -pipe conftest.c  -L. -L/Users/rails_app/.rbenv/versions/2.3.1/lib -L. -L/Users/rails_app/.rbenv/versions/2.3.1/lib  -fstack-protector -L/usr/local/lib     -lruby-static -framework CoreFoundation  -lpthread -ldl -lobjc "
conftest.c:13:57: error: use of undeclared identifier 'rb_thread_blocking_region'
int t(void) { void ((*volatile p)()); p = (void ((*)()))rb_thread_blocking_region; return !p; }
                                                        ^
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:     printf("%p", &t);
 9:   }
10:
11:   return 0;
12: }
13: int t(void) { void ((*volatile p)()); p = (void ((*)()))rb_thread_blocking_region; return !p; }
/* end */

"clang -o conftest -I/Users/rails_app/.rbenv/versions/2.3.1/include/ruby-2.3.0/x86_64-darwin16 -I/Users/rails_app/.rbenv/versions/2.3.1/include/ruby-2.3.0/ruby/backward -I/Users/rails_app/.rbenv/versions/2.3.1/include/ruby-2.3.0 -I. -I/Users/rails_app/.rbenv/versions/2.3.1/include  -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT    -O3 -Wno-error=shorten-64-to-32  -pipe conftest.c  -L. -L/Users/rails_app/.rbenv/versions/2.3.1/lib -L. -L/Users/rails_app/.rbenv/versions/2.3.1/lib  -fstack-protector -L/usr/local/lib     -lruby-static -framework CoreFoundation  -lpthread -ldl -lobjc "
Undefined symbols for architecture x86_64:
  "_rb_thread_blocking_region", referenced from:
      _t in conftest-188205.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
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:     printf("%p", &t);
 9:   }
10:
11:   return 0;
12: }
13: extern void rb_thread_blocking_region();
14: int t(void) { rb_thread_blocking_region(); return 0; }
/* end */

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

have_func: checking for rb_wait_for_single_fd()... -------------------- yes
Results logged to /Users/rails_app/vendor/bundle/ruby/2.3.0/extensions/x86_64-darwin-16/2.3.0-static/mysql2-0.3.21/gem_make.out

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

qiita.com

を参考に、以下のコマンド実行後にbundle install すると無事、エラーがでなくなりました。

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

パーフェクトRuby on Rails

パーフェクトRuby on Rails