Ubuntu 20.04 で mysql2 gem のインストールに失敗するとき

結論

libmysqlclient-dev を入れましょう*1

$ sudo apt install -y libmysqlclient-dev

これで bundle install が通るはずです。

参考

検索用に、エラーメッセージ例です。

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

current directory:
/FOO/vendor/bundle/ruby/3.0.0/gems/mysql2-0.5.3/ext/mysql2
/home/BAR/.rbenv/versions/3.0.2/bin/ruby -I
/home/BAR/.rbenv/versions/3.0.2/lib/ruby/3.0.0 -r
./siteconf20210820-21651-7t078b.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=.
        --curdir
        --ruby=/home/BAR/.rbenv/versions/3.0.2/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
/home/BAR/.rbenv/versions/3.0.2/lib/ruby/3.0.0/mkmf.rb:1050:in `block in
find_library': undefined method `split' for nil:NilClass (NoMethodError)
        from /home/BAR/.rbenv/versions/3.0.2/lib/ruby/3.0.0/mkmf.rb:1050:in `cofor inspection.
Results logged to
/home/BAR/FOO/vendor/bundle/ruby/3.0.0/extensions/x86_64-linux/3.0.0/mysql2-0.5.3/gem_make.out

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.

In Gemfile:
  mysql2

*1:2021/08/20 現在

Powered by はてなブログ