Ruby on Rails Development in Windows via Cygwin

Such a love/hate relationship I have with Linux. It has everything but a great editor (Please no flames from the vi and emacs crowd… those are great editors if you know how to use them properly, but I’d prefer to spend my time learning Ruby and Rails rather learning how to use a text editor).

Windows, on the other hand, lacks much in the Rails development arena, but has been graced with a really great text editor: e-Text Editor, a TextMate clone for Windows. That’s just enough motivation to keep me hacking away at Windows (at least until I get a Mac… then you’re all on your own).

So on to the actual point of this article. Via Cygwin, I have been able to setup up an acceptable Rails development environment for myself in Windows, but since Cygwin can be a little tricky to setup properly, I decided to get some community service hours in by providing a tutorial.

Without further ado (cue drum-roll and general fan-fare)…

Preparation

I had various things such as Ruby and MySQL already set up on my computer before installing Cygwin. The Windows version of Ruby will have to go the way of the dinosaurs once we bring in Cygwin, but it does play well with the Windows version of MySQL. However, I’m not sure if that would still be the case if setup after Cygwin (it probably would, but why take any chances?), so our first step will be to setup MySQL.

Install MySQL for Windows

There’s a few million tutorials on how to do this, so I’m going to cover the bare minimum. If you run into any problems, go make friends with Google.

First, go download the current MySQL binary. At the time of this writing, it’s 5.0.45. You can download either the Essentials or the regular setup executable. I normally go with the regular setup since space is not an issue for me, but I’ll leave the choice to you. Run the installer, and follow the on screen prompts. The install process is pretty self-explanatory, but I’ll cover a bit of minutiae.

You can pretty much use the default settings throughout the install. I normally like to keep all my server apps (apache, mysql, etc) in a common directory for ease. If you’d like to do the same, you can change the install directory when you have the option. After the install finishes, you’ll be asked if you’d like to go ahead and configure MySQL. Yes you do, so proceed with that. Choose the development options throughout (the explanatory paragraphs will mention that this setting or that setting is better for either production or development). When you’re asked if you’d like MySQL to run as a Windows service, tell it yes. When you’re prompted to setup accounts, make sure you disable the anonymous account. You can leave the root password blank, as long as you also turn off remote access. The default Rails database config has blank root passwords, so it’ll save you from having to customize the config every time you start a new app, and it’s just easier to not have to type in a password every time you want to do something with MySQL. The rest of this tutorial will assume the password is blank, though I’ll provide little pointers here and there in case you did set a password.

Install Cygwin

With that out of the way, we can move on to Cygwin. You can grab the setup at the Cygwin website. Look for the “Install or Update Now!” link around the center of the home page. The setup file is a small download, but go ahead and get a pot of coffee ready for brewing, because the rest will be downloaded during install.

Run the setup program, and click next. You’re going to want to stick with the default here (Install from Internet), so click next again. The root directory should default to ‘C:\cygwin’. If not, it’ll make your life a little easier if you change it to that. Leave the two recommended settings checked, and click next again. Now, you’re going to be prompted for a Local Package Directory. I forget what this defaults to, but I like to set it to ‘C:\cygwin\install’ just to keep everything together. Click next again, and you’ll be prompted for your connection settings. For most purposes you should be fine with the default of “Direct Connection”. If that doesn’t work, and you’re unsure of what your settings should be, you can try “Use IE5 Settings”. If you know you’re connecting through a proxy, what are you bothering me for? Just enter your information there.

Time for a new paragraph for this rather tedious install process, but we’re getting close to the end. After clicking next, you’ll be prompted to choose a download site. I always feel a little bit like I’m in Vegas at this point in the install, because it’s basically just a gamble as to which one to pick. FTP is more efficient for file transfer than HTTP, so that weeds it down a bit. Other than that, you can either pick one at random or try to discern one that might be close to you. There’s alot of college servers there, so if you recognize a college that close to your area, go ahead and pick that one.

Okay, I devoted way more attention than was necessary to picking a mirror. Once you’ve rolled the dice, click next. The installer is now going to download a list of available packages and then it’ll present you with a very cryptic screen. Fortunately, we’re going to stick with the defaults for most of these, but we will have to make one small change. Expand the “Devel” category, and then scroll down in the list until you come to the “ruby” package. Click where it says “Skip” to set it to install. And with that, you can click next again and go have a cup of coffee.

When the install completes. You’ll be asked if you’d like to create a Desktop icon or Program Group, give one or both a check, just to simplify your life, and you’re done.

Installing RubyGems

The process from here ought to be familiar for awhile if you’ve ever done any Rails development before. However, we do have a few gotchas to take a look at.

First, download the rubygems archive, which is at version 0.9.4 at the time of this writing. Save it to your desktop to make things easy on yourself. Now, we need to open up a Cygwin command prompt, using one of our handy, dandy shortcuts (either on your desktop or in your start menu… you did remember to do that right?). In the command prompt, you’ll already be in your home directory (i.e. C:\Documents and Settings\[your name]\) which is short-handed as ‘~’ (Tip: you can run ‘cd ~’ at any point to return to your home directory). We need to get to the RubyGems archive we downloaded, so if you followed instructions that’ll mean running ‘cd Desktop’. Now, run the following command (remembering to change the version to whatever is appropriate):

$ tar -pxzf rubygems-0.9.4.tgz

After that finishes it’s work change into the directory that was created:

$ cd rubygems-0.9.4

And run the setup.rb program:

$ ruby setup.rb

Note:

At some point either by now or in the next few steps, you might come across a cryptic error resembling:

ruby: no such file to load -- ubygems (LoadError)

If or when this occurs, you can simply run the following command, and you’ll be good to proceed:

$ unset RUBYOPT

Now back to our regularly scheduled tutorial…

Install Rails

Normally, this is a easy as:

$ gem install rails --include-dependencies

But I have recently noticed, both in Linux and Cygwin that this will return a NoSuchGem error. (Confirmation?). Go ahead and try it the tried and true way first, but if that doesn’t work, continue on.

Go ahead now and download the rails gem, which is at 1.2.3 at the time of this writing. Save it to your desktop, like before. Back in your Cygwin command prompt change back to your desktop (’cd ..’), and run the following command to take care of the dependencies:

$ gem install rake activerecord actionpack actionmailer actionwebservice

You’ll be prompted at points to confirm the download of additional dependencies. Of course, you’ll want to allow those. Grab another cup of coffee, and when it finishes up, run:

$ gem install rails-1.2.3.gem

With that, we now have Rails up and running.

Getting Autotest

Autotest is a very nice feature that will run both your tests and specs automatically based on file changes. It’s actually just a part of a larger suite of testing tools, though, available through the ZenTest gem. Let’s go ahead and get that:

$ gem install zentest

Creating a Rails App

Now that we’ve got all the boring stuff out of the way, let’s get to the interesting part, setting up a Rails app and bootstrapping it for Behavior Driven Development via the RSpec framework. First, change to whatever directory you want to store your Rails apps in (I like to simply use my home directory) and then run:

$ rails myapp

(Where ‘myapp’ is whatever you want to name your app). Then change into the new app’s directory, via:

$ cd myapp

This is where all the fun will happen. But I’ve gotten a little ahead of myself. Before we continue, we need to take care of a few little things. First, we’re going to need both a test and development database for our new app. We can take care of that easily with:

$ mysqladmin -uroot create myapp_development

and

$ mysqladmin -uroot create myapp_test

Now we need to make a slight change to the database.yml config file for our app, due to some Cygwin/Windows compatibility issues. Open the database.yml file (found in the config folder of your app’s directory) in your favorite text editor or wordpad (notepad would not be a good choice due to the Unix format line-endings). Once there, replace every occurrence of ‘localhost’ with ‘127.0.0.1′ (fixes a socket problem with MySQL).

Install the RSpec and Spec:Rails Plugins

There, that’s taken care of. Let’s bootstrap our app with RSpec. (Oddly enough though, we can’t seem to do this in Cygwin. I ran into a very weird issue trying. The plugins appear as if they are installing normally, but afterwards, nothing shows up in the vendors/plugins directory of the app. Odder still, if you try to install the plugins again, it will actually error out, saying that the directories already exist. The only way I could get the plugins installed was to backdoor it through a standard Windows command prompt, but that requires keeping a separate version of Ruby on the Windows side. (Confirmation?) Unfortunately, I have not been able to find another way, so for the meantime, I will go ahead and still put the process here. However, be advised that it may not work.)

$ ruby script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec

and when that finishes:

$ ruby script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec_on_rails

Note:

If you’re feeling adventurous, you can download the trunk versions of these plugins instead. As of this writing, this would give you access to the new Story Runner functionality of RSpec, that has not yet been released. Instead of the above commands run:

$ ruby script/plugin install svn://rubyforge.org/var/svn/rspec/trunk/rspec

and then:

$ ruby script/plugin install svn://rubyforge.org/var/svn/rspec/trunk/rspec_on_rails

Now back to our tutorial, already in progress…

Whichever route you choose above, the next step is to generate the standard RSpec folders and files:

$ ruby script/generate rspec

Generating Some Stuff to Play With

And that’s all there is to it. But for the sake of instruction let’s generate a sample controller and model. This will let us run autotest and see how the whole process went. I’ll be pretending that this app is going to be a blog, but you can go ahead and create something more meaningful here if you want.

The model:

$ ruby script/generate rspec_model post

The controller:

$ ruby script/generate rspec_controller blog

After these two complete, open the 001_create_posts.rb migration file that was created in the apps db/migrate directory. It will initially look like this:

class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts do |t|
end
end

def self.down
drop_table :posts
end
end

Modify it to look like this:

class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts do |t|
t.column :title, :string
t.column :content, :string
t.column :created_at, :datetime
end
end

def self.down
drop_table :posts
end
end

Save and close. Then go back to your Cygwin command prompt and run:

$ rake db:migrate

followed by:

$ rake db:test:clone

With that done, we can run autotest and get a glimpse of the rest of our lives in development

$ autotest
loading autotest/rails_rspec
/usr/bin/ruby -S script/spec -O spec/spec.opts  spec/helpers/blog_helper_spec.rb
 spec/controllers/blog_controller_spec.rb spec/models/post_spec.rb
...

Finished in 0.389 seconds

3 examples, 0 failures

Those three examples are default examples generated for us by RSpec.

Finally…

Take a breather now. You’ve done alot of work, and accomplished quite alot too. I hope this helps those of you who are stuck programming in a Windows environment.

As always, if something is unclear, feel free to ask for further explanation. I do my best to try and write to the lowest common denominator, but I don’t always succeed. Thanks for reading. Now get out there and do some coding.

  • Del.icio.us
  • Digg
  • Facebook
  • Google Bookmark
  • StumbleUpon
  • Technorati
  • To Vivekanand3435
    thank you .....
    really helpful article
  • jaidevs
    Just a suggestion: keep rails and everything associated in a Linux vm, with the code in a shared directory (not samba, but a vmware share). This way you can have your editor and other development tools in the host os, and when you are not developing you can just suspend the vm and it is all shut down and out of sight. As a plus you get the bonus of vm snapshots which are really useful for testing
  • Jim
    Thanks for the great tutorial. Is it possible to run the tail command with this installation.
  • Update: jEdit is *incredible* for Rails development if you add the Ruby syntax modules. I'm using it on Mac OS X; I've had good luck with it on Linux as well, though I've never used it for Rails there.
  • FWIW, Linux does have good text editors available besides [emacs|vi]. My favorite is probably Kate; gedit is also good.
  • @Antun:
    I ran into the same problem. Setting your database to localhost fixes the problem and as far as I know, there's no difference between the two.

    *thinks*

    Unless there is, and I just broke something vital. Oh well.
  • I'm having problems getting the rake command to run with Cygwin + Rails. I've changed all references of localhost to ‘127.0.0.1′, which allows me to run the rails command, and do things like generate models, controllers and a scaffold. However, when I run:

    > rake db:drop:all

    I get the following error:

    > "This task only drops local databases. mp is on a remote host."
    > rake aborted!

    It seems that by setting my database to 127.0.0.1, rake can't modify my database.

    Has anyone else run into this?
  • Ibrahim: Oh, and I forgot to mention another reason. e Text Editor uses Cygwin, already, for some of its more advanced features. There's some things you can't do with e unless your app is in Cygwin as well: running formatted specs from within e, for example.
  • Ibrahim: the main reason to use Cygwin is for the issues discussed in my previous post: Rails, RSpec, Autotest, Redgreen and Snarl: Reasons I Don't Like Windows. If you were able to achieve a Rails development environment, sans Cygwin, where all this works, I'd love to know more about it. Please post back the versions of the various things you're using (Windows, Rails, Autotest, etc), and any steps you might have taken that diverge from just a straight, by-the-book install.
  • ibrahimahmed
    Hi Chris, I m using Rails on Windows, I use ferret, mongrel, zentest...etc and I wasn't forced to install Cygwin for anything needed by Rails development environment. Is Cygwin really required for anything you use in your Rails environment.
  • Chris: Interesting... At what point do things start to go awry for you?

    I just tried to install the RSpec plugins on an edge rails and everything was fine until I tried to generate the default RSpec files and folders (ruby script/generate rspec)... It errors out every time. I even tried the trunk version or RSpec in addition to the current release, with the same results.

    Just to be thorough, I tried generating rspec before freezing edge, but whatever the issue is seems to affect all rspec generators, as I couldn't get the rspec_model or rspec_controller generators to work in edge either. Now, it's possible to manually create your specs without the generators, so I went ahead and tried autotest on some existing specs, which failed as well.

    It would appear that RSpec is completely broken in Edge Rails, at least on Windows. However, if I had to guess, I would say its actually that edge rails runs from the plugin directory that is giving RSpec problems, rather than some new change in Rails itself.

    I might end up adding this tracker, but I'm hesitant since it's asking for a change to support something that is itself experimental in nature. Actually, more likely than not, I'll simply mention it on the RSpec mailing list, and see what people come up with.

    Thanks for noticing this.
  • Chris Finch
    Thanks, I'll be interested to see if it is an actual problem or just somethin silly i am doing
  • Chris: Hmm... I don't think I actually have tried this setup with edge rails, but that's interesting that you've experience problems with that. I'll go back and try this setup again with edge rails, and see if I get a similar result. I'll post anything I find here.
  • Chris Finch
    Chris,
    I use a mac at home but unfortunately have to use Windows XP at work. I stumbled across this when I was trying to recreate my nice mac development environment on Windows.
    I use cygwin and e-text-editor and I tried hard to get rspec working with autotest and snarl. I have come to rest at the solution you provide above - without snarl but hey coloured autotests.
    However, when using edge rails, I have found the above does not work - do you have a similar experience?
  • It's a Windows issue in general. Everything in Rails runs slow on Windows (rake, generate, etc). RSpec in particular has a "spec server" which will load your environment in advance and keep it running so that this doesn't have to be done each time you run a spec. Unfortunately, I have not been able to get it to work along side autotest, at least under Windows.

    However, I am experiencing the same slowdown, so it's only a matter of time before I finally get fed up enough to thoroughly investigate the matter. If you come across something first, though, send it my way.
  • James Knight
    Chris, I ended up installing mysql :)
    Now it's all working, but I find autotest running incredibly slowly? Do you have the same problem, or any fixes for it?
  • Glad it helped you out. Good luck with that ;).
  • James Knight
    Woah, how timely is this? RailsConfEurope had a session on BDD and rspec this morning, and I've spent the evening trying to do exactly what you've outlined here.

    I've made it harder for myself by using sqlite3 rather than mysql, which is complaining about a segmentation fault. It's going to be a long night!
blog comments powered by Disqus