mysql2のインストールで"Failed to build gem native extension" - ebc0729/RailApp GitHub Wiki

エラー

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

    current directory: /home/imura/workspace/RailApp/vendor/bundler/ruby/2.3.0/gems/mysql2-0.4.10/ext/mysql2
/usr/bin/ruby2.3 -r ./siteconf20180315-10917-gf8lx4.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
checking for mysql_query() in -lmysqlclient... no
-----
mysql client is missing. You may need to 'apt-get install libmysqlclient-dev' or 'yum install mysql-devel', and try again.
-----
*** 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=/usr/bin/$(RUBY_BASE_NAME)2.3
	--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-mysql-dir
	--without-mysql-dir
	--with-mysql-include
	--without-mysql-include=${mysql-dir}/include
	--with-mysql-lib
	--without-mysql-lib=${mysql-dir}/lib
	--with-mysqlclientlib
	--without-mysqlclientlib

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

  /home/imura/workspace/RailApp/vendor/bundler/ruby/2.3.0/extensions/x86_64-linux/2.3.0/mysql2-0.4.10/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/imura/workspace/RailApp/vendor/bundler/ruby/2.3.0/gems/mysql2-0.4.10 for inspection.
Results logged to /home/imura/workspace/RailApp/vendor/bundler/ruby/2.3.0/extensions/x86_64-linux/2.3.0/mysql2-0.4.10/gem_make.out

解決策

エラーメッセージに書いてありました。

mysql client is missing. You may need to 'apt-get install libmysqlclient-dev' or 'yum install mysql-devel', and try again.

したがって、以下のコマンドを叩く。

sudo apt-get install libmysqlclient-dev
bundle install --path vendor/bundler