環境
Ruby 2.5.7
Rails 5.2.4
経緯
自分のメモ用記事ですが、今回のエラーを解説している記事が見つからなかったので。どなたかの参考になれば幸いです。
学習のため、現在作成中のRailsアプリケーションのテンプレートエンジンを、erbからslimに変更しようとした。
重めの改修になりそうだったので既存のアプリケーションはそのままに、コピーを作成してそれをslim化させる。
Qiita - cloneしたリポジトリを別リポジトリとしてリモートにpushする
GitHubのリポジトリやアプリケーションのコピーは完了し、コピーしたslim用のappディレクトリでbundle installを試みたところ下記のようなエラーが発生した。
(この時点でコピー元のアプリケーションではエラーは発生していません。)
$ bundle install
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/.........
...
Using gem一覧
...
Fetching mysql2 0.5.3
Installing mysql2 0.5.3 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /home/vagrant/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/mysql2-0.5.3/ext/mysql2
/home/vagrant/.rbenv/versions/2.5.7/bin/ruby -r ./siteconf20200915-19570-14xz74g.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 'sudo apt-get install libmariadb-dev', 'sudo apt-get install libmysqlclient-dev' or 'sudo 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=/home/vagrant/.rbenv/versions/2.5.7/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/vagrant/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.0/mysql2-0.5.3/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /home/vagrant/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/mysql2-0.5.3 for inspection.
Results logged to /home/vagrant/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.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
解決策
mysql client is missing. You may need to 'sudo apt-get install libmariadb-dev', 'sudo apt-get install libmysqlclient-dev' or 'sudo yum install mysql-devel', and try again.
エラーのこの一文、3パターンのコマンドを試してみてねとあるので、順番にやってみる。
$ sudo apt-get install libmariadb-dev
sudo: apt-get: command not found
apt-getコマンドが見つからないと言われたので、2つ目の'sudo apt-get install libmysqlclient-dev'は飛ばして3つ目の'sudo yum install mysql-devel'を試す。
$ sudo yum install mysql-devel
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Determining fastest mirrors
* base: ftp.tsukuba.wide.ad.jp
* extras: ftp.tsukuba.wide.ad.jp
* updates: ftp.tsukuba.wide.ad.jp
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
nodesource | 2.5 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/3): extras/7/x86_64/primary_db | 206 kB 00:00:00
(2/3): updates/7/x86_64/primary_db | 4.5 MB 00:00:00
(3/3): nodesource/x86_64/primary_db | 43 kB 00:00:01
Resolving Dependencies
--> Running transaction check
---> Package mariadb-devel.x86_64 1:5.5.65-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================================================================================
Installing:
mariadb-devel x86_64 1:5.5.65-1.el7 base 756 k
Transaction Summary
==============================================================================================================================================================================================
Install 1 Package
Total download size: 756 k
Installed size: 3.3 M
Is this ok [y/d/N]: y
Downloading packages:
mariadb-devel-5.5.65-1.el7.x86_64.rpm | 756 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 1:mariadb-devel-5.5.65-1.el7.x86_64 1/1
Verifying : 1:mariadb-devel-5.5.65-1.el7.x86_64 1/1
Installed:
mariadb-devel.x86_64 1:5.5.65-1.el7
Complete!
途中でパッケージのダウンロードを行うか聞かれるのでyを入力しEnter。
コマンドに入力したmysql-develと言うのはサーバ上でmysqlを動作させるために必要なパッケージ管理システムで、今回はその中で不足していたmariadb-develをダウンロードした形です。
ダウンロードが正常に終わり、再度
$bundle install
を行うと正常に完了しました。
まとめ
パッケージ管理システムは理解できたのですが、mariadb-develが今回のエラーにどのように関わっていたのか、いまいち理解ができなかったので、詳しい方はコメント等でご教授いただけると幸いです。
また質問や解釈の違い、記述方法に違和感ありましたら、コメント等でご指摘いただけると幸いです。
最後まで読んでいただきありがとうございました。
参考サイト
Qiita - cloneしたリポジトリを別リポジトリとしてリモートにpushする
Hatena Blog - CentOSにMySQLをインストール