一般在windows运行安装mysql2 gem包的时候,会遇到如下错误。
$ gem install mysql2
Building native extensions. This could take a while...
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.
G:/Ruby192/bin/ruby.exe extconf.rb
checking for rb_thread_blocking_region()... *** 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.
解决方法
1. 安装DevKit
DevKit 是windows平台下编译和使用本地C/C++扩展包的工具。它就是用来模拟Linux平台下的make, gcc, sh来进行编译。
注:这个方法目前仅支持通过RubyInstaller安装的Ruby,如果不是建议你重新安装。
。下载Devkit
。解压指定解压路径,路径中不能有空格。如C:\DevKit
。安装Devkit
>转到 Devkit目录
cd C:\DevKit
>初始化
C:\DevKit>ruby dk.rb init
[INFO] found RubyInstaller v1.9.2 at G:/Ruby192
Initialization complete! Please review and modify the auto-generated
'config.yml' file to ensure it contains the root directories to all
of the installed Rubies you want enhanced by the DevKit.
> ruby版本确认
C:\DevKit>ruby dk.rb review
Based upon the settings in the 'config.yml' file generated
from running 'ruby dk.rb init' and any of your customizations,
DevKit functionality will be injected into the following Rubies
when you run 'ruby dk.rb install'.
G:/Ruby192
>安装Devkit
C:\DevKit>ruby dk.rb install
[INFO] Installing 'G:/Ruby192/lib/ruby/1.9.1/rubygems/defaults/operating_system.
rb'
[INFO] Installing 'G:/Ruby192/lib/ruby/gems/1.9.1/doc/bundler-1.0.15/ri/Bundler/
Source/Rubygems/defaults/operating_system.rb'
[INFO] Installing 'G:/Ruby192/lib/ruby/site_ruby/devkit.rb'
2. 安装mysql (这个不详细说了)
安装好之后将mysql安装目录下的bin下的libmysql.dll复制到 ruby安装目录/lib下。
3.安装mysql2 gem包
请注意下面的引号。以及版本。如果rails 版本<3.1.0就安装mysql 0.2.6。如果rails 版本是3.1.*才需要安装mysql 0.3.*。
C:\DevKit>gem install mysql2 -v 0.2.6 -- '--with-mysql-dir="D:\Program Files\MySQL\MySQL Server 5.1"'
Successfully installed mysql2-0.2.6-x86-mingw32
1 gem installed
Installing ri documentation for mysql2-0.2.6-x86-mingw32...
Enclosing class/module 'mMysql2' for class Client not known
Installing RDoc documentation for mysql2-0.2.6-x86-mingw32...
Enclosing class/module 'mMysql2' for class Client not known
如果出现上面的信息,恭喜你安装成功了!
如果你在ubuntu上遇到这个问题,那很简单运行以下命令即可。
sudo apt-get install libmysql-ruby libmysqlclient-dev
还是linux上编写ruby程序方便啊!推荐在linux下编写ruby程序。