Setting up Ruby on Rails with MySQL in Windows

Update 2009-02-17 Why go through all the trouble of installing all this in your Windows machine, if you're not going to actually develop there? turnkeylinux.org has a great collection of ready-made virtual machines, including one for Ruby on Rails. See Using Turnkey Linux Rails with Capistrano for an overview.

Having worked with Ruby on Rails for the last months on Mac OS X, I've gotten quite spoiled: Textmate is the best editor I've ever worked with, and the Apple Developer Tools just come with everything you need.

Not so much on Windows, unfortunately. That needs a manual... Here it is!

I will take you trough the steps necessary for getting your Ruby on Rails development up and running. I assume we don't have an application yet, so we will generate one.

Part 1: Installing Ruby

Pick the recommended one-click installer from the Ruby download page.

Recommended Ruby installer

And indeed, the one-click installer is pretty much one-click. It might be good to select the installation of RubyGems.

Ruby installer - check RubyGems

For ease of use, it's a good idea to add the Ruby binaries to your path. To do so, open up your system properties, and set the Path an Environment Variable.

Advanced -> Environment Variables: Add ';c:\ruby\bin' to the end of the Path variable" width="300" height="193" />

Add ';c:\ruby\bin' (without the quotes, including the semicolon) to the end of the Path variable. You can now try to open a shell (Start -> Run -> "cmd"), and type 'ruby'. If nothing happens (just a blinking cursor), you're all set.

Part 2: Installing MySQL

At the time of writing, MySQL 5.1 is the most recent version. However, for some reason this doesn't work as well as it should. In stead, we revert to MySQL 5.0.

On the MySQL download page, find the Older Releases. You'll have to do a little digging, but in the end you will find an installer version for you platform (remember that there are both 32- and 64-bit versions).

In the installer, I would keep all settings to the defaults (you're only using this Windows machine for your development, and not for your deployment, right?). Sit through the installer and the ads, and you're ready to start configuring your MySQL installation.

In the configuration, choose the Standard configuration...

MySQL installation - 1: choose 'Standard Configuration'

...add the MySQL binaries to the Windows path...

MySQL installation - 2: check 'Include Bin Directory In Windows Path'

...and do not modify the default security settings. This gives you a root account without a password; again, this is good for local development only.

MySQL installation - 3: Uncheck 'Modify Security Settings'

That was the easy part. Because of some reason unknown to me, the link between Rails and MySQL requires some tinkering (not as much as installing MySQL on Snow Leopard, though).

You will have to copy the file libmysql.dll from the MySQL binaries directory (usually at c:\Program files\MySQL\mysql\bin) to c:\Ruby\bin. In my situation, executing copy "c:\Program Files\MySQL\MySQL Server 5.0\bin\libmySQL.dll" c:\ruby\bin from a shell did the trick.

Part 3: Adding Rails

Open up a shell, and run

gem install rails

Go make yourself some coffee, this might take a while.

While your at it, also update your version of RubyGems; the one that's packaged with the one-click installer is likely not the newest one.

gem update --system

Next, install the MySQL gem; this is necessary for actually connecting to the database.

gem install mysql

So far for the installation, now it's time to create an application.

Part 4: MyFirstRails

Open up a shell, and navigate to a directory where you want to start developing your application. Assuming that you have a 'development' directory,

cd c:\development

A quick note on that location: it is my experience that it's best to keep your working space as close to your root (c:) as possible. Paths on Windows are limited to 255 characters, and you directory structure might start getting pretty deep pretty quickly, so you don't want to start of at 'c:\Documents and Settings\Joe Doe\Projects\Rails\Development'.

Generating the application

From this location, run

rails -d mysql MyFirstRails

You will see a whole bunch of logging flying by, and a new directory MyFirstRails has been created. Let's go there...

cd MyFirstRails

Creating the database

For our pet project, the default database settings are fine. If you ever need to tweak those, you can find them in config\database.yml.

Now, we create the database.

rake db:create

You will not see much output, but believe me, stuff is happening here.

You're now ready to start you application!

ruby script/server

Go to

http://localhost:3000

or, as is necessary on some systems,

http://127.0.0.1:3000

and behold your application.

My First Rails - The result of going to 'http://localhost:3000'

But... Someone already built me an application!

Ah, you lucky man, you got someone to get you started. Alright, that simplifies some part of this. Move your project into a directory, navigate to it using a shell, and run

rake db:create

to create the database,

rake db:schema:load

to create the necessary tables in the database. If you are updating an exisiting application, use db:migrate instead. Next, use

rake db:fixtures:load

to get the testing data into the database (or perhaps rake spec:db:fixtures:load if your project uses RSpec, which it should), and fire it all up with

ruby script/server

And now?

There, you're all set to start developing your awesome world-taking web application! If you don't know where to go now, I have some pointers.

  • I really enjoyed working with Agile Web Development with Rails (amazon bol) to get me started. I haven't seen the 2009 edition I'm linking to here, but judging from the 2007 edition, it should be good. The 2009 edition should be on its way to my home at this very moment.
  • If you've passed the basics, and looking for the best way to work with Rails, try The Rails Way (amazon bol). It's more in-depth and less accessible, but the content is awesome. It's a shame this book is from 2006, because Rails has changed quite a lot since then. Still, highly recommended.
  • I also recommend watching some Railscasts. These are roughly 10-minute presentations showing you features of Rails, the best way to do something, or it showscases cool plugins. For me, they always show me that things can be done way easier.

I have promised myself to write an in-depth review about both the two books above. Feel free to remind me.