View Article

Setting up Rails on Windows with Cygwin

October 2nd, 2008 by matt

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:

wget http://rubyforge.org/frs/download.php/43985/rubygems-1.3.0.tgz
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:

unset RUBYOPT

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
./script/server

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 127.0.0.1 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:

[client]
host=127.0.0.1
[mysqld]
host=127.0.0.1

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!

Tags: , , ,

Posted in Guides | comments (rss)

You can leave a response, or trackback from your own site.

Responses

  • Doug on November 26th, 2008 at 2:07 pm

    I found this site after having trouble running mysql in an cygwin rxvt window (it was hanging, a well known issue with cygwin and non-cygwin command line apps). I’m on 64bit Vista. I had trouble running the latest Console 2 (the text was black on a black background and no obvious way to fix this), and download the one you suggest. I got errors about missing msvcp71.dll and msvcr71.dll. I downloaded those from http://www.dll-files.com and it works great. I’m able to run mysql from Console 2. Thanks.

  • Doug on November 26th, 2008 at 2:21 pm

    I just found that someone compile Console 2 with a 64bit Vista fix:
    http://groke.org/blog/?p=75

  • Reuben on December 7th, 2008 at 3:02 am

    I find Cygwin a wonderful way to get administrative things done in Windows and keep the old *nix skills sharpened while working in that environment (admittedly where all my web development is done from, given I’ve been a classic ASP programmer for 8 years, now moving to Rails).

    As I am also a Linux systems administrator keeping high production systems online 24/7. I’ve been using Cygwin since its onslaught days in the late 90’s -well before Redhat purchased Cygnus Solutions. Naturally, when I discovered that I could get Rails working in Cygwin, my love of BASH on Windows evermore continues.

    Gratefully, this site and it’s Cygwin/Rails insight, has been additionally resourceful in my dedicating a video tutorial (one of a series of of videos to come) viewable on YouTube: http://www.youtube.com/watch?v=mWHdxN86n0Q titled “Ruby on Rails + Cygwin + Windows Vista”.

    I am working on a main website location where all the series of videos tutorials, and articles will reside (http://rails-dev.officialnameserver.net/rorcygwinvista), I will be be providing full credits and links to resources that have provided me with the much needed information on getting this going, and phaseshiftllc.com, and the articles contained here regarding Cygwin and Rails on Windows is going to be one on the top of that list.

    Respectfully,

    -Reuben :)+<

  • Laurie on May 11th, 2009 at 8:53 pm

    I saw a similar post about this on another blog so it just goes to show you that it is a small world after all (I hate that Disney ride).

  • Installing MySQL gem on Windows & cygwin for Rails | Phase Shift on June 17th, 2009 at 11:06 pm

    [...] socket, but we want it to use the server we installed in Windows (outside of cygwin). Check out the Getting Cygwin/Rails to work with MySQL section of our previous [...]

  • Nicholas on July 7th, 2009 at 8:59 am

    Hi. I read a few of your other posts and wanted to know if you would be interested in exchanging blogroll links?

  • Russ on August 8th, 2009 at 12:45 pm

    Using this tutorial I was able to get cygwin up and running with rails and Mysql on Windows 7! It was much easier than Vista… 7 is awesome

  • roger rubygems on August 13th, 2009 at 11:01 am

    I had a different fix for the ‘could not connect to /tmp/mysql.sock’ problem, but similar

    http://betterlogic.com/roger/?p=1829
    Thanks!
    -r

  • bed on September 22nd, 2009 at 2:27 pm

    Great Site.Thank for suggesion.

  • ssaidwho on December 8th, 2009 at 2:53 pm

    I found that in addition to the my.cnf file change, I also needed to set host: 127.0.0.1 in the database.yml file. It was set to localhost previously

Leave a Reply




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">