LoginSignup
0
0

bundle install mysql2のエラー【Window10】

Posted at

エラー状況

gemfileに書かれたmysql2をインストールしようとpowershellでbundle installを実行したところエラー発生。

エラー文

(少し試行錯誤した後のエラー文しか引っ張ってこれなかったので初期のエラー文とは違う可能性があります)

Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Installing mysql2 0.5.5 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/mysql2-0.5.5/ext/mysql2
C:/Ruby32-x64/bin/ruby.exe extconf.rb
--with-ldflags\=-L/usr/local/opt/openssl/lib
--with-cppflags\=-I/usr/local/opt/openssl/include
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_gc_mark_movable()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_enc_interned_str() in ruby.h... 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-mysqlclientlib
        --without-mysqlclientlib
C:/Ruby32-x64/lib/ruby/3.2.0/mkmf.rb:1083:in `block in find_library': undefined
method `split' for nil:NilClass (NoMethodError)

    paths = paths.flat_map {|path| path.split(File::PATH_SEPARATOR)}
                                       ^^^^^^
        from C:/Ruby32-x64/lib/ruby/3.2.0/mkmf.rb:1083:in `each'
        from C:/Ruby32-x64/lib/ruby/3.2.0/mkmf.rb:1083:in `flat_map'
        from C:/Ruby32-x64/lib/ruby/3.2.0/mkmf.rb:1083:in `find_library'
        from extconf.rb:131:in `<main>'

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

C:/Ruby32-x64/lib/ruby/gems/3.2.0/extensions/x64-mingw-ucrt/3.2.0/mysql2-0.5.5/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/mysql2-0.5.5 for inspection.
Results logged to
C:/Ruby32-x64/lib/ruby/gems/3.2.0/extensions/x64-mingw-ucrt/3.2.0/mysql2-0.5.5/gem_make.out

  C:/Ruby32-x64/lib/ruby/site_ruby/3.2.0/rubygems/ext/builder.rb:120:in `run'
C:/Ruby32-x64/lib/ruby/site_ruby/3.2.0/rubygems/ext/ext_conf_builder.rb:28:in
`build'
~~~
(中略)
~~~
`process_queue'
C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/bundler-2.4.22/lib/bundler/worker.rb:90:in
`block (2 levels) in create_threads'

An error occurred while installing mysql2 (0.5.5), and Bundler cannot continue.

In Gemfile:
  mysql2

自分が試した解決法

ここのGitHubのページを主に参照しました

  1. opensslのインストール
    mysql2のインストールにはopensslのライブラリが必要で、そのopensslのパス設定が問題になってるようなのですが、そもそもopensslがインストールされていなかったのでインストールしました。
    公式のここ(https://www.openssl.org/) から行うのが安全かと思いますが、これはソースファイルのみでWindows OS用にコンパイルするのは少し難しいらしく、自分はよくわからなかったので個人でopensslインストーラーを配布されてる?方のサイト(https://slproweb.com/products/Win32OpenSSL.html) を利用しました。
    opensslのインストールで検索すると、割とこのサイトのURLが貼られているので信用は高い方だとは思いますが自己責任でお願いします。

  2. 管理者権限でコマンドプロンプト
    別のサイトでシェルが悪さしている可能性がありますと書かれていたので一応powershellでなく管理者権限でコマンドプロンプトの方から行いました。

  3. ridkridk.cmd enableを実行
    ridkコマンドでRubyinstallerを開けることを確認、ridk.cmd enableを実行しました。

  4. gem install mysql2 --platform=ruby -- --with-mysql-dir=c:/(Ruby32-x64までのパス)/msys64/mingw64を実行
    ここでbundleでなくgemでまずインストールしました。
    Rubyまでのパスが分からないときは Win環境だと where.exe rubyC:\Ruby32-x64\bin\ruby.exeなどと出ると思うのでC:からRuby32-x64までのパスを入れると良いと思います。数字はRubyのverによって変わります。(withオプション以下は自分の場合だとc:/Ruby32-x64/msys64/mingw64

  5. bundle install 実行
    ここでbundlerを用いてmysql2を書いたgemfileをもつディレクトリでbundle installを実行してインストールに成功しました。

おそらく2,4は必要ないかなとは思いますが一応書いておきました。

補足

"ridk"はRubyInstaller Development Kitの略語であり、Windows上でRubyの拡張機能やgemを正しくビルドおよびインストールするための手順を簡素化するために使用されるもの。
ridk enableコマンドは、Windows上でRubyInstaller Development Kit (DevKit) を有効にするためのコマンド。
DevKitがシステムの環境変数などに適切な設定を行い、これにより、後続のgemのインストールや拡張機能のビルドがDevKitを利用して行われるようになるようです。

感想

環境構築しているとこでのエラーで解決までにかなり時間を要してしまった。
このエラーは有名っぽく結構数の記事が出てましたが、Win環境,bundlerでの日本語記事が少なかったので記事にしておきました。

参考記事

[Rails]初のbundle installでmysql2のエラーが出た時の解決策をまとめてみた
Cannot install mysql2 gem on Windows 10

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