Help us understand the problem. What is going on with this article?

【Tips】Ubuntuへのmysql2 gemインストール時に発生しうるエラーへの対処

問題

Ruby on Rails 等で利用する Ruby の MySQL 用ライブラリmysql2インストールが失敗する。

入力

gem install mysql2

出力

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

    current directory: /home/【ユーザー名】/.anyenv/envs/rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/mysql2-0.5.3/ext/mysql2
/home/【ユーザー名】/.anyenv/envs/rbenv/versions/2.7.0/bin/ruby -I /home/【ユーザー名】/.anyenv/envs/rbenv/versions/2.7.0/lib/ruby/2.7.0 -r ./siteconf20200222-2823-u3a2hd.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/【ユーザー名】/.anyenv/envs/rbenv/versions/2.7.0/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/【ユーザー名】/.anyenv/envs/rbenv/versions/2.7.0/lib/ruby/2.7.0/mkmf.rb:1050:in `block in find_library': undefined method `split' for nil:NilClass (NoMethodError)
        from /home/【ユーザー名】/.anyenv/envs/rbenv/versions/2.7.0/lib/ruby/2.7.0/mkmf.rb:1050:in `collect'
        from /home/【ユーザー名】/.anyenv/envs/rbenv/versions/2.7.0/lib/ruby/2.7.0/mkmf.rb:1050:in `find_library'
        from extconf.rb:87:in `<main>'

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

  /home/【ユーザー名】/.anyenv/envs/rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/extensions/x86_64-linux/2.7.0/mysql2-0.5.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/【ユーザー名】/.anyenv/envs/rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/mysql2-0.5.3 for inspection.
Results logged to /home/【ユーザー名】/.anyenv/envs/rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/extensions/x86_64-linux/2.7.0/mysql2-0.5.3/gem_make.out

環境

  • Windows Subsystem for Linux (WSL)
    • Ubuntu 18.04 LTS
  • anyenv 1.1.1
    • rbenv 1.1.2-20-g143b2c9
      • ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux]

原因:MySQLの開発用ライブラリ不足

「Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers.」とあるように、native extensionとして必要なMySQLに関連するライブラリがインストールされていない。

解決方法

入力

sudo apt install libmysqld-dev
gem install mysql2

出力

Building native extensions. This could take a while...
Successfully installed mysql2-0.5.3
Parsing documentation for mysql2-0.5.3
Installing ri documentation for mysql2-0.5.3
Done installing documentation for mysql2 after 0 seconds
1 gem installed

参考文献

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした