I’ve been asked several times how I have my Ruby on Rails development environment set up. The most frequently misunderstood aspect of a Rails development environment is that you do not have to have your Subversion repository physically located on the same server as your deployed application. The only requirement is that all the pieces of your development environment puzzle need to be able to access one another over the internet. So, here’s how I have myself setup. It works on both linux (Ubuntu) and Windows XP.

My primary development platform is Ubuntu Linux but I have also set up Apache and Subversion on Windows XP. So I’ll try to make Windows related notes along the way. My deployment platform is always a Linux based system – usually Ubuntu Server. In order for Capistrano to work it’s magic in needs to make a bunch of symbolic links which is pretty much a Linux feature. While you can develop on any platform you want, make sure you are deploying to a Linux server.

Setting Up Subversion and Apache

This is a description of how to set up your development environment web server and Subversion repository. Again, it is not necessary to have your Subversion repository on your live server. My repository resides on my development machine. I actually have a domain name assigned to my development machine so that it can be accessed over the internet. I use zoneedit.com to manage the DNS. It’s a pretty ugly site but it’s free, easy to use, and gets the job done. If you don’t want to bother with setting up zoneedit, simply put the IP address of your development machine in your server’s /etc/hosts file. Or, just use your IP address and forget about setting up a domain for your development machine altogether.

Make sure you have Subversion installed either by installing from Apt if you are on Ubuntu or downloading and installing Subversion if you are on Windows. I’m on Ubuntu so I do the following:

sudo apt-get install subversion subversion-tools

Once subversion is installed, pick a place on your file system where you would like to store your repository. You might choose a location like /home/username/svn or E:\svn on Windows. You want to make sure that Apache is able to write to the folders on your file system. Change to the directory where you want your repository and then create it as follows:

cd /home/username/svn
svnadmin create application-name
sudo chgrp -R www-data application-name
sudo chmod -R g+w application-name

Now you have an empty repository. You cannot access it over the web yet as we have not yet set up Apache. You can however access the repository over your file system. So if you aren’t interested in accessing your repository through Apache, you can stop here and start using Subversion. You won’t be able to deploy your application over the internet though. So you will probably want to configure Apache to work with Subversion.

On Ubuntu you will need to install libapache2-svn which is the package that provides the mod_dav_svn and mod_authz_svn modules for the Apache 2.2 web server. These modules provide Subversion’s WebDAV server backend, to serve repositories over the http and https protocols.

sudo apt-get install libapache2-svn

If you are on Windows, make sure you get the apache the appropriate apache module. Then enable the following modules.

sudo a2enmod dav
sudo a2enmod dav_svn

If you are on windows you will probably need to enable these modules by editing your apache configuration file. You will need to add the following lines to httpd.conf

file: httpd.conf

LoadModule dav_module modules/mod_dav.so
LoadModule dav_fs_module modules/mod_dav_fs.so
LoadModule dav_svn_module modules/mod_dav_svn.so

Next, in your Apache configuration file you need to tell Apache where your repository will be physically located. I’m using Apache 2.2 so I go into /etc/apache2/sites-available/default and setup the location for my repository as follows:

file: /etc/apache2/sites-available/default

<Location /path/to/repository>
DAV svn
SVNPath /absolute/path/to/repository
AuthType Basic
AuthName “Subversion Repository”
AuthUserFile /absolute/path/to/password/file.txt
Require valid-user

There are three different paths in the above configuration. The first path is the path in the Location tag. This is the path in your URL after your domain. For example, you might have the URL http://www.my-dev-site.com/svn/application-name in which case you would have <Location /svn/application-name> Note, that your url path does not have to map exactly on to the path on your file system.

The second path is the SVNPath which is the absolute path to the location of your Subversion repository. In our example above we would use /home/username/svn/application-name

The third path is the absolute path to you htpasswd file. We haven’t created this yet. You may choose to put it in your home directory at /home/username/svnpasswords.txt This is the file that will house the usernames and passwords for accessing your Subversion repository through Apache. Here is how to create you password file.

htpasswd -c /home/username/svnpasswords.txt

If you want more than one user, run the same command again but leave off the -c. If you don’t leave off the -c then a new password file will be created and all your previous entries will be lost.

Restart Apache and you have a functioning Subversion repository.

Setup the standard Subversion directories as follows:

svn mkdir http://www.my-dev-site.com/svn/application-name/trunk -m “creating trunk”
svn mkdir http://www.my-dev-site.com/svn/application-name/tags -m “creating tags”
svn mkdir http://www.my-dev-site.com/svn/application-name/branches -m “creating branches”

Importing Ruby on Rails Project Into Subversion

Now we need to set up version control for our Ruby on Rails project. Let’s assume we’ve already got a Ruby on Rails project we want to add to Subversion. If you don’t, now is the time to create one.

cd /home/username/rails
rails application-name

Make sure you change directories so that you are in the root directory of your Rails application. Then checkout the trunk of your new repository.

svn co http://www.-my-dev-site.com/svn/application-name/trunk .

Now add all of our project files to your working copy.

svn add *

We don’t really want ALL of our files under version control. For example, we don’t want to store session files, log files, etc. So tell Subversion to ignore a few files.

svn propset svn:ignore ”*” log/
svn revert log/*
svn propset svn:ignore ”*” tmp/
svn revert tmp/*
svn commit -m “initial project import”
svn update

Once you are generally comfortable with setting up a project like this manually, you may want to remove some of the hassle by storing an example “starter” rails project in Subversion. When you are ready for a new project, copy the example project out of Subversion. This is an excellent tip and a clever time saver so you can stop using the rails command.

Adding Multiple Files To Subversion

During the course of developing your Ruby on Rails application you will most likely have the need to add a bunch of files to version control. For example, you may have run a few generators and now you need all those files in Subversion. If you want to add all of the “unversioned” files to Subversion, here’s a command that will do that for you.

svn st | grep ”\?” | awk ‘{print $2}’ | xargs svn add

This series of commands runs the status command in subversion, pipes the output to grep where we only select the lines that start with ? (indicating that the file is not in Subversion yet). The output from grep is piped to awk where we pluck out the file name of each unversioned file and pass that file name to svn add. It’s a great time saver.