Setting up Rails on Windows with Cygwin

Update: If you're using Rails 2.2, you'll need to perform some extra work to get MySQL working.

Getting Started

I like developing in Ruby on Rails, but I don't own a Mac. I've found that setting up a Rails development environment within Windows can get frustrating and cumbersome at times. I've also found that using Cygwin helps to keep all of the Rails related libraries all in one easy to manage location. OK, enough of the boring stuff, let's open up that command prompt and get started!

But wait! Before we begin, I have to talk about one more thing. One of the frustrating things for me while I was learning Rails was watching all of these Rails screencasts and seeing everyone use Textmate. Textmate is awesome for Rails development, but it's not available for Windows (and will probably never be). Luckily, Alexander Stigsen has been developing a great Textmate app for Windows called "e". I use e (the text editor) almost everyday and it is great for development in Rails (other languages have good support as well). One cool thing is that e relies on Cygwin for some of the bundles, so if you do install and use e, you'll get Cygwin as part of the package.

If you don't want to use e, that's cool too. Just download the Cygwin setup file and follow the same steps (just ignore any asides about setting up e). Let's roll.

Installing Cygwin

First things first and that's installing Cygwin. Either grab the Cygwin standalone setup, or grab and install e and get to the Cygwin setup screens.

Note to e users: e will setup and install Cygwin the first time you run e, not upon installation of e. Also, make sure to select "manual" configuration of Cygwin instead of "automatic".

In the package selection screen, select the libraries that you'll need. Note, to make the package selection easier, hit the "View" button to change from "category" to "full". For reference, here's what I install (the bold ones are the really important ones for Rails):

  • ruby
  • subversion
  • make
  • openssh
  • openssl
  • openssl-devel
  • sqlite
  • git (if you're into that sort of thing)
  • git-completion
  • nano

A few other packages you may want to pick up:

  • gcc
  • gcc-g++
  • ImageMagick
  • libmagick-devel

Once the Cygwin installation is complete, you'll be ready to get Rails up and running.

IMPORTANT NOTE: Every command from this point assumes you are executing through a Cygwin command prompt (Not the Windows default command prompt). Also, I'm assuming you're using nano as your text editor. If not, just replace the command with your favorite one.

Installing Ruby Gems

You'll need to pick up Ruby Gems to be able to get Rails going. Download the latest release, un-tar and run the setup script:

tar -xzf rubygems-1.3.0.tgz
cd rubygems-1.3.0
ruby setup.rb

If you get an error like No such file to load -- ubygems (LoadError), all you need to do is run the following from the command line:


And rerun the Ruby Gems setup.

Install Rails (Finally!)

Once Ruby Gems is all set up, you'll just need to run a few commands to get the rails and associated gems.

gem install rails

I like to install a few helpful gems to get things started:

gem install capistrano mongrel railsmachine gem_plugin daemons rspec

Testing It All Out

Open up a command prompt and type:

rails test_site
cd test_site

Open up a browser and head to http://localhost:3000/. Your rails site should be up and running! Congratulations, you're ready to work with Rails.

Getting Cygwin/Rails to Work With MySQL

By default Rails will use the sqlite database driver, but you may want to develop using MySQL. This can be tricky. In my experience, the best way to install MySQL for use with Rails/Cygwin is to install the Windows version of MySQL (not the MySQL package via Cygwin). Once MySQL is installed, you'll need to setup a config file to tell Cygwin to use instead of localhost when connecting (or you might run into an error saying it couldn't find tmp/mysql.sock).

In Cygwin:

cd /etc/
nano my.cnf

Type the following into the file:


Save the file and you're done. No more mysql.sock errors!

Rails 2.2 Update for MySQL

If you're using Rails 2.2, you'll need to install MySQL and the gem from source.

If you're using MySQL on Windows, I suggest installing the MySQL GUI Tools Suite. They're great tools for managing and querying data from your MySQL databases. Give them a try.

Nice Trick for e

A really nice thing about Textmate is the ability to cd into a rails directory and type:

mate .

Textmate pops open with the current directory set as the current project. Great stuff. Now how about getting that kind of functionality with e and Cygwin? Easy:

cd ~/
nano bash.rc

Add the following to the file:

alias e="cygstart e"

Save and close the file, restart Cywgin and try it out:

e .

Console 2

Cygwin's console is pretty neat, but we can do better. I personally love using Console 2, which gives you the ability to put cygwin into a tabbed environment (among other things such as colors and transparency).

Getting Cygwin to work with Console 2 is pretty straightforward. Just install Console 2 (be sure to get the 2.0 release *), Go to "Settings > Tabs" and click "Add". You can name the tab anything you'd like ("Cygwin" works just fine). The only thing to do is set the shell parameter to:

c:\cygwin\bin\bash --login -i

Save the settings and now you've got a rocking command line app to do all of your Rails work in.

* If you're on Windows XP 64bit, you'll need to download an older version of the 2.0 release. I recommend getting version 2.00b124.

Start Programming Already!

As you can see, it doesn't take too much work to have a great development environment for Rails on a Windows platform. Go start creating the next great app!

One final note: I've set this all up on a Windows XP 32 bit platform, but it should work just fine on 64 bit and Vista (32 or 64 bit). The only thing to watch out for is to install the correct version of Console 2 if you're using Windows XP 64 bit (which you can find in the Console 2 notes above.

Happy programming!

blog comments powered by Disqus