Error installing mysql2: Failed to build gem native extension
Asked Answered
S

33

533

I am having some problems when trying to install mysql2 gem for Rails. When I try to install it by running bundle install or gem install mysql2 it gives me the following error:

Error installing mysql2: ERROR: Failed to build gem native extension.

How can I fix this and successfully install mysql2?

Silden answered 31/8, 2010 at 10:54 Comment(1)
In my case ruby 2.1 no error was shown but it freezed for several minutes. Spacemonkey answer fixed it.Clinometer
S
979

On Ubuntu/Debian and other distributions using aptitude:

sudo apt-get install libmysql-ruby libmysqlclient-dev

Package libmysql-ruby has been phased out and replaced by ruby-mysql. This is where I found the solution.

If the above command doesn't work because libmysql-ruby cannot be found, the following should be sufficient:

sudo apt-get install libmysqlclient-dev

On Red Hat/CentOS and other distributions using yum:

sudo yum install mysql-devel

On Mac OS X with Homebrew:

brew install mysql
Silden answered 31/8, 2010 at 12:0 Comment(13)
i have the same issue on windows 7..any ideas? the command above didnt work for me..Westmorland
Hello, I'm using Ubuntu 12.04 with Ruby 1.9.3 and Rails 3.2.8 and in my case, installing libmysqlclient-dev was sufficient.Ney
If you are using Mac OSX: update to the latest version of XCode and install the Development Tools. I had the same mysql install issue (even when using the --with-mysql-config=/usr/local/mysql/bin/mysql_config )Annunciation
On centos, I also had to do this: yum install mysql mysql-serverIllomened
I must admit it works. I'm on Xubuntu 13.04 (but pure kubuntu-desktop) and ruby-2.0.0-p247, and after copy-paste-run the command I could gem install mysql2 successfully. I'm impressed. I had so much pain with that gem on Win XP, and managed to install it so easy on kubuntu. Why is it not as easy on Windows?Reasonable
Had the same problem, but brew install mysql wouldn't solve the issue. It still complained that some mysql.h file is missing. This answer solved it https://mcmap.net/q/74872/-mysql-h-is-missing-ruby-on-rails-osxLay
Did not need libmysql-ruby since I had installed Ruby via RVMMedico
In case you are trying to install GitLab, you'll need only libmysqlclient-dev.Young
For windows try the Ticked answer from #19014617Chancellor
I ran "brew install mysql", and I still getting the same error when running bundle install. Anybody can help me?Disinterested
"sudo apt-get install libmysqlclient-dev" worked for me. ThanksDesinence
For Windows, There is a libmysqlcient.dll/libmysqlclient-dev.dll file in Mysql/lib folder. Copy that file and paste into where you installed your ruby i.e. Ruby1.9/lib Hope this will workCrew
On Amazon Linux 2022 Preview (which is based on Fedora 34), I had to install "mysql-community-devel" package.Hoenack
P
67

I'm on a mac and use homebrew to install open source programs. I did have to install mac Dev tools in order to install homebrew, but after that it was a simple:

brew install mysql

to install mysql. I haven't had a mysql gem problem since.

Pulsatory answered 25/5, 2011 at 22:48 Comment(4)
I'm getting error even after doing this. Should I pass some options when I do gem i mysql2 ??Hoem
After running this I had no problems installing the mysql2 ruby gem on Mac OSX Lion.Brainbrainard
If this doesn't help, try brew link mysql --force.Marmalade
@ChuckBatson Perfect it worked! I had [email protected] installed so I just had to do brew link [email protected] --forceMacymad
T
42

here is a solution for the windows users, hope it helps!

Using MySQL with Rails 3 on Windows

  • Install railsinstaller -> www.railsinstaller.org (I installed it to c:\Rails)

  • Install MySQL (I used MySQL 5.5) -> dev.mysql.com/downloads/installer/

--- for mySQL installation ---

If you dont already have these two files installed you might need them to get your MySQL going

vcredist_x86.exe -> http://www.microsoft.com/download/en/details.aspx?id=5555 dotNetFx40_Full_x86_x64.exe -> http://www.microsoft.com/download/en/details.aspx?id=17718

Use default install Developer Machine

-MySQL Server Config-
port: 3306
windows service name: MySQL55
mysql root pass: root (you can change this later)
(username: root)
-MySQL Server Config-

--- for mySQL installation ---


--- Install the mysql2 Gem ---

Important: Do this with Git Bash Command Line(this was installed with railsinstaller) -> start/Git Bash

gem install mysql2 -- '--with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.5\lib" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.5\include"'

Now the gem should have installed correctly

Lastly copy the libmysql.dll file from
C:\Program Files\MySQL\MySQL Server 5.5\lib
to
C:\Rails\Ruby1.9.2\bin

--- Install the mysql2 Gem ---


You will now be able to use your Rails app with MySQL, if you are not sure how to create a Rails 3 app with MySQL read on...


--- Get a Rails 3 app going with MySQL ---

Open command prompt(not Git Bash) -> start/cmd
Navigate to your folder (c:\Sites)
Create new rails app

rails new world

Delete the file c:\Sites\world\public\index.html
Edit the file c:\Sites\world\config\routes.rb
add this line -> root :to => 'cities#index'

Open command prompt (generate views and controllers)

rails generate scaffold city ID:integer Name:string CountryCode:string District:string Population:integer



Edit the file c:\Sites\world\app\models\city.rb to look like this

class City < ActiveRecord::Base
 set_table_name "city"
end

Edit the file c:\Sites\world\config\database.yml to look like this

development:
adapter: mysql2
encoding: utf8
database: world
pool: 5
username: root
password: root
socket: /tmp/mysql.sock

add to gemfile

gem 'mysql2'

Open command prompt windows cmd, not Git Bash(run your app!)
Navigate to your app folder (c:\Sites\world)

rails s

Open your browser here -> http://localhost:3000

--- Get a Rails 3 app going with MySQL ---

Therapeutics answered 16/2, 2012 at 19:44 Comment(5)
Thanks, the problem was that i used widnows PowerShell and not normal command prompt. now it's working :)Frivolous
the host must be 127.0.0.1, and if the password is number, it must be put in quote ex '123456' otherwise we can type normally ex admin123Bassett
Using XAMPP: gem install mysql2 -- --with-mysql-dir=C:\xampp\mysqlRoselani
Look at path --with-mysql-include: gem install mysql2 -- --platform=ruby --with-mysql-lib="d:\mariadb\lib" --with-mysql-include="d:\mariadb\include\mysql"Nancienancy
@Roselani worked great for me but i am using GitBash and needed -- --with-mysql-dir="C:\xampp\mysql" without the quotes my `` were stripped. Thank youIncidental
E
39

For MacOS Mojave:

gem install mysql2 -v '0.5.2' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include

Eisk answered 11/8, 2019 at 13:48 Comment(3)
as well as for MacOS CatalinaGermicide
Worked for the issue: clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [mysql2.bundle] Error 1. Thanks!Orphanage
I was stuck with this for hours. Only this solution worked.Upgrowth
F
22

On Ubuntu(18.04)

i was able to solve this issue by running the following:

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

reference

Fatherinlaw answered 20/1, 2020 at 10:47 Comment(1)
the thing that worked for me on Ubuntu 20.04Mckinney
O
20

Another way for MacOS users

If you used "brew" to install mysql:

gem install mysql2 -v 'x.x.x' -- --with-mysql-config=/usr/local/Cellar/mysql/y.y.y/bin/mysql_config

x.x.x = version of the mysql2 gem you want to install
y.y.y = the version of mysql you have installed ls /usr/local/Cellar/mysql to find it.

Originally answered 30/9, 2015 at 15:15 Comment(1)
Tried ~10 answers from all sort of questions asked here... You solved it. Thanks :)Sniperscope
E
19

I have several computers, 32 and 64 bits processor, they run on Ubuntu Linux, Maverick (10.10) release.

I had the same problem, and for me, the sudo apt-get install libmysql-ruby libmysqlclient-dev did the job!!!

Eremite answered 23/1, 2011 at 7:44 Comment(0)
F
7

Have you tried using

 gem install mysql -- --with-mysql-lib=/usr/lib/mysql/lib 

to specify the location of thebase directory as well as the path to the MySQL libraries that are necessary to complete the gem installation?

Sources: MySQL Gem Install ERROR: Failed to build gem native extension MySQL Forums :: Ruby :: Help needed with installing MySQL binding for Ruby

Flicker answered 31/8, 2010 at 11:23 Comment(1)
Worked great. I had to use a different path, which I found by calling which mysql and then finding the lib directory from there.Klaus
M
7

If you are using yum try:

sudo yum install mysql-devel
Molecular answered 20/10, 2011 at 19:57 Comment(1)
It worked for me in an Amazon EC2 Micro Instance, CentOS based.Ossieossietzky
C
6

This solved my problem once in Windows:

subst X: "C:\Program files\MySQL\MySQL Server 5.5" 
gem install mysql2 -v 0.x.x --platform=ruby -- --with-mysql-dir=X: --with-mysql-lib=X:\lib\opt 
subst X: /D
Convexoconvex answered 9/8, 2011 at 1:40 Comment(3)
I was about to post the source but I see user Ritesh Kumar has already done so. So his merit, not mine.Convexoconvex
If your lib does not have opt folder: gem install mysql2 --platform=ruby -- --with-mysql-dir=X: --without-opt-dirCheri
This worked for me (a whole decade later!). This is the full install command that worked for me gem install mysql2 -v 0.3.11 --platform=ruby -- --with-mysql-lib=X: --without-opt-dir. And using MySQL Server 8.0Buote
V
6

On Debian Stretch the package that worked for me was default-libmysqlclient-dev

sudo apt-get update && apt-get install -y default-libmysqlclient-dev

Vogul answered 30/5, 2018 at 19:57 Comment(0)
K
5

In my case this helped:

$ export LDFLAGS="-L/usr/local/opt/openssl/lib"
$ export CPPFLAGS="-I/usr/local/opt/openssl/include"

Then:

gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/' -- --with-cppflags=-I/usr/local/opt/openssl/include --with-ldflags=-L/usr/local/opt/openssl/lib

Result:

Building native extensions with: '--with-cppflags=-I/usr/local/opt/openssl/include --with-ldflags=-L/usr/local/opt/openssl/lib'
This could take a while...
Successfully installed mysql2-0.5.2
Parsing documentation for mysql2-0.5.2
Installing ri documentation for mysql2-0.5.2
Done installing documentation for mysql2 after 0 seconds
1 gem installed

See this post (WARNING: Japanese language inside).

Kerseymere answered 23/5, 2019 at 12:24 Comment(0)
C
4

I had this issue on Windows 7. This is apparently an incompatibility issue and the solution is as follows:

Download the libmySQL.dll file from an older InstantRails installer. It is available from the InstantRails GitHub repo.

Next, copy that file into your Ruby/bin folder.

Now, you are good to go ;)

Contemporize answered 20/2, 2011 at 9:3 Comment(1)
Hrm. :( I tried this but still get there error when trying to run bundle install.Ting
W
4

If you are still having trouble….

Try installing

   sudo apt-get install ruby1.9.1-dev
Winburn answered 5/8, 2013 at 11:32 Comment(0)
S
4

For M1 with Ventura:

gem install mysql2 -v '0.5.3' -- --with-mysql-config=$(brew --prefix mysql)/bin/mysql_config --with-ldflags="-L$(brew --prefix zstd)/lib -L$(brew --prefix openssl)/lib" --with-cppflags=-I$(brew --prefix openssl)/include

Thanks to https://gist.github.com/fernandoaleman/385aad12a18fe50cf5fd1e988e76fd63

Senary answered 16/6, 2023 at 17:7 Comment(1)
thanks - this still works for M3 🙂 (as long as you install mysql first)Horvath
L
3

I got this error too. Solved by installing development packages. I'm using arch and it was:

sudo pacman -S base-devel

which installed:

m4, autoconf, automake, bison, fakeroot, flex, libmpc, ppl, cloog-ppl, elfutils, gcc,
libtool, make, patch, pkg-config

but I think it actually needed make and gcc. Error output said (on my machine, among other):

"You have to install development tools first."

So it was an obvious decision and it helped.

Limner answered 16/10, 2010 at 12:40 Comment(0)
N
3

You have to Install some dependencies

sudo apt-get install libmysql-ruby libmysqlclient-dev
Northing answered 28/12, 2015 at 13:29 Comment(0)
C
3

I can see most of the people have found the solution to this problem, this is mostly coused but not limited to missing packages, this happened to me after I have purged mysql and reinstalled it. I had to run this command in order to fix my problem:

sudo apt-get install libmysqlclient-dev

This command helped me fix my problem

Converter answered 13/5, 2021 at 9:55 Comment(0)
F
2

I was running into this error on my mac and found that I needed to upgrade from mysql 32bit to mysql 64 bit to get this error to go away. I was running OSX 10.6 on an intel macbook pro with ruby 1.9.2 and rails3.0.0

I also needed to install xcode in order to get unix utilities like "make" that are required to compile the gem.

once this was done I was able to run gem install mysql and gem install mysql2 without error.

Foam answered 14/9, 2010 at 15:59 Comment(0)
I
1

I got the gem built on Mac OS X 10.6.6 by 1) Ensuring the Developer tools package is installed 2) Downloading the current MySQL package (5.5.8 in my case) from Source 3) Installing the cmake tool from cmake.org 4) Following the instructions in section 2.11 of INSTALL-SOURCE from the mysql distribution files 5) sudo gem install mysql2 -- --srcdir=/usr/local/mysql/include The gem built successfully, but there are two errors in the documentation that rdoc and ri complain about. But now when I try to require 'mysql2' I get a

LoadError: no such file to load -- mysql2/mysql2

I was hoping the error I would get was that the libmysqlclient.16.dylib couldn't be found because we figured that out in another post (search for install_name_tool).

My $PATH has /usr/local/mysql in it (that's where my source and built files are located), so I'm a little stumped. If anyone has any thoughts, I'll check back after a few hours of sleep.

Incontestable answered 13/1, 2011 at 8:22 Comment(0)
H
1

For windows user: You set the lib and include path of your mysql, for instance, if youre using xampp you can have like this:

gem install mysql2 -- '--with-mysql-lib="C:\xampp\mysql\lib" --withmysql-include="C:\xampp\mysql\include"'
Herring answered 13/8, 2015 at 5:39 Comment(0)
T
1

Solution only works on Mac OS X

If you've installed MySQL with homebrew, what worked for me was uninstalling MySQL, and installing MySQL Community Edition via the MySQL website (https://www.mysql.com/).

After installed, just re-enter the command to gem install mysql2 or if necessary, sudo gem install mysql2, if you are getting permission denied problems.

Twitty answered 2/12, 2016 at 1:51 Comment(0)
D
1

According to https://github.com/brianmario/mysql2/issues/1175 , I fixed it by

gem install mysql2 -- \
 --with-mysql-lib=/usr/local/Cellar/mysql/8.0.26/lib \
 --with-mysql-dir=/usr/local/Cellar/mysql/8.0.26 \
 --with-mysql-config=/usr/local/Cellar/mysql/8.0.26/bin/mysql_config \
 --with-mysql-include=/usr/local/Cellar/mysql/8.0.26/include
Diphyodont answered 16/11, 2021 at 8:9 Comment(0)
R
0

If still getting error then follow the steps of mysql2 gem installation on Rails 3 on -

http://rorguide.blogspot.com/2011/03/installing-mysql2-gem-on-ruby-192-and.html

where most of the user were able to install mysql2 gem.

Raddy answered 30/5, 2011 at 18:45 Comment(0)
B
0

After you get the mysql-dev issues corrected, you may need to remove the bad mysql2 install. Look carefully at the messages after $ bundle install. You may need to

rm -rf vendor/cache/
rm -rf ./Zentest

This will clear out the bad mysql2 installation so that a final $ bundle install can create a good one.

Benis answered 13/12, 2011 at 2:49 Comment(0)
W
0

download the right version of mysqllib.dll then copy it to ruby bin really works for me. Follow this link plases mysql2 gem compiled for wrong mysql client library

Wenda answered 3/10, 2012 at 9:36 Comment(0)
G
0

Got the "You have to install development tools first." error when trying to install the mysql2 gem after upgrading to Mac OS X Mountain Lion. Apparently doing this upgrade removes the command line compilers.

To fix:

  • I uninstalled my very old version of Xcode (ran the uninstall script in /Developer/Library). Then deleted the /Developer directory.
  • Went to the AppStore and downloaded Xcode.
  • Launched Xcode and went into the Preferences -> Downloads, and installed the command line tools.
Guttate answered 12/2, 2013 at 2:10 Comment(0)
T
0

You are getting this problem because you have not install MySql. Before install mysql2 gem. Install MySQL. After that mysql2 gem will install.

Titivate answered 18/3, 2014 at 21:7 Comment(0)
S
0

I just wanted to add this answer specifically for Mac Users.

My server was running perfectly fine until I updated my xcode. The while starting my rails server the error was shown like this

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /Users/user/.rvm/rubies/ruby-1.9.3-p448/bin/ruby extconf.rb --with-mysql-
    checking for rb_thread_blocking_region()... /Users/user/.rvm/rubies/ruby-1.9.3-  
    p448/lib/ruby/1.9.1/mkmf.rb:381:in `try_do': The compiler failed to generate an    
    executable file. (RuntimeError)

And there was suggestion to install mysql2 gem at the end of the error message. So when i tried installing it I got the error as above mentioned in this question. The error I got is as follows

ERROR:  Error installing mysql2:
ERROR: Failed to build gem native extension.

So as suggested in this post I tried 'brew install mysql' and that quitted saying that mysql version so and so already installed. But there was warning before it saying

Warning: You have not agreed to the Xcode license.
Builds will fail! Agree to the license by opening Xcode.app or running:
xcodebuild -license

Then I tried this sudo xcodebuild -license and type 'agree' at the end. You have to be root to agree to the license.

After this, I again tried bundle install and then everything is working fine as normal. [ Even due to this xcode updation, I had problem with my tower also.]

Schedule answered 1/10, 2014 at 7:29 Comment(0)
P
0

libmysql-ruby has been phased out and replaced. New command:

 sudo apt-get install ruby-mysql libmysqlclient-dev
Perch answered 12/3, 2018 at 10:41 Comment(0)
I
0

Under ubuntu 20.04 LTS it's the only solution that have been working for me:

sudo apt-get install ruby-mysql2
Ilion answered 28/10, 2021 at 13:14 Comment(0)
C
0

An updated solution for Windows users. I am running

Windows 10

MySql 8.0.30

Ruby 3.1.2

My Ruby version is the one from rubyinstaller.org with the MYSYS devkit included

I downloaded a MariaDB C connector library msi installer file v3.1.17, ran it and it installed the relevant files in a directory in Program Files. I copied that directory to another location, renamed it so that it didn't have any spaces and ran the command:

gem install mysql2 --platform=ruby -- --with-mysql-dir="c:/my-mariadb-connector-path-with-no-spaces"

This gave me a functioning gem.

This is a specific version of the library and not the most recent. Finding your way around the download options on the MariaDB website is a bit confusing, so here is the direct download link.

https://dlm.mariadb.com/2319542/Connectors/c/connector-c-3.1.17/mariadb-connector-c-3.1.17-win64.msi

Before I got to this point I'd tried several things which hadn't worked. I've included them below as background information, but if all you're interested in is a working solution then you can stop reading now.

A few sources suggest that if you have the MYSYS devkit installed you can build the gem with the following command:

ridk exec gem install mysql2 --platform=ruby -- --use-system-libraries

This did not compile for me.

You may also have seen a recommendation to download an archived version - 6.1 - of the MySQl connector library. I tried this, the gem compiled, but when I tried to start my app I got an error message: "Incorrect MySQL client library version! This gem was compiled for 6.1.11 but the client library is 10.5.5."

In MySQL V8 and above the C library is no longer an optional extra but is included in the main installation. I found the relevant files, copied them to a no spaces directory path and tried the usual command. Again the gem compiled but the app wouldn't start. This time the error message said that "This gem was compiled for 8.0.30 but the client library is 10.5.5"

I also tried the latest version of of the MariaDB library. With that the error message said that the gem was compiled for 10.6.8. You need the specific version described above, and yes, it really is a MariaDB library you need even if you're using "Original" MySql as distributed by the Oracle Corporation

Cordie answered 24/8, 2022 at 11:44 Comment(0)
M
0
  1. Download the connector C file from [here][1]. (https://downloads.mysql.com/archives/c-c/)

  2. Extract the file to a location, like C:\ror\mysqlC\mysql-connector-c-6.1.11-winx64\lib

  3. Use this template

gem install mysql2 -- '--with-mysql-lib="$lib_location" --with-mysql-include="$include_location"'

eg: My installed location is lib_location -> C:\ror\mysqlC\mysql-connector-c-6.1.11-winx64\lib

gem install mysql2 -- '--with-mysql-lib="C:\ror\mysqlC\mysql-connector-c-6.1.11-winx64\lib" --with-mysql-include="C:\ror\mysqlC\mysql-connector-c-6.1.11-winx64\include"'

Monohydric answered 12/10, 2022 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.