1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Railsにmysql2をbundle installする際、詰まったメモ(Ubuntu18.04)

Last updated at Posted at 2018-12-24

この記事では、

  • Ubuntu18.04
  • mysql Ver 14.14 Distrib 5.7.24, for Linux (x86_64) using EditLine wrapper
  • Rails 4.2.10
  • Ruby 2.5.3
    の環境で詰まったことについてメモしています。

####目次
エラーその1 -lmysqlclient が見つかりません
エラーその2 mysql.hファイルが見つかりません

#エラーその1  -lmysqlclient が見つかりません
経緯です。解決法まで読み飛ばしで大丈夫です。

RailsのAPPファイルの下にあるdatabase.ymlにmysql2の設定を書き込み、bundle installを実行

:~/environment/testapp$ bundle install

中略

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

    current directory: /home/?ユーザ名?/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2/ext/mysql2
/home/?ユーザ名?/.rbenv/versions/2.5.3/bin/ruby -r ./siteconf20181221-24010-ajdc75.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.

中略

	--with-mysqlclientlib
	--without-mysqlclientlib

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

  /home/?ユーザ名?/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.0-static/mysql2-0.5.2/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/?ユーザ名?/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2 for
inspection.
Results logged to
/home/?ユーザ名?/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.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

少々長いですが、とりあえず「エラー発生```
gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'


:~/environment/testapp$ gem install mysql2 -v '0.5.2' --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.5.3/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2/ext/mysql2

/home/?ユーザ名?/.rbenv/versions/2.5.3/bin/ruby -r ./siteconf20181221-24140-1qf39s3.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-mysqlclientlib
--without-mysqlclientlib

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

/home/?ユーザ名?/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.0-static/mysql2-0.5.2/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/?ユーザ名?/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2 for inspection.
Results logged to /home/?ユーザ名?/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.0-static/mysql2-0.5.2/gem_make.out

再び長いですが、要するに「エラー発生
/home/?ユーザ名?/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.0-static/mysql2-0.5.2/mkmf.log
でエラー内容を確認せよ」とのことなので、参照。
内容は消えてしまったのですが、```
/usr/bin/ld: -lmysqlclient が見つかりません
```と出力されてました。
多分意味は、ファイル/usr/bin/ldの参照先mysqlclientが見つからないということ。
####解決法
おそらくCentOSの記事ですが[komiyakの通り道](http://d.hatena.ne.jp/komiyak/20150120/1421722579)を参考にしました。

とりあえずldconfigでmysqlclientを探す。

:~/environment/testapp$ ldconfig -p | grep mysqlclient
libmysqlclient.so.20 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20

どうやら、記事と同じくある模様。このファイルは共有ファイルとのこと。
ただ、/usr/lib64/libmysqlclient.soが足りないらしい。
追加のため、記事通りMySQL-sharedをインストールしようとしたが、Ubuntuでのインストール方法がわからず撃沈。

色々調べていると、libmysqlclient.soはlibmysqlclient.so.??(??はバージョン)に飛ばすだけらしい。要するに、他のプログラムからの参照を、指定したいバージョンに誘導するということ。[wagavulin's blog  Linux共有ライブラリの簡単なまとめ](https://www.wagavulin.jp/entry/20091026/1256577635)によると簡単に作成できるようなので作成しました。

:/usr/lib/x86_64-linux-gnu$ sudo ln -s libmysqlclient.so.20.3.11 libmysqlclient.so

これでとりあえず大丈夫なはずなので、再びbundle installを実行。
これに関しては解決しました。
#エラーその2 mysql.hファイルが見つかりません
再び経緯です。解決法まで読み飛ばして結構です。

bundle installを実行結果

:~/environment/testapp$ bundle install
Fetching gem metadata from https://rubygems.org/..........

中略

Fetching mysql2 0.5.2
Installing mysql2 0.5.2 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory: /home/?ユーザ名?/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2/ext/mysql2

/home/?ユーザ名?/.rbenv/versions/2.5.3/bin/ruby -r ./siteconf20181224-31640-1c080i6.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... yes
checking for mysql.h... no
checking for mysql/mysql.h... no

mysql.h 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-mysqlclientlib
--without-mysqlclientlib

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

/home/?ユーザ名?/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.0-static/mysql2-0.5.2/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/?ユーザ名?/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2 for
inspection.
Results logged to
/home/?ユーザ名?/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.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

またまた長いですが、要するに「エラー発生```
 /home/?ユーザ名?/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.0-static/mysql2-0.5.2/mkmf.log
```を参照せよ」とのことなので参照。
また内容が消えてしまったのですが、「mysql.hが見つかりません」とのこと。
####解決法
[stack overflow](https://stackoverflow.com/questions/14604228/mysql-h-file-cant-be-found)でコレの質問があり、回答を参考に、libmysqlclient-devをインストールしました。

~/environment/testapp$ sudo apt-get install libmysqlclient-dev

このあとに、bundle installを実行すると成功しました。

1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?