Error when trying to install app with mysql2 gem

MysqlRuby on-RailsRubyRuby on-Rails-4Mysql2

Mysql Problem Overview


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?

Mysql Solutions


Solution 1 - Mysql

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.

Solution 2 - Mysql

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/

Solution 3 - Mysql

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)

Solution 4 - Mysql

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

Solution 5 - Mysql

Based on the solution here

brew install openssl

export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/

solved the problem.

Solution 6 - Mysql

After Homebrew update ([email protected]) 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

Solution 7 - Mysql

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: https://stackoverflow.com/questions/21508866/openssl-rvm-brew-conflicting-error

Solution 8 - Mysql

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

Solution 9 - Mysql

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

Solution 10 - Mysql

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

Solution 11 - Mysql

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

Solution 12 - Mysql

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.

Solution 13 - Mysql

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

Solution 14 - Mysql

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

Solution 15 - Mysql

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)

Solution 16 - Mysql

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"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzenvelopeView Question on Stackoverflow
Solution 1 - MysqlAlessandro BerardiView Answer on Stackoverflow
Solution 2 - MysqlAleksei MatiushkinView Answer on Stackoverflow
Solution 3 - MysqlstereodenisView Answer on Stackoverflow
Solution 4 - MysqlAllenView Answer on Stackoverflow
Solution 5 - MysqlGilg HimView Answer on Stackoverflow
Solution 6 - MysqlAlexey MorozovView Answer on Stackoverflow
Solution 7 - MysqlzenvelopeView Answer on Stackoverflow
Solution 8 - MysqlBhupendra singh negiView Answer on Stackoverflow
Solution 9 - Mysqlk1r8r0wnView Answer on Stackoverflow
Solution 10 - MysqlHaseeb ZulfiqarView Answer on Stackoverflow
Solution 11 - MysqlPaul TrottView Answer on Stackoverflow
Solution 12 - MysqlEmad SaeedView Answer on Stackoverflow
Solution 13 - MysqlDaniel CintraView Answer on Stackoverflow
Solution 14 - MysqlicnView Answer on Stackoverflow
Solution 15 - MysqlSajad TorkamaniView Answer on Stackoverflow
Solution 16 - MysqlBrian J. FoxView Answer on Stackoverflow