LoginSignup
1
0

More than 1 year has passed since last update.

CentOS7.9 gem install mysql2でエラーが出た際の対処法

Last updated at Posted at 2021-06-09

はじめに

Railsにて開発中にSQLiteからMySQLへの移行作業をおこなっていた。

gemfileにmysqlを加えて、sqliteをコメントアウトしてからbundle installをおこなった。

開発環境

・MacBook Pro
プロセッサ2 GHz クアッドコアIntel Core i5
メモリ16GB
・virtualbox+vagrant+CentOs7.9

作業

$ bundle install

bundle installをすると下記のエラーが発生する。

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.

gem install mysql2をおこなうように指示あり。

$ gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'

指示に従うが、再びエラーが発生する。

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

    current directory: /home/ユーザー名/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/mysql2-0.5.3/ext/mysql2
/home/ユーザー名/.rbenv/versions/2.7.3/bin/ruby -I /home/ユーザー名/.rbenv/versions/2.7.3/lib/ruby/2.7.0 -r ./siteconf20210609-17717-kiy8y5.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/ユーザー名/.rbenv/versions/2.7.3/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/ユーザー名/.rbenv/versions/2.7.3/lib/ruby/2.7.0/mkmf.rb:1050:in `block in find_library': undefined method `split' for nil:NilClass (NoMethodError)
    from /home/ユーザー名/.rbenv/versions/2.7.3/lib/ruby/2.7.0/mkmf.rb:1050:in `collect'
    from /home/ユーザー名/.rbenv/versions/2.7.3/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/ユーザー名/.rbenv/versions/2.7.3/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/ユーザー名/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/mysql2-0.5.3 for inspection.
Results logged to /home/ユーザー名/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/extensions/x86_64-linux/2.7.0/mysql2-0.5.3/gem_make.out

Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.
→「MySQLに関連するライブラリやヘッダがないのかもしれません」とのこと。

$ sudo yum install mysql-devel

MySQLモジュールを使うCプログラムをコンパイルするときには、このパッケージが必要とのこと。(teratail.comの回答参照)
mysql-develをインストールする。

$ gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'
$ bundle install

その後、上記の手順でコマンドを入力し、無事にbundle installが完了しました。
ご覧いただきありがとうございました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0