Error when trying to install app with mysql2 gem - mysql

Im trying to install an open source rails 3.2.21 application that uses the mysql2 gem, but when i try and run the bundle commant I get the following error:
Fetching: mysql2-0.3.18.gem (100%)
Building native extensions. This could take a while...
p
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.
/Users/my_username/.rvm/rubies/ruby-2.1.2/bin/ruby -r ./siteconf20150614-72129-orqsb7.rb extconf.rb
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
-----
Using mysql_config at /usr/local/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for mysqld_error.h... yes
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/Cellar/mysql/5.6.25/lib
-----
creating Makefile
make "DESTDIR=" clean
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
linking shared-object mysql2/mysql2.bundle
ld: warning: directory not found for option '-L/Users/travis/.sm/pkg/active/lib'
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1
make failed, exit code 2
Gem files will remain installed in /Users/my_username/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/mysql2-0.3.18 for inspection.
Results logged to /Users/my_username/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/extensions/x86
I tried uninstalling every version of mysql I installed via homebrew and reinstalling them, like so:
brew uninstall --force mysql && brew install mysql
Then running:
sudo gem install mysql2
As suggested by a number of similar questions asked on here, but it still results in the same error as above.
Please could someone offer guidance on how to get this up and running?

For anybody still experiencing the issue:
When you install openssl via brew, you should get the following message:
Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries
Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:
LDFLAGS: -L/usr/local/opt/openssl/lib
CPPFLAGS: -I/usr/local/opt/openssl/include
PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig
You can set these build flags (for the local application) by running the following:
bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"
This worked for me.
See bundler's documentation for more information.

The error log says:
ld: library not found for -lssl
So, you need to install libssl:
brew install openssl
As it was pointed out in comments, there might be a need to export the path to the library.
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/

Try this:
gem install mysql2 -v '0.5.2' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
(Update version as appropriate)

The solution for me was to install the Xcode Command Line Tools.
I had recently updated Xcode through the Mac App Store, and every time I do that, I've found that I have to reinstall the Command Line Tools again.
xcode-select --install

Based on the solution here
brew install openssl
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/
solved the problem.

After Homebrew update (openssl#1.1) there is a new path for libs, so may use:
bundle config build.mysql2 --with-opt-dir=$(brew --prefix openssl)
bundle install
It will fix ld: library not found for -lssl error

Thanks #mudasobwa for pointing me in the right direction. It turns out the error was caused by an unlinked openssl file, so running:
brew reinstall openssl && brew link openssl --force
Solved the problem. I found the solution here: OpenSSL, RVM, Brew, conflicting error

On MacBook air M1(macOS) it worked for me.
Install zstd
brew install zstd
Install mysql2
gem install mysql2 -v '0.5.3' -- --with-opt-dir=$(brew --prefix openssl) --with-ldflags=-L/opt/homebrew/Cellar/zstd/1.5.0/lib

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
From here:
https://gorails.com/setup/osx/10.14-mojave

The combination of commands solved it me. I am on Mojave.
brew reinstall openssl && brew link openssl --force
and then
gem install mysql2 -v '0.4.10' -- \
--with-ldflags=-L/usr/local/opt/openssl/lib \
--with-cppflags=-I/usr/local/opt/openssl/include

Steps for me on Monterey M1 Mac
brew install openssl#3
brew install zstd
gem install mysql2 -v '0.5.3' -- --with-opt-dir=$(brew --prefix openssl) --with-ldflags=-L/opt/homebrew/Cellar/zstd/1.5.0/lib
bundle config --local build.mysql2 "--with-opt-dir=$(brew --prefix openssl) --with-ldflags=-L/opt/homebrew/Cellar/zstd/1.5.0/lib"
bundle install

Seems that you miss the main files needed to build mysql2 gem
sudo apt-get install libsqlite3-dev libmysqlclient-dev -y
libsqlite3-dev is not mandatory but install it since it's the default rails DB.

Mac Catalina using Homebrew fix:
gem install mysql2 -- --with-opt-dir="$(brew --prefix openssl)"

the following command works for my Mac os 12.1 MacOs Monterey
gem install mysql2 -v '0.5.3' -- \
--with-mysql-lib=/opt/homebrew/Cellar/mysql/8.0.28/lib \
--with-mysql-dir=/opt/homebrew/Cellar/mysql/8.0.28 \
--with-mysql-config=/opt/homebrew/Cellar/mysql/8.0.28/bin/mysql_config \
--with-mysql-include=/opt/homebrew/Cellar/mysql/8.0.28/include
Please refer this link for more details
https://github.com/brianmario/mysql2/issues/1175

This finally worked for me on macOS Monterey 12.3 (M1 Pro):
gem install mysql2 -- --with-mysql-dir=/opt/homebrew/Cellar/mysql/8.0.28_1
Make sure you read the installation instructions. Notable points for me were:
Make sure MySQL is installed (brew install mysql)
Make sure XCode select tools are installed (xcode-select --install)
Set the with-mysql-dir option to wherever mysql was installed (check with brew info mysql)

Combining the answers given by Aleksei Matiushkin and Alexey Mozorov fixed the problem for me.
But I also updated the openssl before adding the path.
Thanks!

I've been coding with mysql2 gem for years and have encountered with this issue time to time.
Today I found that this magic option -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include no longer worked on my Mac. Indeed, it looks like the default location where brew installs openssl has changed:
$ brew reinstall openssl
...
If you need to have openssl#3 first in your PATH, run:
echo 'export PATH="/opt/homebrew/opt/openssl#3/bin:$PATH"' >> ~/.zshrc
For compilers to find openssl#3 you may need to set:
export LDFLAGS="-L/opt/homebrew/opt/openssl#3/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl#3/include"
So following the message, I had to make a few changes to the command and got it work finally:
$ gem install mysql2 -v '0.5.3' -- --with-ldflags=-L/opt/homebrew/opt/openssl#3/lib --with-cppflags=-I/opt/homebrew/opt/openssl#3/include
Hope this will help someone!

This helped me on MacOs 12.6 (Monterey)
brew install mysql openssl
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
gem install mysql2 -v '0.5.3' \
--source 'https://rubygems.org/' -- \
--with-cppflags=-I/usr/local/opt/openssl/include \
--with-ldflags=-L/usr/local/opt/openssl/lib
bundle install
Thanks https://github.com/brianmario/mysql2/issues/1175#issuecomment-1069721834

I found that I had to use --with-opt-dir=/usr/local/opt.
Specifically, I added the following to my ~/.bundle/config file:
BUNDLE_BUILD__MYSQL2: "--with-opt-dir=/usr/local/opt"

Related

Rails & MySQL on Apple Silicon

has anyone been able to get Rails running with the MySQL via the mysql2 gem on Apple Silicon? I'm working with Ruby 2.5.3 and Rails 5.2.3 but would love to hear of any successes with any versions. Currently I am stuck with the mysql2 gem install failing on:
linking shared-object mysql2/mysql2.bundle
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1
Thank you!
I've had success with just the following:
rbenv exec gem install mysql2 -- \
--with-mysql-lib=/opt/homebrew/Cellar/mysql/8.0.25_1/lib \
--with-mysql-dir=/opt/homebrew/Cellar/mysql/8.0.25_1 \
--with-mysql-config=/opt/homebrew/Cellar/mysql/8.0.25_1/bin/mysql_config \
--with-mysql-include=/opt/homebrew/Cellar/mysql/8.0.25_1/include
(Note that you may need to change the version number in those paths)
Notably, this worked with the most recent version of mysql and does not require any Intel versions or using an emulated version of HomeBrew (e.g. ibrew).
Configure Bundler to use this build configuration automatically:
You may also want to set this configuration as your default for mysql2. This way anytime bundler has to re-install mysql2 (on this project or any other project on the same computer), it will automatically use this configuration. You can do that with the following:
bundle config set --global build.mysql2 \
--with-mysql-lib=/opt/homebrew/Cellar/mysql/8.0.25_1/lib \
--with-mysql-dir=/opt/homebrew/Cellar/mysql/8.0.25_1 \
--with-mysql-config=/opt/homebrew/Cellar/mysql/8.0.25_1/bin/mysql_config \
--with-mysql-include=/opt/homebrew/Cellar/mysql/8.0.25_1/include
Based on #matthias-winkelmann's answer, use brew to find the correct version of mysql for you:
gem install mysql2 -v '0.5.3' -- \
--with-mysql-lib=$(brew --prefix mysql)/lib \
--with-mysql-dir=$(brew --prefix mysql) \
--with-mysql-config=$(brew --prefix mysql)/bin/mysql_config \
--with-mysql-include=$(brew --prefix mysql)/include
Do notice the specific version of the mysql2 gem I'm referring to here.
I am using Montery12.1 with M1-14inc
Install env with brew
brew install openssl
brew install zstd
export env data
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
export LIBRARY_PATH=$LIBRARY_PATH:$(brew --prefix zstd)/lib/
export LIBRARY_PATH=$LIBRARY_PATH:$(brew --prefix openssl#1.1)/lib/
Install mysql
gem install mysql2 -v '0.5.3' \
--source 'https://rubygems.org/' -- \
--with-cppflags=-I/usr/local/opt/openssl/include \
--with-ldflags=-L/usr/local/opt/openssl/lib
I got error like that
I have tried with, it's work fine:
gem install mysql2 -v 0.5.3 -- --srcdir=/usr/local/mysql/include
UPDATE
I found a simple and fast solution
arch -x86_64 gem install mysql2 -v 0.5.3 -- --srcdir=/usr/local/mysql/include
Refs: https://stackoverflow.com/a/71006010/11049888
After many attempts and much searching I have a working answer. First, you have to have the two brews installed on your M1 Mac- homebrew under arm and homebrew (ibrew) under emulated x86. See https://soffes.blog/homebrew-on-apple-silicon for this. Then, I was able to install mysql#5.7 under emulation, as well as openssl. This is needed because mysql2 is compiling under emulation, it seems, and is referencing those libraries.
ibrew install mysql#5.7
ibrew install openssl
I then needed to add mysql and its libraries to my PATH.
export PATH="/opt/homebrew/bin:/usr/local/bin:/opt/homebrew/opt/mysql:/opt/homebrew/opt/mysql/lib:/usr/local/Cellar//mysql#5.7//5.7.34:/usr/local/Cellar//mysql#5.7//5.7.34/lib:/usr/local/Cellar//mysql#5.7//5.7.34/bin:$PATH"
You might need to tweak this based on version numbers - and when you have it working put it in your ~/.zshrc or other shell initializer file.
Then I was able to install the mysql2 gem with these flags:
gem install mysql2 -V -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include --with-opt-dir="$(ibrew --prefix openssl)"
I'm not a C coder so I don't totally get the details of how mysql2 is linking up to the mysql libraries. I would prefer to use the available mysql under ARM, expecting it must run faster, but not sure how to get it to link up.

Cannot Install "mysql gem" on MacOS Sierra

I am trying to install the mysql2 gem for one of my rails projects but I am getting an error. I run this command:
gem install mysql2
The error says:
ld: library not found for -lssl
However when I run:
brew install openssl
I get (which proves that I have the -lssl library):
Error: openssl-1.0.2f already installed
To install this version, first `brew unlink openssl`
And alternatively when I run (which proves I have mysql on my machine):
brew install mysql
I get:
Error: mysql-5.7.10 already installed
To install this version, first `brew unlink mysql`
Very lost and would love to solve this, thank you in advance. I am running brew version 1.0.4.
I am having the same problem on Mac OS Mojave.
The following worked for me. First – very important – I ran xcode and discovered it needed to install some crap again. Then I did
brew reinstall openssl
gem install mysql2 -v '0.4.5' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
bundle install
The second line is based on the answer here.
I solved the same issue on my MacOS Sierra by refreshing gems reference:
gem source -r https://rubygems.org/
gem source -a https://rubygems.org/

Rails 3.2.3 MySQL 5.5.1 Mysql2 gem install fails with ld incompatibilities

I have a clean build system as follows
Ubuntu 11.04
Rvm 1.13.5
Ruby 1.9.3p194
Rails 3.2.3
Gem 1.8.24
MySql 5.5.24-1 (installed in /usr/local)
I am trying to install the mysql2 gem (0.3.11) and getting the following problem:
pal#smurf01:~$ sudo gem install mysql2
[sudo] password for pal:
Building native extensions. This could take a while...
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.
/usr/local/rvm/rubies/ruby-1.9.3-p194/bin/ruby extconf.rb
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for mysql.h... yes
checking for errmsg.h... yes
checking for mysqld_error.h... yes
creating Makefile
make
compiling client.c
client.c: In function 'rb_raise_mysql2_error':
client.c:98:3: warning: ISO C90 forbids mixed declarations and code
client.c: In function 'rb_mysql_client_socket':
client.c:590:3: warning: ISO C90 forbids mixed declarations and code
compiling mysql2_ext.c
compiling result.c
linking shared-object mysql2/mysql2.so
/usr/bin/ld: skipping incompatible /usr/local/mysql/lib/libmysqlclient_r.so when searching for -lmysqlclient_r
/usr/bin/ld: skipping incompatible /usr/local/mysql/lib/libmysqlclient_r.a when searching for -lmysqlclient_r
/usr/bin/ld: cannot find -lmysqlclient_r
collect2: ld returned 1 exit status
make: *** [mysql2.so] Error 1
Gem files will remain installed in /usr/local/rvm/gems/ruby-1.9.3-p194/gems/mysql2-0.3.11 for inspection.
Results logged to /usr/local/rvm/gems/ruby-1.9.3-p194/gems/mysql2-0.3.11/ext/mysql2/gem_make.out
pal#smurf01:~$
First question: Has anyone got Rails mysql2 running with MySql 5.5.1 on Ubuntu? I have it running on another system with 5.1. The gem description is vague on supported MySql versions.
Second question: Has anyone encountered this problem? I± have battled various library issues with the mysql2 gem over the passed year or so but this seems to be different.
Any assistance is gratefully recieved.
BTW: Reverting back to MySql 5.1 is a valid solution!
Regards
Peter
Or just apt-get install libmysqlclient-dev and then gem install mysql2
So the problem appears to have been a missing library and some links. Doing the following solved the issue.
Get the new and install the libmysqlclient-dev deb package kindly provided by Nathan Rennie Waldock
wget http://ppa.launchpad.net/nathan-renniewaldock/ppa/ubuntu/pool/main/m/mysql-5.5/libmysqlclient-dev_5.5.24-1~ppa1~natty_amd64.deb./;l
dpkg -i libmysqlclient-dev_5.5.24-1~ppa1~natty_amd64.deb
Then create some links that mysql2 needs as it can't seem to follow the mysql path
cd /usr/lib/
ln -s /usr/local/mysql-5.5.24-linux2.6-x86_64/lib/libmysqlclient_r.a .
ln -s /usr/local/mysql-5.5.24-linux2.6-x86_64/lib/libmysqlclient_r.so .
ln -s /usr/local/mysql-5.5.24-linux2.6-x86_64/lib/libmysqlclient_r.so.18 .
ln -s /usr/local/mysql-5.5.24-linux2.6-x86_64/lib/libmysqlclient_r.so.18.0.0 .
Now happily install the mysql2 gem
sudo gem install mysql2
Fetching: mysql2-0.3.11.gem (100%)
Building native extensions. This could take a while...
Successfully installed mysql2-0.3.11
1 gem installed
Installing ri documentation for mysq2l-0.3.11...
Installing RDoc documentation for mysql2-0.3.11...
Had the same issue in Ubuntu 12.04 also using the ppa mentioned in the accepted answer.
But also there was no libmysqlclient file on my system with _r in the name.
Googling around I found out that package builders have started to drop the _r suffix from the files, so that might have happened between 11.10 and 12.04.
Also the paths were a bit different to the once mentioned so these are the symlinks that I had to create:
ln -s /usr/lib/x86_64-linux-gnu/libmysqlclient.a ./libmysqlclient_r.a
ln -s /usr/lib/x86_64-linux-gnu/libmysqlclient.so ./libmysqlclient_r.so
ln -s /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18 ./libmysqlclient_r.so.18
ln -s /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.0.0 ./libmysqlclient_r.so.18.0.0
I had this problem as well...
I did dpkg -r mysql-devel as when I was trying to do apt-get install libmysqlclient-dev I got an error saying:
Unpacking libmysqlclient-dev (from .../libmysqlclient-dev_5.5.24-0ubuntu0.12.04.1_i386.deb) ... dpkg:
error processing
/var/cache/apt/archives/libmysqlclient-dev_5.5.24-0ubuntu0.12.04.1_i386.deb
(--unpack): trying to overwrite '/usr/bin/mysql_config', which is
also in package mysql-devel 5.5.28-2
After doing this, gem install mysql2 worked! yay :)

Errors Installing mysql2 gem via the Bundler

I am trying to install the mysql2 gem via the Bundler, but it keeps dying with the following error:
** executing command
/home/cc_rails/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/rubygems/installer.rb:483:in 'rescue in block in build_extensions':
ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)
/home/cc_rails/.rvm/rubies/ruby-1.9.2-p0/bin/ruby extconf.rb --with-mysql-config=/usr/bin/mysql_config
checking for rb_thread_blocking_region()... yes
checking for mysql.h... no
checking for mysql/mysql.h... no
-----
mysql.h is missing. please check your installation of mysql and try again.
-----
*** 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-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/home/cc_rails/.rvm/rubies/ruby-1.9.2-p0/bin/ruby
--with-mysql-config
Gem files will remain installed for inspection.
Most things I found via Googling recommended passing the --with-mysql-config parameter to fix it. So, based on:
$ which mysql_config
/usr/bin/mysql_config
I added the following to the Bundler's config:
$ bundle config build.mysql2 --with-mysql-config='/usr/bin/mysql_config'
However, still no luck -- same crash as above.
Since it's dying with the error mysql.h is missing, I checked for that, and it's allegedly around, just can't be found by the Bundler.
$ find / -name mysql.h
/usr/include/mysql5/mysql/mysql.h
Any thoughts?
Answer was similar to the one Wrikken posted -- here's what I did to fix it for the sake of future readers.
(This is for RHEL 5.5 -- similar but different commands apply for Ubuntu/Debian/etc.)
Doing sudo yum list installed will print out all installed packages on your machine (note: yum on RHEL requires you add a Red Hat Network repository [I use EPEL], and run it via sudo).
I had mysql and mysql-server, which explained why MySQL worked fine for every pre-existing app, but no mysql-devel, which is necessary to fix that mysql.h is missing error and similar other build errors.
Long story short, after a mysqldump -u root -ppassword --all-databases > full-dump.sql for safety, it was fixed with a simple
sudo yum install mysql-devel
For Ubuntu have to install following.
libmysqlclient-dev
libmysqlclient16
For Mac with a brew install of mysql the following solution fixed the problem for me:
I edited the mysql_config file in /usr/local/Cellar/mysql/5.6.12/bin and removed the W-compiler options -Wno-null-conversion and -Wno-unused-private-field for cflags and cxxflags.
This solved the problem with "gem install mysql2".
Reference: http://www.randomactsofsentience.com/2013/05/gem-install-mysql2-missing-mysqlh-on-os.html
The above problem will be occured because the mysql-devel package is not properly installed in your system. I will be explain the same in Centos of how to fix it. When you try to install that package using,
yum install mysql-devel
somtimes there will be some clash occurs with the existing packages that gets installed if you install the MySql-Administrative tool and MySQL query browser.
In that case, you need to uninstall all the existing mysql2 packages and install it again.
rpm -qa -last | grep -i mysql
yum remove MySQL-server-5.5.27-1.rhel5
yum remove MySQL-client-5.5.27-1.rhel5
yum remove mysql-gui-tools-5.0r12-1rhel4
yum remove mysql-query-browser-5.0r12-1rhel4-a
So, you can uninstall whatever mysql things displayed with rpm -qa as like above.
Then you can install the mysql-server and mysql-client.
yum install mysql-server
yum install mysql-client
Now you do the installation of mysql-devel package.
yum install mysql-devel
Now there is no package clashes and you can able to install the mysql2 gem.
gem install mysql2 -v '0.3.11'
Now your mysql2 gem will be successfully installed and you are good to go.
I got the same error. and for ubuntu 16. I had to write below command:
sudo apt-get install libmysqlclient-dev
and it work back.
In my case the problem was a misbehaving mysql_config script. When invoked by the command line with the --cflags option it would return a string of options containing:
-Xclang -target-feature -Xclang -aes -Qunused-arguments
For some reason the call to have_header('mysql.h') in the extconf.rb script would fail if those option were included.
What worked for me was to hand edit the mysql_config file removing the reference to those options from the line:
cflags="-I$pkgincludedir -Os -w -pipe -march=native -Xclang -target-feature -Xclang -aes -Qunused-arguments -O2 -g -DDBUG_OFF " #note: end space!
which I rewrote as:
cflags="-I$pkgincludedir -Os -w -pipe -march=native -O2 -g -DDBUG_OFF " #note: end space!
I know this is ancient, but if anyone still gets this zlib error, make certain that you typed:
rvm use
(whatever version you're using)
I could have sworn I did that. Just posting in case anyone is pulling their hair out and this helps. If not good luck. :)
I ran into this issue while bundle installing for redmine on fedora 23. The solution I found was to issue this command - sudo dnf install redhat-rpm-config.
Not only did this fix my issue with installing mysql2, but also for nokogiri and redcarpet.
On my Ubuntu 16.04.4 LTS running mysql Ver 15.1 Distrib 10.0.34-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2, the following worked:
sudo apt-get install libmariadb-client-lgpl-dev
I saw, that the error message of the current installer suggests running
apt-get install libmysqlclient-dev
This might work as well.

Error installing mysql2: Failed to build gem native extension

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?
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
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.
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 ---
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
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
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.
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!!!
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
If you are using yum try:
sudo yum install mysql-devel
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
On Debian Stretch the package that worked for me was default-libmysqlclient-dev
sudo apt-get update && apt-get install -y default-libmysqlclient-dev
If you are still having trouble….
Try installing
sudo apt-get install ruby1.9.1-dev
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).
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 ;)
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.
You have to Install some dependencies
sudo apt-get install libmysql-ruby libmysqlclient-dev
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
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.
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.
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"'
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.
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.
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.
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
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.
You are getting this problem because you have not install MySql. Before install mysql2 gem. Install MySQL. After that mysql2 gem will install.
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.]
libmysql-ruby has been phased out and replaced. New command:
sudo apt-get install ruby-mysql libmysqlclient-dev
Under ubuntu 20.04 LTS it's the only solution that have been working for me:
sudo apt-get install ruby-mysql2
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