Rails on Windows (oh the pain!)

Why? I bet you’re thinking why: why would I be crazy enough to want to develop in rails on a Windows machine?
I plan to work on a project with a friend and we both have Windows Surfaces. We both also have iMacs at home, but it would be easier for us to develop on something portable so that we can work together when we catch up. Also, I was kind of interested in working with Windows a bit more, because I tend to have a very negative opinion of it (other than for gaming) and I want to work on that. Now that that’s all out of the way, time for the fun part!

After failing multiple times to get rails working locally with a mysql database, I finally found this StackOverflow thread which helped me install rails and get it working with mysql on Window 10 64 bit http://stackoverflow.com/questions/21126326/installing-mysql2-gem-on-windows. The steps below aim to extend what’s written in the Stack Overflow answer to include some things that helped me set up my dev environment.

1. Cleanup

  • Uninstall all ruby versions using Control Panel > Programs and Features, just get rid of it, it’s not like you’ve got it working anyway!
  • Delete the ruby folders you have on your file system
  • Remove the path to any ruby install folders from your PATH variable

Checking it worked:
Open a Command Prompt and run:

> gem -v
> ruby -v

both of these should result in an error that looks something like this:

'gem' is not recognized as an internal or external command,
operable program or batch file.

2. Installing Ruby

Go here: http://rubyinstaller.org/downloads/ and download Ruby 2.0.0-p648 (x64).

During install, you will be given the option to add the installation to your path, you should do this!
You can check this has worked by opening your command prompt and running:

> ruby -v
ruby 2.0.0p648 (2015-12-16) [x64-mingw32]

3. Installing Rails DevKit

Go here http://rubyinstaller.org/downloads/ again and this time download DevKit-mingw64-64-4.7.2-20130224-1432-sfx.exe

Extract the rails devkit to C:\DevKit
open a Command Prompt and run:

> cd C:\DevKit
> ruby dk.rb init
> ruby dk.rb install

Add the following DevKit folders to your PATH

  • C:\DevKit\bin
  • c:\DevKit\mingw\bin

4. Install mysql connector

Download the connector from here.
Unzip it to c:\mysql-connector

5. Set up svm map (whatever the heck that is)

Download it from here
Unzip it to c:\svm-map
Add the following to your PATH

  • C:\svm-map
  • C:\svm-map\python-mingw-lib

6. Generate 64 bit mysql libraries:

Hop back over to your Command Prompt window, but this time make sure you’re running as administrator

> cd c:\mysql-connector\lib
> gendef.exe libmysql.dll
> dlltool -v --dllname libmysql.dll --def libmysql.def --output-lib libmysql.lib
> copy libmysql.dll C:\Ruby200-x64\bin
> copy libmysql.lib C:\Ruby200-x64\bin

7. Install MySQL community server (if you don’t have it)

Download the latest community server from https://dev.mysql.com/downloads/mysql/

Note: Make sure you get the file with the subtitle “(mysql-installer-web-community-5.7.11.0.msi)”

Open the downloaded installer and pick the “custom” install.
From the list available select the 64 bit version of mysql community server and mysql workbench.

Add MySQL bin directory to your PATH

8. Install MySQL gem

In your Command Prompt window run the following:

gem install mysql2 --platform=ruby -v '0.3.18' -- '--with-mysql-dir="C:/mysql-connector/"'

With this setup the gem should compile and you should ready to install rails and generate your project.
Note: Once you have created your project make sure you update your Gemfile to include the mysql2 gem with the version '~> 0.3.18'