Ruby MYSQL2 gem installation on windows 7
EDIT 30/09/2014
When this answer was posted the 64 bit rails installer wasn't the recommended version - it now seems people are starting to use it more. It should be noted when you download the MySQL Connector you need to download either 64 or 32bit to correspond to the version of rails you installed.
Amazingly I lucked upon an answer very early this morning as I happened to be looking for something else of a similar nature. I'm not quite sure why there isn't a single simple guide for this as it appears to be very straight forward!
For some reason just specifying the mysql-dir when you install the gem doesn't pick up with other sub directories so you need to set the parameters manually.
For anyone else experiencing the same problem, I did the following:
1) Download the MySql C Connector from: http://dev.mysql.com/downloads/connector/c/
NOTE Don't download the installer, download the ARCHIVE for your OS
Download either the 32bit or 64 bit ARCHIVE to correspond with the version of rails you installed.
2) Extract the file to C:\mysql-connector
3) Then ran:
gem install mysql2 --platform=ruby -- '--with-mysql-lib="C:\mysql-connector\lib" --with-mysql-include="C:\mysql-connector\include" --with-mysql-dir="C:\mysql-connector"'
Voila everything is working fine.
EDIT 30/01/2014
I just had to do a fresh install on a bricked machine and the command in step 3 didn't work, what did work was:
gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:\mysql-connector"'
I'm not quite sure what the difference is but this time it seems to be picking up the directories ok, so if the first one doesn't work try this one!
I think this has to do with how you go about installing rails, this time round I used the railsinstaller which seems to set the paths up correctly.
A lot of the outcome here seems to depend on the shell your using, a lot of people are having problems with powershell so I wouldn't advise using it. I did this in an elevated command prompt.
Oh and lastly if you get an error regarding the mysql2 gem when you do RAILS S you need to copy the libmysql.dll from the LIB directory of the mysql connector to the bin directory where rails has been installed.
Installing Mysql 2 gem fails
You're installing an older version of the gem (0.4.5). I had the same issue (using 0.4.3). Installing version 0.4.10 solved this for me.
Try this:
gem install mysql2 -v 0.4.10
If that installs cleanly, you'll have to update your Gemfile
to require this version:
gem 'mysql2', '~> 0.4.10'
Don't install a newer version of the gem (i.e. 0.5.x), they won't work with Rails 4 or older releases of Rails 5 (before 5.0.7/5.1.6) (see https://github.com/brianmario/mysql2/issues/950).
I cannot load my 'mysql2' gem.
There is a bug in Rails 4.2.4 and previous, with the newly released 0.4.0 version of the mysql2 gem -- one part of Rails will accidentally refuse to use the newly released 0.4.0 version of mysql2.
The issue is reported here, although without a lot of details:
https://github.com/rails/rails/issues/21544
Until a new version of Rails is released that fixes this one way or another, add this to your Gemfile, specifying that mysql2 0.4.0 won't work:
# mysql 0.4.0 does not work with Rails 4.2.4
# https://github.com/rails/rails/issues/21544
gem 'mysql2', '>= 0.3.13', '< 0.4.0'
You previously probably just have gem 'mysql2'
in your Gemfile -- add the version constraints as above, so it knows 0.4.0 won't work. Add the comments so you know why you did it, and can remove it later when no longer neccesary (probably whenever Rails 4.2.5 comes out).
Edit the Gemfile in your app like above, and then run bundle update mysql2
in your app directory, so your app will be using a mysql2 gem version 0.3.x again, as current Rails version wants.
When Rails 4.2.5 or later comes out and you upgrade to it, you will probably want to go back to your Gemfile and remove the version requirement specification for mysql2, return it to saying just gem 'mysql2'
again. So your app will be willing to use the newer mysql2 0.4.0 gem, once Rails is willing to do so too.
Can't install mysql2 Gem on MAMP
I've fixed this by simply running gem install using the mysql config an existing config.
gem install mysql2 -v {mysql2_gem_version} -- --with-mysql-config=/usr/local/Cellar/mysql/{mysql_gem_version}/bin/mysql_config
gem install mysql2 -v '0.3.17' -- --with-mysql-config=/usr/local/Cellar/mysql/5.6.21/bin/mysql_config
Bundler ignoring install of MySQL2 gem when run on my 'production' server
All sorted. Although it's with a work-around I'm not entirely happy with.
The Research
More searches of Google led me to this page:
http://redmine.autotelik.co.uk/blogs (do a search for 'mysql2' and it will find the relevant part)
This led me to... Stack Overflow (of course!):
Make bundler use different gems for different platforms
Which in turn led me to:
https://github.com/carlhuda/bundler/issues/646
The Problem
When you run Bundler on Windows, it includes an entry for the Windows version of mysql2. However, this is different to what's required on Linux. As a result, the Gemfile.lock file is inappropriate for use on Linux.
The Solution
The goal is to have the Gemfile.lock file generated by Bundler to be generated on the production server. From what I've read, this isn't generally recommended. That said, to achieve this...
I no longer include the Gemfile.lock file in my git repository:
echo 'Gemfile.lock' >> .gitignore
I've removed the file from git:
git rm Gemfile.lock
git commit -am "Removing Gemfile.lock to cope with Windows and Linux differences"
git push
I've told Capistrano to not run in deployment mode by adding the following line to my config/deploy.rb
:
set :bundle_flags, ""
You might want to include the --quiet
flag in the quotes, but after this experience I'm keen to see what get's installed when I deploy.
Now when I run cap production deploy
it'll regenerate the Gemfile.lock
file each time. This means the correct version of mysql2 is now used on my production server.
I'd imagine this can be improved upon, but for now it's working for me.
Hope this helps someone someday.
Can't install mysql2 gem ... (side stuff on homebrew too)
A few things:
apt-get
is a package installation tool for Linux, so those commands won't work with your Mac.
You have a permissions issue with your /usr/local
directory which is causing the errors with brew update
and brew install
. To fix this, you can give yourself ownership with the following command:
sudo chown -R $USER /usr/local
There are some caveats with this, so check out this thread: Installing in Homebrew errors
Next, you don't have the mysql
installed. The mysql2
gem does not install mysql
; it is simply a Ruby library that allows you to connect to a running mysql
process with Ruby code. I don't believe you can install the gem unless the process is installed and running, which explains that big ugly stack trace from gem install mysql2
.
So, after you update your /usr/local
permissions, retry brew install mysql
and follow the instructions to get the service running. You can confirm that it's working if you see mysqld
or mysqld_safe
daemon process running with a command like ps ax | grep mysqld
. Assuming that works, you should then be able to install the mysql2
gem.
Related Topics
Using Activerecord Interface for Models Backed by External API in Ruby on Rails
Sleep Until Condition Is True in Ruby
How to Get Rspec to Run All Tests Nested Under a Folder
"Gem Update --System Is Disabled on Debian" Error
Controlling Tor Client with Ruby
Carrierwave File Upload with Different File Types
Memory Size of a Hash or Other Object
Rails - Paperclip Validating Attachment Size When It Shouldn't Be
Dynamic CSS in Rails Asset Pipeline, Compile on Fly
Advice on Using Modules with Ruby on Rails
Ruby: How to Calculate a Path Relative to Another One
Rails Nested With_Option :If Used in Validation
Datetime with Mongodb/Mongoid and Rails 3 Not Populating
Ruby on Rails - Paperclip and Dynamic Parameters