Ruby gem mysql2 install failing

夙愿已清 提交于 2019-11-26 18:22:53

You have to have 64-bit MySQL installed on your machine, plus the build tools you get when you install xcode.

tunelko

Ubuntu:

sudo apt-get install libmysqlclient-dev  #(mysql development headers)
sudo gem install mysql2 -- --with-mysql-dir=/etc/mysql/

That's it!

Result:

Building native extensions. This could take a while...
Successfully installed mysql2-0.2.6
1 gem installed
Installing ri documentation for mysql2-0.2.6...
Enclosing class/module 'mMysql2' for class Result not known
Enclosing class/module 'mMysql2' for class Client not known
Installing RDoc documentation for mysql2-0.2.6...
Enclosing class/module 'mMysql2' for class Result not known
Enclosing class/module 'mMysql2' for class Client not known

You need the MySQL development headers for mysql2 to properly compile. This is needed, because much of the functionality is written in C and subsequently linked against MySQL.

A solution is to either point the gem installer to your mysql install folder explicitly like so

gem install mysql2 -- --with-mysql-dir=/usr/local/mysql
# or where ever you installed your mysql server to

or by installing a mysql server to a known location (e.g. by using homebrew.) and then installing the gem

# install the mysql server locally
brew install mysql
# install the gem
gem install mysql2

Both approaches require you to have XCode installed to have the required GCC compiler.

With Brew and MySQL installed, I used the following to install the mysql2 gem

gem install mysql2 -- --with-mysql-config=/usr/local/Cellar/mysql/5.5.10/bin/mysql_config

If you're using Bundler, you can tell bundler about this with command:

bundle config build.mysql2 --with-mysql-config=/usr/local//Cellar/mysql/5.5.10/bin/mysql_config

As documented here: http://gembundler.com/man/bundle-config.1.html

on Mac OSX Moutain Lion, below command worked for me:

gem install mysql2 -- --srcdir=/usr/local/mysql/include

Ubuntu 15.04:

sudo apt-get install libmysqlclient-dev
sudo gem install mysql2

Ubuntu-16.04:

sudo apt-get install ruby-mysql2
sudo gem install mysql2

Output:

Building native extensions.  This could take a while...
Successfully installed mysql2-0.3.19
Parsing documentation for mysql2-0.3.19
Installing ri documentation for mysql2-0.3.19
Done installing documentation for mysql2 after 0 seconds
1 gem installed

You might want to check this thread at the MySQL web site: http://forums.mysql.com/read.php?116,178217,178217, particularly the answer by Scott Derrick: http://forums.mysql.com/read.php?116,178217,189357#msg-189357

To be more specific, try using

sudo gem install mysql2 -- --with-mysql-dir=/usr/local/mysql

Hope that helps.

I dug through 2 days worth of the internet and Stack Overflow, and it wasn't until I found this link and then starting working through this ticket for mysql2 that I actually resolved the issue.

With my setup (as explained in the ticket), the compiler switches of -Wno-null-conversion -Wno-unused-private-field would break and give me an error that wasn't exactly correct, which was:

mysql.h is missing. please check your installation of mysql and try again

I guess that if you type rvm use ruby-2.1.0@rails4.0 --create in you directory then bundle install it works.

I solved this by being specific on where are the directories, no need to reinstall anything, with brew or macports or whatever, just by specifying where are the things (only one thing: the ruby on my mac was installed with rvm, I'm not using the default one that comes with os x):

adding to your gem install mysql2 the flags below

  • --srcdir="..." - the includes
  • --with-mysql-dir="..." - the mysql directory
  • --with-mysql-config="..." - the mysql_config file

like this gem install mysql2 --srcdir=/usr/local/mysql/include/ --with-mysql-dir=/usr/local/mysql --with-mysql-config=/usr/local/mysql/bin/mysql_config

In Centos 6.x (7 should work) with SCL (software collection) rh-mysql:

scl enable rh-mysql56 bash
gem install mysql2 -v '0.4.5' -- --with-mysql-include=/opt/rh/rh-mysql56/root/usr/include --with-mysql-lib=/opt/rh/rh-mysql56/root/usr/lib64
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!