An error occurred while installing mysql2 (0.4.8), and Bundler cannot continue

I think you missing dev library of mysql:

On ubuntu

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

For Mac, I had a problem with openssl issue

An error occurred while installing mysql2 (0.5.2), and Bundler cannot continue.

I fixed this issue use command as below;

$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"
$ sudo bundle install

Console Result:

Fetching mysql2 0.5.2
Installing mysql2 0.5.2 with native extensions
Using puma 3.12.0
Using sprockets 3.7.2
Using sprockets-rails 3.2.1
Fetching rails 5.2.2.1
Installing rails 5.2.2.1
Using rubyzip 1.2.2
Using sass-listen 4.0.0
Using sass 3.7.3
Using tilt 2.0.9
Using sass-rails 5.0.7
Using selenium-webdriver 3.141.0
Using spring 2.0.2
Using spring-watcher-listen 2.0.1
Using turbolinks-source 5.2.0
Using turbolinks 5.2.0
Using uglifier 4.1.20
Using web-console 3.7.0
Bundle complete! 18 Gemfile dependencies, 79 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

Alternative Resolve

if you still have error and got the message in the console that: You are replacing the current local value of build.mysql2, which is currently nil. Please try as below:

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

Then run:

$ sudo 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

Show 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

Finally, run bundle install in your project:

$ sudo bundle install

Let me know if it can save your day!!! :)


if you are on mac, make sure openSSL is installed first.

brew install openssl

then export

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

make sure you have mysql installed

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

then run bundle

bundle install

I almost lost whats remaining of my hair because I got a new machine and I didnt remember that I had to install openssl.