LoginSignup
4

More than 3 years have passed since last update.

[Rails] bundle install 中に gem 'mysql' のところで失敗する場合の対策(CentOS)

Last updated at Posted at 2019-09-23

原因は色々あるが、今回については以下のエラーにより、mysql-develがインストールされていないせいだと判明。

error

$ bundle install --path vendor/bundle
...
Installing mysql2 0.5.2 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/home/sasaki/lab/rails/laboratory/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.2/ext/mysql2
/usr/local/rbenv/versions/2.6.3/bin/ruby -I
/usr/local/rbenv/versions/2.6.3/lib/ruby/2.6.0 -r
./siteconf20190923-14019-1jjewwe.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
checking for -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/local/rbenv/versions/2.6.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-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/sasaki/lab/rails/laboratory/vendor/bundle/ruby/2.6.0/extensions/x86_64-linux/2.6.0-static/mysql2-0.5.2/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/home/sasaki/lab/rails/laboratory/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.2
for inspection.
Results logged to
/home/sasaki/lab/rails/laboratory/vendor/bundle/ruby/2.6.0/extensions/x86_64-linux/2.6.0-static/mysql2-0.5.2/gem_make.out

An error occurred while installing mysql2 (0.5.2), and
Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.2' --source
'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  mysql2

上記エラーのうち、注目すべきはここ

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

mysql-devel入れてよ、とのこと

対策

mysql-community 系を使っている人は、上記メッセージの通りにコマンドを打っても失敗する。

こうなる
$ sudo yum install mysql-devel
[sudo] username のパスワード:
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.cat.net
 * epel: mirror01.idc.hinet.net
 * extras: mirrors.cat.net
 * ius: hkg.mirror.rackspace.com
 * updates: mirrors.cat.net
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ mariadb101u-devel.x86_64 1:10.1.35-1.ius.centos7  を インストール
--> 依存性の処理をしています: mariadb101u-libs(x86-64) = 1:10.1.35-1.ius.centos7 のパッケージ: 1:mariadb101u-devel-10.1.35-1.ius.centos7.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ mariadb101u-libs.x86_64 1:10.1.35-1.ius.centos7 を インストール
--> 依存性の処理をしています: mariadb101u-common(x86-64) = 1:10.1.35-1.ius.centos7 のパッケージ: 1:mariadb101u-libs-10.1.35-1.ius.centos7.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ mariadb101u-common.x86_64 1:10.1.35-1.ius.centos7 を インストール
--> 依存性の処理をしています: mariadb101u-config(x86-64) = 1:10.1.35-1.ius.centos7 のパッケージ: 1:mariadb101u-common-10.1.35-1.ius.centos7.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ mariadb101u-config.x86_64 1:10.1.35-1.ius.centos7 を インストール
--> 衝突を処理しています: 1:mariadb101u-config-10.1.35-1.ius.centos7.x86_64 は mysql-community-server と衝突しています
--> 衝突を処理しています: 1:mariadb101u-common-10.1.35-1.ius.centos7.x86_64 は mysql-community-common と衝突しています
--> 衝突を処理しています: 1:mariadb101u-libs-10.1.35-1.ius.centos7.x86_64 は mysql-community-libs と衝突しています
--> 衝突を処理しています: 1:mariadb101u-libs-10.1.35-1.ius.centos7.x86_64 は mysql-community-libs-compat と衝突しています
--> 依存性解決を終了しました。
エラー: mariadb101u-common conflicts with mysql-community-common-5.7.27-1.el7.x86_64
エラー: mariadb101u-libs conflicts with mysql-community-libs-compat-5.7.27-1.el7.x86_64
エラー: mariadb101u-config conflicts with mysql-community-server-5.7.27-1.el7.x86_64
エラー: mariadb101u-libs conflicts with mysql-community-libs-5.7.27-1.el7.x86_64
 問題を回避するために --skip-broken を用いることができます。
 これらを試行できます: rpm -Va --nofiles --nodigest

ではどうするか
mysql-devel ではなく、 mysql-community-devel を入れる

すると、インストールでき、bundle install も成功した

実際にmysql-develインストールに使ったコマンド

# 他のもまとめてインストールした
$ sudo yum install --enablerepo=mysql57-community mysql-community-server mysql-community-devel

参考資料

railsでmysql(無料版)を使う人は、この記事の通りにやれば大丈夫そう

おまけ

文字コード変更はしておいた方がよさそう?
気になる方はコチラを参考にどうぞ

CentOS 7にMySQLをインストールして初期設定するまで - 文字コードの確認と変更

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
4