Error when trying to install app with mysql2 gem
Asked Answered
A

20

108

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?

Angelia answered 14/6, 2015 at 20:53 Comment(4)
try to run "brew doctor" and check if there are any errors. If so, follow the instructions and try again.Mollusc
Have you installed gem's dependencies? github.com/brianmario/mysql2#general-instructionsBuxton
@ValAsensio, I heartily disagree with you there, I think you should always try to use your production db in development, if possible. I never use sqlite in development, nor does anyone I work with.Tripletail
@niels. Yes. I agree with this"I think you should always try to use your production db in development, " This ancient comment of mine was useless. I deleted it.Smuggle
M
78

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/
Meave answered 15/6, 2015 at 5:27 Comment(4)
That was not enough, One need to export it afterward like this: export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/Mortgagee
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/` Works for me after a long try. Thx!!Improbable
Why does Ruby suffer from this problem so much worse than any other ecosystems? Why do Gem developers not put effort into making good install processes?Sueannsuede
@AndyRay This is absolutely not ruby to blame, but MacOS. This OS has never been suitable for development.Meave
F
179

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.

Fireboard answered 22/9, 2016 at 0:31 Comment(8)
If you just want to use gem install, the following works: gem install mysql2 -v '0.3.21' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/includeTripletail
^^^ @Tripletail that should be the answer... Alessandro's does not work on Sierra ("Warning: Refusing to link: open ssl..." --> something to do with the deprecated system of openssl)Aladdin
@Aladdin niels's solution is for gem install, mine is for bundle install. What issue are you experiencing exactly, can you post the error? The solution I posted still works for me.Fireboard
I started from trying to bundle install for a repo that depends on mysql. @AlessandroBerardi's solution did not work for me; @niels' did. :/Epiboly
The bundle config command didn't work for me (although it has in the past). The 'gem install' command of niels did work (hours later). The bundle config command failed because it looks like it passed --with-ldflags and --with-cppflags as arguments to clang producing clang: error: unsupported option '--with-cppflags=-I/usr/local/opt/openssl/include'Salmagundi
@Aladdin niels' comment is already an answer https://mcmap.net/q/202422/-error-when-trying-to-install-app-with-mysql2-gemHeilbronn
--with-cppflags appears to break on latest OSX. I believe it can be omitted.bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib"Equitant
Yes just use the bundle config without the cppflags option and it works on big sir see xavier Shay's comment aboveCoccus
M
78

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/
Meave answered 15/6, 2015 at 5:27 Comment(4)
That was not enough, One need to export it afterward like this: export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/Mortgagee
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/` Works for me after a long try. Thx!!Improbable
Why does Ruby suffer from this problem so much worse than any other ecosystems? Why do Gem developers not put effort into making good install processes?Sueannsuede
@AndyRay This is absolutely not ruby to blame, but MacOS. This OS has never been suitable for development.Meave
M
47

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)

Madea answered 15/3, 2019 at 15:46 Comment(3)
Given that the SSL library was not found this is the correct approach and worked fine for me.Progressist
First install brew install openssl then gem install mysql2 -v '0.4.10' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include worked for me, Thanks!Breastplate
@TouseefMurtaza your answer worked for me - thank you!Hardening
E
36

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
Elf answered 30/10, 2016 at 10:30 Comment(0)
M
33

Based on the solution here

brew install openssl

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

solved the problem.

Mortgagee answered 18/6, 2019 at 20:15 Comment(1)
the export really helps. even with existing openssl the path is missing.Silverweed
T
18

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

Teetotum answered 31/7, 2020 at 13:51 Comment(2)
This is the way.Paraphrast
Confirming that this works in December 2022, while the other answers are a bit outdated.Falconiform
A
11

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

Angelia answered 15/6, 2015 at 8:25 Comment(0)
C
11

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

Coz answered 25/7, 2021 at 11:58 Comment(3)
This was the ONLY thing that worked for me on the M1 Mac Mini. Thank you for posting!Sigma
Thanks for this post!View
This is the solution for Mac M1 proVanderpool
C
5

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)
Crore answered 17/3, 2022 at 9:44 Comment(1)
I checked mysql folder name ls /opt/homebrew/Cellar/mysql, and found this folder 8.2.0_1. So, I used this command gem install mysql2 -- --with-mysql-dir=/opt/homebrew/Cellar/mysql/8.2.0_1. And it Worked! Thanks.Reporter
B
3
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

Bendix answered 19/1, 2019 at 12:24 Comment(0)
M
2

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
Marchall answered 12/12, 2019 at 11:6 Comment(0)
V
2

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
View answered 14/12, 2021 at 22:48 Comment(2)
That third line to install mysql2 generates this error on an M1 Mac w/ Monterey: linking shared-object mysql2/mysql2.bundle ld: warning: directory not found for option '-L/opt/homebrew/Cellar/zstd/1.5.0/lib' ld: library not found for -lzstd 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/u/.gem/ruby/2.6.5/gems/mysql2-0.5.3 for inspection. Results logged to /Users/u/.gem/ruby/2.6.5/extensions/-darwin-21/2.6.0-static/mysql2-0.5.3/gem_make.outKopple
wonderful its work on Mac M1 chip..Bagger
A
1

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.

Alisaalisan answered 28/4, 2020 at 3:54 Comment(0)
A
1

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

Aneurysm answered 8/8, 2021 at 1:11 Comment(0)
E
1

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

Erythro answered 18/2, 2022 at 2:10 Comment(0)
S
0

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!

Schrick answered 4/12, 2022 at 12:15 Comment(0)
B
0

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

Burt answered 29/12, 2022 at 15:2 Comment(0)
S
0

Ignoring implicit function declarations and linking zstd worked for me:

bundle config --global build.mysql2 '-- --with-cflags="-Wno-error=implicit-function-declaration" --with-ldflags=-L/opt/homebrew/opt/zstd/lib'
Shashaban answered 8/3, 2024 at 6:12 Comment(0)
D
0

The main answer always works for me, but this time (after updating to Ruby 3.2.3), it still didn't work with a different error:

client.c:1438:3: error: implicit declaration of function 'mysql_ssl_set' is invalid in C99
[-Werror,-Wimplicit-function-declaration]
  mysql_ssl_set(wrapper->client,
  ^
client.c:1438:3: note: did you mean 'mysql_close'?
/usr/local/Cellar/mysql/8.3.0_1/include/mysql/mysql.h:797:14: note: 'mysql_close' declared
here
void STDCALL mysql_close(MYSQL *sock);

I found that when we updated to Ruby 3.2.3, we also needed the mysql2 Gem to be on v0.5.6 for it to work. Just posting in case that helps someone else.

Dunant answered 3/4, 2024 at 3:29 Comment(0)
F
-1

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"
Frisch answered 14/3, 2019 at 0:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.