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
I’ve solved that adding mysql_config
It work for me.
Months later on another computer, I’m able to run bundle and
rails environment
, but notrspec
. 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:
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: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)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:
gem uninstall mysql2
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
Gemfile.lock
file with the correct mysql2 version (mysql2 (0.5.2)
)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:
Voilá:
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:
brew + for none M1 Macs:
@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:
I now have this issue with
I have tried all the above remedies. After manually installing
mysql2
I can runbin/rails console
and perform database operations but I cannot runrspec
.Installation:
RSpec:
As I said,
console
is OK:Reading @awead’s comment above, I ran into this and it was important not to include
--srcdir
.What I ended up using:
This https://github.com/brianmario/mysql2/issues/1010#issuecomment-460257986 solved it