Windows7でのgem install mysql2エラーへの対処
Windows環境へRuby on Railsをセットアップする際にmysql2のインストールで
ハマったので対処方法を後世のために記しておきます。
なお、以下の環境における内容になります。
[環境情報] OS:Windows 7 Ruby:2.0.0p353 Rails:4.0.2 MySQL:5.6.15
Ruby、Railsのインストールまでは順調に進むのですがMySQLのライブラリである
mysql2をインストールしようとすると以下のようなエラーが発生します。
gem install mysql2 ERROR: Error installing mysql2: ERROR: Failed to build gem native extension. (中略) 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-mysql-dir」オプションを指定してMySQLサーバーなどをインストールしている
ディレクトリを指定します。
しかし、今度は別のエラーが発生します。
gem install mysql2 --platform=ruby '--with-mysql-dir="(MySQLのディレクトリ)"' ERROR: Error installing mysql2: ERROR: Failed to build gem native extension. (中略) [RUBY_HOME]\lib\ruby\gems\2.0.0\gems\mysql2-0.3.15\ext\mysql2/result.c:530: undefined reference to `mysql_error@4' [RUBY_HOME]\lib\ruby\gems\2.0.0\gems\mysql2-0.3.15\ext\mysql2/result.c:547: undefined reference to `mysql_fetch_fields@4' result.o: In function `rb_mysql_result_free_result': [RUBY_HOME]\lib\ruby\gems\2.0.0\gems\mysql2-0.3.15\ext\mysql2/result.c:76: undefined reference to `mysql_free_result@4' result.o: In function `rb_mysql_result_each': [RUBY_HOME]\lib\ruby\gems\2.0.0\gems\mysql2-0.3.15\ext\mysql2/result.c:499: undefined reference to `mysql_num_rows@4' result.o: In function `rb_mysql_result_free_result': [RUBY_HOME]\lib\ruby\gems\2.0.0\gems\mysql2-0.3.15\ext\mysql2/result.c:76: undefined reference to `mysql_free_result@4' collect2.exe: error: ld returned 1 exit status make: *** [mysql2.so] Error 1 (後略) ※[RUBY_HOME]はRubyがインストールされているディレクトリを表します
途方にくれましたが、以下の手順で解決できました。
「--with-mysql-dir」全体を「'」、ライブラリパスを「"」で囲うのをお忘れずに。
(1)以下のzipファイルを取得し、任意の場所に解凍する。
http://cdn.mysql.com/Downloads/Connector-C/mysql-connector-c-noinstall-6.0.2-win32.zip
(2)mysql-connector-c-noinstall-6.0.2-win32\lib\libmysql.dllを[RUBY_HOME]\binにコピーする。
(3)(1)で解凍したライブラリのディレクトリを指定してmysql2のインストールを実行。
gem install mysql2 --platform=ruby -- '--with-mysql-dir="(解凍ディレクトリ)\mysql-connector-c-noinstall-6.0.2-win32"'
参考にしたのは以下のサイトになります、感謝。
http://www.oiax.jp/rails/zakkan/ruby_2_0_mysql_windows.html