mysql2をbundle installできないし、brew info opensslも動かない


どうしたの?

状況:
- Gemfileにmysql2をbundle installしようとしたところで、インストールエラーが発生。
- ググると$ brew info opensslを勧められるも動かない

やりたいこと:
- デプロイ用のDBをPostgreSQLからMySQLに変更したく、まずはGemをインストールしたい。

環境:
- Rails 6.0.2
- Ruby 2.6.5
- gem mysql2(バージョン指定なし)
- AWSのEC2インスタンス内で操作(SSHを使用してインスタンスに接続)

もう少し説明

mysql2 gemをGemfileに記述し、bundle installをすると以下が発生

ターミナル
$ bundle

Your Gemfile lists the gem devise (>= 0) more than once.
You should probably keep only one of them.
Remove any duplicate entries and specify the gem only once.
While it's not a problem now, it could cause errors if you change the version of one of them later.
Your Gemfile lists the gem omniauth-google-oauth2 (>= 0) more than once.
You should probably keep only one of them.
Remove any duplicate entries and specify the gem only once.
While it's not a problem now, it could cause errors if you change the version of one of them later.
Your Gemfile lists the gem dotenv-rails (>= 0) more than once.
You should probably keep only one of them.
Remove any duplicate entries and specify the gem only once.
While it's not a problem now, it could cause errors if you change the version of one of them later.
Fetching gem metadata from https://rubygems.org/............
Resolving dependencies...
Using rake 13.0.1
Using concurrent-ruby 1.1.6
Using i18n 1.8.3
Using minitest 5.14.1
Using thread_safe 0.3.6
Using tzinfo 1.2.7
Using zeitwerk 2.3.0
Using activesupport 6.0.3.1
Using builder 3.2.4
Using erubi 1.9.0
Using mini_portile2 2.4.0
Using nokogiri 1.10.9
Using rails-dom-testing 2.0.3
Using crass 1.0.6
Using loofah 2.6.0
Using rails-html-sanitizer 1.3.0
Using actionview 6.0.3.1
Using rack 2.2.3
Using rack-test 1.1.0
Using actionpack 6.0.3.1
Using nio4r 2.5.2
Using websocket-extensions 0.1.5
Using websocket-driver 0.7.2
Using actioncable 6.0.3.1
Using globalid 0.4.2
Using activejob 6.0.3.1
Using activemodel 6.0.3.1
Using activerecord 6.0.3.1
Using mimemagic 0.3.10
Using marcel 0.3.3
Using activestorage 6.0.3.1
Using mini_mime 1.0.2
Using mail 2.7.1
Using actionmailbox 6.0.3.1
Using actionmailer 6.0.3.1
Using actiontext 6.0.3.1
Using arbre 1.2.1
Using formtastic 3.1.5
Using formtastic_i18n 0.6.0
Using has_scope 0.7.2
Using method_source 1.0.0
Using thor 1.0.1
Using railties 6.0.3.1
Using responders 3.0.1
Using inherited_resources 1.11.0
Using jquery-rails 4.4.0
Using kaminari-core 1.2.1
Using kaminari-actionview 1.2.1
Using kaminari-activerecord 1.2.1
Using kaminari 1.2.1
Using polyamorous 2.3.2
Using ransack 2.3.2
Using ffi 1.13.1
Using sassc 2.4.0
Using sprockets 4.0.2
Using sprockets-rails 3.2.1
Using tilt 2.0.10
Using sassc-rails 2.1.2
Using activeadmin 2.7.0
Using execjs 2.7.0
Using autoprefixer-rails 9.7.6
Using bcrypt 3.1.13
Using bindex 0.8.1
Using msgpack 1.3.3
Using bootsnap 1.4.6
Using popper_js 1.16.0
Using bootstrap 4.5.0
Using bundler 2.2.20
Using byebug 11.1.3
Using coderay 1.1.3
Using orm_adapter 0.5.0
Using warden 1.2.8
Using devise 4.7.2
Using dotenv 2.7.6
Using dotenv-rails 2.7.6
Using multipart-post 2.1.1
Using faraday 1.0.1
Using hashie 4.1.0
Using jbuilder 2.10.0
Using jwt 2.2.1
Using kgio 2.11.4
Using libv8 7.3.492.27.1 (x86_64-linux)
Using rb-fsevent 0.10.4
Using rb-inotify 0.10.1
Using ruby_dep 1.5.0
Using listen 3.1.5
Using mini_racer 0.2.14
Using multi_json 1.14.1
Using multi_xml 0.6.0
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/tako_zhangyu/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2
/home/tako_zhangyu/.rbenv/versions/2.6.5/bin/ruby -I
/home/tako_zhangyu/.rbenv/versions/2.6.5/lib/ruby/2.6.0 -r
./siteconf20210620-23114-pervwr.rb extconf.rb
--with-ldflags\=-L/usr/local/opt/[email protected]/lib
--with-cppflags\=-I/usr/local/opt/[email protected]/include
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/tako_zhangyu/.rbenv/versions/2.6.5/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/tako_zhangyu/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/extensions/x86_64-linux/2.6.0/mysql2-0.5.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/home/tako_zhangyu/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3
for inspection.
Results logged to
/home/tako_zhangyu/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/extensions/x86_64-linux/2.6.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

立ち向かう

ひとまず以下のエラー文で検索をかけてみる

Make sure that `gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'`
succeeds before bundling.

以下のコマンドで解決というような記事がたくさんあったが、まずssh内にbrewがなかったので実行できず

ターミナル
$ brew info openssl

以下コマンドでbrewをインストール

ターミナル
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

PATHを2つ通す

ターミナル
$ echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /home/tako_zhangyu/.bash_profile
$ eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

Homebrewがインストールされているかを確認する意味を込めて以下を実行

ターミナル
$ brew -v
Homebrew 3.1.12

バージョンが表示されたのでOK

以下を実行

ターミナル
$ brew info openssl

[email protected]: stable 1.1.1k (bottled)

表示された。

別ルートの解決

ここからは多くの記事のように以下コマンドを打つなどの解決策があると思いますが

ターミナル
$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/[email protected]/lib --with-cppflags=-I/usr/local/opt/[email protected]/include"

なんやかんやで違うルートで解決したので記録します。


最初のエラー文の中にある以下の記述を実行してみる

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

結果的に有効だったのは以下コマンドのみ

ターミナル
$ sudo yum install mysql-devel

読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd
依存性の解決をしています

〜〜〜〜省略〜〜〜〜
インストール:
  mariadb-devel.x86_64 1:5.5.68-1.amzn2                                         

依存性関連をインストールしました:
  mariadb-libs.x86_64 1:5.5.68-1.amzn2                                          

完了しました!

bundle installを実行

ターミナル
$ bundle
~~~~省略~~~~
Bundle complete! 26 Gemfile dependencies, 109 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

できた