Fail to install MySQL2 gem on Mac - mysql

I'm trying to install mysql2 gem for rails applications but it gives me following error while running sudo gem install mysql2
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.
current directory: /Users/ateeq/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.4.5/ext/mysql2
/Users/ateeq/.rbenv/versions/2.3.1/bin/ruby -r ./siteconf20161103-83594-10ft58j.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
checking for rb_big_cmp()... yes
-----
Using mysql_config at /usr/local/bin/mysql_config
-----
checking for mysql.h... yes
checking for SSL_MODE_DISABLED in mysql.h... yes
checking for SSL_MODE_PREFERRED in mysql.h... yes
checking for SSL_MODE_REQUIRED in mysql.h... yes
checking for SSL_MODE_VERIFY_CA in mysql.h... yes
checking for SSL_MODE_VERIFY_IDENTITY in 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.7.16/lib
-----
creating Makefile
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/Users/ateeq/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-16/2.3.0-static/mysql2-0.4.5/mkmf.log
current directory: /Users/ateeq/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.4.5/ext/mysql2
make "DESTDIR=" clean
current directory: /Users/ateeq/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.4.5/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
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
make failed, exit code 2
Gem files will remain installed in /Users/ateeq/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.4.5 for inspection.
Results logged to /Users/ateeq/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-16/2.3.0-static/mysql2-0.4.5/gem_make.out
I installed rails via brew & rbenv. Versions are mentioned below:
Ruby Version: ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin16]
Rails Version: Rails 4.2.6
MySql Version: mysql-5.7.16

Pull up an instance of Terminal and run:
brew doctor
It's likely that one or more dependencies for building the mysql gem are out of date.
You'll have to run:
brew update
You probably also upgraded to the Sierra release of OS X recently. Make sure your XCode command line tools is up to date. Run:
xcode-select --install
Make sure you accept the license for the new version of XCode by running:
sudo xcodebuild -license

Here is my solution for above error:
I just uninstalled mysql by running brew remove mysql and then run following command to install mysql2 gem sudo gem install mysql2. It worked for me. After this, I reinstalled mysql by running brew install mysql.
P.s. I don't know about this behaviour. If anyone can explain, it'd be great.

Related

Couldn't install mysql2 0.5.3 in M1 MacBook Big Sur

Hi I am trying to install mysql2 gem in MacBook M1 Big Sur, I am getting the the following errors.
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
Full Error
Using mysql_config at /opt/homebrew/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for SSL_MODE_DISABLED in mysql.h... yes
checking for SSL_MODE_PREFERRED in mysql.h... yes
checking for SSL_MODE_REQUIRED in mysql.h... yes
checking for SSL_MODE_VERIFY_CA in mysql.h... yes
checking for SSL_MODE_VERIFY_IDENTITY in mysql.h... yes
checking for MYSQL.net.vio in mysql.h... yes
checking for MYSQL.net.pvio in mysql.h... no
checking for MYSQL_ENABLE_CLEARTEXT_PLUGIN in mysql.h... yes
checking for SERVER_QUERY_NO_GOOD_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_NO_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_WAS_SLOW in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_ON in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_OFF in mysql.h... yes
checking for my_bool in mysql.h... no
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /opt/homebrew/Cellar/mysql/8.0.26/lib
-----
creating Makefile
current directory: /Users/santosh/.rvm/gems/ruby-2.6.3/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR=" clean
current directory: /Users/santosh/.rvm/gems/ruby-2.6.3/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
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/santosh/.rvm/gems/ruby-2.6.3/gems/mysql2-0.5.3 for inspection.
Results logged to /Users/santosh/.rvm/gems/ruby-2.6.3/extensions/-darwin-20/2.6.0/mysql2-0.5.3/gem_make.out
An error occurred while installing mysql2 (0.5.3), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'` succeeds before bundling.
I tried to install with different version of Ruby, nothing went well.
I had this problem too and I think what finally solved it was running:
gem install mysql2 -v '0.5.3' -- --with-opt-dir=$(brew --prefix openssl) --with-ldflags=-L/opt/homebrew/Cellar/zstd/1.5.0/lib
However, for what it's worth, I'd previously installed without success but might have also been needed:
homebrew, and brew doctor said it was ready to brew.
Xcode tools xcode-select --install
mysql brew install mysql (and I could make a local connection)
openssl brew install openssl && brew link openssl --force
rbenv and rbenv rehash
cmake brew install cmake
Apple Macbook air M1. Monterey. You need to install MySQL and openssl using brew .
This works for me.
gem install mysql2 -- --with-opt-dir=/opt/homebrew/opt/openssl#3 --with-mysql-dir=/opt/homebrew/opt/mysql

mysql2: Cannot find mysql_config at /usr/local/Cellar/mysql#5.6/5.6.42/bin/mysql_config after system upgrade

I upgraded mac OS Mojave from v10.14.0 to v10.14.2 and all the packages I installed with Homebrew. MySQL got upgraded from v5.6.42 to v5.6.46. After the upgrade mysql2 gem started to fail to compile with:
current directory: /Users/hirurg103/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/mysql2-0.4.4/ext/mysql2
/Users/hirurg103/.rbenv/versions/2.4.1/bin/ruby -r ./siteconf20191218-70879-1yhhybm.rb extconf.rb
--with-mysql-config=/usr/local/Cellar/mysql#5.6/5.6.42/bin/mysql_config
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
-----
Cannot find mysql_config at /usr/local/Cellar/mysql#5.6/5.6.42/bin/mysql_config
-----
I looked at the contents of the /usr/local/Cellar/mysql#5.6 folder and there is no 5.6.42 directory in it:
ls -l /usr/local/Cellar/mysql#5.6/
total 0
drwxr-xr-x 16 hirurg103 staff 512 Dec 18 13:50 5.6.46_2
I am using mysql2 version 0.4.4
Do you have any ideas how to fix this issue? Thank you!
I was able to solve this issue by running gem install mysql2 -v 0.4.4 with --with-mysql-config argument:
gem install mysql2 -v 0.4.4 -- --with-mysql-config=/usr/local/Cellar/mysql#5.6/5.6.46_2/bin/mysql_config
After this bundle install went without errors

Ruby on Rails bundle install mysql2 error

About
Currently, I`m building the Ruby on Rails project environment.
However, if I implement bundle install, the following error occurs.
I do not know where the cause is, I would like advice.
Environment
Ruby v2.5.3
Rails 5.2.2
MySQL 5.7
Run Command 1
$ bundle install
Output error
Errno::EACCES: Permission denied # rb_sysopen -
/Users/xxx/project_dir/vendor/bundle/ruby/2.5.0/gems/mysql2-0.5.2/CHANGELOG.md
An error occurred while installing mysql2 (0.5.2), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'` succeeds before bundling.
Run Command 2
$ sudo gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'
Output error
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.
current directory: /Users/xxx/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2/ext/mysql2/Users/xxx/.rbenv/versions/2.5.3/bin/ruby -I /Users/xxx/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0 -r ./siteconf20190220-84910-2gf7i1.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
-----
Using mysql_config at /usr/local/opt/mysql#5.7/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for SSL_MODE_DISABLED in mysql.h... yes
checking for SSL_MODE_PREFERRED in mysql.h... yes
checking for SSL_MODE_REQUIRED in mysql.h... yes
checking for SSL_MODE_VERIFY_CA in mysql.h... yes
checking for SSL_MODE_VERIFY_IDENTITY in mysql.h... yes
checking for MYSQL.net.vio in mysql.h... yes
checking for MYSQL.net.pvio in mysql.h... no
checking for MYSQL_ENABLE_CLEARTEXT_PLUGIN in mysql.h... yes
checking for SERVER_QUERY_NO_GOOD_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_NO_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_WAS_SLOW in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_ON in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_OFF in mysql.h... yes
checking for my_bool in mysql.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/opt/mysql#5.7/lib
-----
creating Makefile
current directory: /Users/xxx/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2/ext/mysql2
make "DESTDIR=" clean
current directory: /Users/xxx/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
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
make failed, exit code 2
It seems that some system libraries are missing. As far as I understand you are using macos. So please try: xcode-select --install and bundle install
UPDATE
We had a private chat with #getty and we came up a solution. The problem was with some missing headers regarding to ruby program.
So we fixed the problem like following:
rbenv uninstall 2.5.3
CC=gcc rbenv install 2.5.3
rbenv local 2.5.3
bundle install
This solution would be applied to rvm or other version managers.

Cannot install mysql2 gem inside Mysql Docker container: Failed to build gem native extension

Related Github Issue: https://github.com/docker-library/mysql/issues/441
Trying to install Sequel gem (https://github.com/jeremyevans/sequel) fails with "Failed to build gem native extension".
Docker image:
FROM mysql:latest
RUN apt-get update
RUN apt-get install -y rubygems ruby-mysql2 wget build-essential libmysqlclient-dev ruby2*-dev sqlite3 libsqlite3-dev
RUN gem install mysql2
Output:
$ docker build -t "mysqltest" .
Sending build context to Docker daemon 8.704kB
Step 1/4 : FROM mysql
---> a8a59477268d
Step 2/4 : RUN apt-get update
---> Using cache
---> ab35c534ffcf
Step 3/4 : RUN apt-get install -y rubygems ruby-mysql2 wget build-essential libmysqlclient-dev ruby2*-dev sqlite3 libsqlite3-dev
---> Using cache
---> b703bf4ef78d
Step 4/4 : RUN gem install mysql2
---> Running in 79790b7f4506
Building native extensions. This could take a while...
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.
current directory: /var/lib/gems/2.3.0/gems/mysql2-0.5.1/ext/mysql2
/usr/bin/ruby2.3 -r ./siteconf20180610-8-1ixqzle.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
-----
Using mysql_config at /usr/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for SSL_MODE_DISABLED in mysql.h... yes
checking for SSL_MODE_PREFERRED in mysql.h... yes
checking for SSL_MODE_REQUIRED in mysql.h... yes
checking for SSL_MODE_VERIFY_CA in mysql.h... yes
checking for SSL_MODE_VERIFY_IDENTITY in mysql.h... yes
checking for MYSQL.net.vio in mysql.h... yes
checking for MYSQL.net.pvio in mysql.h... no
checking for MYSQL_ENABLE_CLEARTEXT_PLUGIN in mysql.h... yes
checking for SERVER_QUERY_NO_GOOD_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_NO_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_WAS_SLOW in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_ON in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_OFF in mysql.h... yes
checking for my_bool in mysql.h... no
-----
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/lib/x86_64-linux-gnu
-----
creating Makefile
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/var/lib/gems/2.3.0/extensions/x86_64-linux/2.3.0/mysql2-0.5.1/mkmf.log
current directory: /var/lib/gems/2.3.0/gems/mysql2-0.5.1/ext/mysql2
make "DESTDIR=" clean
current directory: /var/lib/gems/2.3.0/gems/mysql2-0.5.1/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.so
/usr/bin/ld: cannot find -lssl
/usr/bin/ld: cannot find -lcrypto
collect2: error: ld returned 1 exit status
Makefile:255: recipe for target 'mysql2.so' failed
make: *** [mysql2.so] Error 1
make failed, exit code 2
Gem files will remain installed in /var/lib/gems/2.3.0/gems/mysql2-0.5.1 for inspection.
Results logged to /var/lib/gems/2.3.0/extensions/x86_64-linux/2.3.0/mysql2-0.5.1/gem_make.out
The command '/bin/sh -c gem install mysql2' returned a non-zero code: 1
mkmf.log content: https://gist.github.com/ifischer/529950ce6af29280d5c778e702321b35
Found the problem:
I need to install default-libmysqlclient-dev instead of libmysqlclient-dev:
FROM mysql:latest
RUN apt-get update
RUN apt-get install -y rubygems ruby-mysql2 wget build-essential default-libmysqlclient-dev ruby2*-dev sqlite3 libsqlite3-dev
RUN gem install mysql2 sequel sqlite3

Error Installing Mysql2 on OSX

Attempting to install a specific version Mysql2 Gem. And I get this:
sudo gem install mysql2 -v '0.3.20'
Building native extensions. This could take a while...
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.
/usr/local/var/rbenv/versions/2.2.3/bin/ruby -r ./siteconf20161017-56699-1dsu6v9.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()... no
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.7.16/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: 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
For reference I ran brew install mysql beforehand and it ran successfully. Also I tried uninstalling all versions of brew's mysql references just in case.
Is there something im missing? I've looked around and most pages point back to making sure you run brew install mysql which I have.
In case I wasn't more clear with my above explanation, the failure I am getting is after successfully running brew install mysql the error is from trying to install the gem afterwards via gem install mysql2 -v '0.3.20'
Any idea?
Thanks
How to make it work on MacOS Mojave.
Make sure openssl is installed on Mac via Homebrew:
brew install openssl
Then install mysql2 gem following this special way:
gem install mysql2 -v 'x.x.x' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
where x.x.x - specified version of the mysql2 gem.
Just for reference what ended up working was xcode-select --install which seemed to install the OpenSSL libraries which took care of the ld: library not found for -lssl issue.