Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.


I was having similar issue, but for v4.10

Error occurred while installing mysql2 (0.4.10), and Bundler cannot continue

So I had to run the below commands to fix the above issue.

sudo apt-get update
sudo apt-get install build-essential

Ubuntu 18

You may need to install

sudo apt-get install libssl-dev

as in This answer


MacOS Monterey (M1)

The location of the lib and include files has changed in Homebrew. I used this guidance to set the correct paths for the installation.

I started out by refreshing my Ruby and Rails installation using this procedure. This installs the openssl dependency.

$ export LIBRARY_PATH=$LIBRARY_PATH:$(brew --prefix [email protected])/lib/

After double checking my mysql install, I had a final hurdle with zstd blocking the build. I fixed this using (credit to this answer):

 bundle config --local build.mysql2 \
   "--with-ldflags=-L/opt/homebrew/Cellar/zstd/1.5.0/lib"

You're now read to run bundle again

 bundle install