mysql2: LoadError: cannot load such file -- mysql2/mysql2

I’ve seen all the issues regarding this error, and I’ve tried all the suggestions, and I’m still getting this error. This is a relatively new Mac OS install, and I’ve used mysql2 without any issues before, which is why this is truly baffling.

I’m seeing the error with multiple Rubies: 2.3.3, 2.3.7, 2.4.1.

Here’s what I did:

$ ruby --version
ruby 2.3.7p456 (2018-03-28 revision 63024) [x86_64-darwin17]
$ brew install mysql
$ mysql --version
mysql  Ver 8.0.12 for osx10.13 on x86_64 (Homebrew)
$ gem install mysql2 -V -- --srcdir=/usr/local/include/mysql --platform=ruby
[...]
Successfully installed mysql2-0.5.2
1 gem installed
$ ruby -e "require 'mysql2'"
[...]
cannot load such file -- mysql2/mysql2 (LoadError)

That’s the short version. I initially encountered this problem when trying to do a bundle install on an existing project, with an earlier version of mysql2, and that led me to the above. I’ve tried different options with the gem command such as:

  • –srcdir=/usr/local/Cellar/mysql/8.0.12
  • –with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
  • as well as trying different Ruby versions

Homebrew is up-to-date, XCode is installed, etc. Other gems that require compiled extensions are working fine. Am I just missing something obvious here?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 7
  • Comments: 37

Most upvoted comments

I’ve solved that adding mysql_config

gem install mysql2 -v 0.4.4 -- --with-mysql-config=/usr/local/Cellar/mysql@5.6/5.6.42/bin/mysql_config --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include

It work for me.

  • sudo gem install mysql2
  • gem uninstall mysql2 (in your rails app)
  • bundle

Months later on another computer, I’m able to run bundle and rails environment, but not rspec. Same error message.

Edit: Thanks to @dominicsayers above, I deleted the contents of tmp/ and could run Rspec. Bootsnap might have been involved.

I’m facing the same issue as well. I’m using:

  • Ruby 2.6.1
  • mysql 8.0.15
  • mysql2 gem 0.5.1
  • rbenv

I’ve separately tried both of the following commands to download mysql2 (after just doing gem install mysql2 to no avail), and while I’m able to download mysql2 0.5.1 (with either command), I can’t get rid of the Rails Server issue:

  1. gem install mysql2 -v 0.5.1 -- --with-mysql-config=/usr/local/Cellar/mysql/8.0.15/bin/mysql_config --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include --platform=ruby (courtesy of @tiagogeraldi’s comment)
  2. gem install mysql2 -v '0.5.1' -- --srcdir=/usr/local/include/mysql --platform=ruby

Please help!

Edit: Added where I got the first command from.

Edit 2: Fixed it! This is what I did:

  • I uninstalled mysql2 using gem uninstall mysql2
  • I used the first command from above to install the mysql2 gem, but I used version 0.5.2 instead of 0.5.1 (the latest version currently):
    • gem install mysql2 -v 0.5.2 -- --with-mysql-config=/usr/local/Cellar/mysql/8.0.15/bin/mysql_config --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include --platform=ruby
  • I updated my Gemfile.lock file with the correct mysql2 version (mysql2 (0.5.2))
  • I ran bundle install

And I’m able to run my server now 😃

As of today Homebrew defaults to MySQL 8.0, OpenSSL 1.1 and mysql2 0.5 that work great all together out of the box. However to upgrade of my legacy applications from Rails 3.2 to more recent versions I had to setup local environment on my MacOS BigSur 11.1.

My goal was to install mysql2 gem version 0.3.x (this is only one allowing smooth migration from Rails 3.2 to Rails 4.2 through versions 4.0 and 4.1) together with mySQL 5.x. Both of them in turn depend on OpenSSL 1.0

Here is what worked for me:

// Clean-up all your faulty attempts you did before
// Homebrew does not store openssl 1.0 anymore, so I had to find alternative — openssl1.0.2t 
wget https://github.com/tebelorg/Tump/releases/download/v1.0.0/openssl.rb
brew install openssl.rb

brew install mysql@5.7
brew link mysql@5.7 --force
brew install mysql-client@5.7
// NB! Pay close attention to path where you have mysql_config and openssl 1.0 installed
gem install mysql2 -v 0.3.21 -- --with-mysql-config=/usr/local/bin/mysql_config --with-ldflags=-L/usr/local/Cellar/openssl/1.0.2t/lib --with-cppflags=-I/usr/local/Cellar/openssl/1.0.2t/include

Voilá:

Building native extensions with: '--with-mysql-config=/usr/local/bin/mysql_config --with-ldflags=-L/usr/local/Cellar/openssl/1.0.2t/lib --with-cppflags=-I/usr/local/Cellar/openssl/1.0.2t/include'
This could take a while...
Successfully installed mysql2-0.3.21
Parsing documentation for mysql2-0.3.21
Installing ri documentation for mysql2-0.3.21
Done installing documentation for mysql2 after 0 seconds
1 gem installed

For anyone having the same problems as me in https://github.com/brianmario/mysql2/issues/1010#issuecomment-480860283, in the end I deleted my project folder, did a git clone and rebundled from scratch. That fixed it for me.

@rhugo thats on M1? On M1, for me always works this, when pointing to exact paths:

gem install mysql2 -v '0.5.5' -- \
--with-mysql-lib=/opt/homebrew/Cellar/mysql@5.7/5.7.38/lib \
--with-mysql-dir=/opt/homebrew/Cellar/mysql@5.7/5.7.38 \
--with-mysql-config=/opt/homebrew/Cellar/mysql@5.7/5.7.38/bin/mysql_config \
--with-mysql-include=/opt/homebrew/Cellar/mysql@5.7/5.7.38/include

brew + for none M1 Macs:

$ gem install mysql2 -v '0.5.5' -- \
--with-mysql-lib=/usr/local/Cellar/mysql@5.7/5.7.38/lib \
--with-mysql-dir=/usr/local/Cellar/mysql@5.7/5.7.38 \
--with-mysql-config=/usr/local/Cellar/mysql@5.7/5.7.38/bin/mysql_config \
--with-mysql-include=/usr/local/Cellar/mysql@5.7/5.7.38/include

@sajjadmurtaza @vishaldeepak Have you tried deleting tmp/?

For M1 this worked for me:

gem install mysql2 -v 0.5.3 – --with-mysql-config=/opt/homebrew/opt/mysql@5.7/bin/mysql_config --with-ldflags=-L/opt/homebrew/opt/openssl@1.1/lib --with-cppflags=-I/opt/homebrew/opt/openssl@1.1/include

On my new mac M1 CPU, the directories changed a bit:

gem install mysql2 -v 0.4.4 -- --with-mysql-config=/opt/homebrew/Cellar/mysql@5.7/5.7.34/bin/mysql_config --with-ldflags=-L/opt/homebrew/opt/openssl/lib --with-cppflags=-I/opt/homebrew/opt/openssl/include

I now have this issue with

  • Ruby 2.6.2
  • MySQL 5.7.25
  • mysql2 gem 0.5.2
  • rbenv

I have tried all the above remedies. After manually installing mysql2 I can run bin/rails console and perform database operations but I cannot run rspec.

Installation:

gem uninstall mysql2
gem install mysql2 -v '0.5.2' -- --with-mysql-config=/usr/local/Cellar/mysql@5.7/5.7.25/bin/mysql_config --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include --platform=ruby
bundle install

RSpec:

$ bin/rspec
/Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:33:in `require': cannot load such file -- mysql2/mysql2 (LoadError)
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `block in require'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:257:in `load_dependency'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `require'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/mysql2-0.5.2/lib/mysql2.rb:33:in `<main>'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.2/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/bundler-2.0.1/lib/bundler/runtime.rb:81:in `block (2 levels) in require'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/bundler-2.0.1/lib/bundler/runtime.rb:76:in `each'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/bundler-2.0.1/lib/bundler/runtime.rb:76:in `block in require'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/bundler-2.0.1/lib/bundler/runtime.rb:65:in `each'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/bundler-2.0.1/lib/bundler/runtime.rb:65:in `require'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/bundler-2.0.1/lib/bundler.rb:114:in `require'
	from /Users/dominic/Development/openc/oc-core-snapshots-app/config/application.rb:12:in `<top (required)>'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/spring-2.0.2/lib/spring/application.rb:92:in `require'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/spring-2.0.2/lib/spring/application.rb:92:in `preload'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/spring-2.0.2/lib/spring/application.rb:153:in `serve'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/spring-2.0.2/lib/spring/application.rb:141:in `block in run'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/spring-2.0.2/lib/spring/application.rb:135:in `loop'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/spring-2.0.2/lib/spring/application.rb:135:in `run'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/spring-2.0.2/lib/spring/application/boot.rb:19:in `<top (required)>'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	from /Users/dominic/.rbenv/versions/2.6.2/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	from -e:1:in `<main>'

As I said, console is OK:

$ bin/rails console
Running via Spring preloader in process 35672
Loading development environment (Rails 5.2.3)
irb(main):001:0> Snapshot.first
   (3.0ms)  SET NAMES utf8mb4 COLLATE utf8mb4_unicode_520_ci,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
  Snapshot Load (0.9ms)  SELECT  `snapshots`.* FROM `snapshots` ORDER BY `snapshots`.`id` ASC LIMIT 1
=> nil

Reading @awead’s comment above, I ran into this and it was important not to include --srcdir.

What I ended up using:

bundle config build.mysql2 --with-mysql-config=/usr/local/opt/mysql-client/bin/mysql_config --platform=ruby --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include --with-opt-dir=/usr/local/opt/openssl@1.1