Installing Trac with Subversion on Ubuntu
The goal of this tutorial is to demostrate how to setup a Subversion ↔ Trac enviroment on Ubuntu 10.04. A MySQL database and Subversion Python bindings are going to be used. Please note that only general instructions are provided, and it's asummed that you have basic knowledge on Linux administration.
Note: for a full installation tutorial on Trac, please read TracInstall
- Installing the software and its dependencies
- Automatic reference to the SVN changesets in Trac tickets
Installing the software and its dependencies
In order to get Trac and Subversion installed, you will need to get a few packages listed below. Also, make sure your system is updated.
sudo apt-get install apache2 libapache2-mod-python python-setuptools python-genshi mysql-server python-mysqldb
Installing Subversion (SVN) is pretty straight forward. Just run:
sudo apt-get install subversion
There are different ways of installing Trac. But since this tutorial is focused on Ubuntu, you'll do the Ubuntu way:
sudo apt-get install trac
This part is maybe the most important section on this tutorial. You'll learn how to syncronize Trac and Subversion in order to be able to see on your Trac Project website what's going on in your repository and also how to automate some tasks.
Creating the project
You may already know how to do this, but let's make a review just in case.
Create a directory to store the SVN projects:
sudo mkdir /var/lib/svn
Create a the project directory:
sudo mkdir /var/lib/svn/YourProject
Use svnadmin to create a project in the previously created folder:
sudo svnadmin create /var/lib/svn/YourProject
In order to perform some changes to the project, Trac needs write access to it:
sudo chown -R www-data /var/lib/svn/YourProject
Start the Subversion server (or use your preferred method):
sudo svnserve -d
Setup the MySQL database
Before configuring your new Trac project, you'll need to setup the MySQL database.
Log into MySQL database, using the root credentials you've setup during the installation:
mysql -u root -p
Once logged in, create the database for Trac:
CREATE DATABASE trac DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
Now create the username which Trac is going to use to connect to the database:
GRANT ALL ON trac.* TO trac@localhost IDENTIFIED BY 'yourpassword';
You can now exit the MySQL command line.
Initiate the enviroment
Let's create a directory to contain all the Trac project (just like we did for SVN projects).
sudo mkdir /var/lib/trac
Create a directory where to store the Trac project in:
sudo mkdir /var/lib/trac/YourProject
As you did for Subversion, change the ownership of the project files to Apache's user www-data:
sudo chown -R www-data:www-data /var/lib/trac/YourProject
Use trac-admin to create the new project:
sudo trac-admin /var/lib/trac/YourProject initenv
Also you will need to fill in some information, like the project name. It will ask you for the MySQL connection string. Input the following (according to the way we did setup the MySQL database in the step 2.2).
Pay attention to the question about the location of the Subversion project. Enter the path as discussed before:
If the MySQL default engine wasn't InnoDB, you might need to convert the tables tha trac-admin has just created. Issue the following within your MySQL client:
USE trac; \ ALTER TABLE `attachment` ENGINE = InnoDB; \ ALTER TABLE `auth_cookie` ENGINE = InnoDB; \ ALTER TABLE `cache` ENGINE = InnoDB; \ ALTER TABLE `component` ENGINE = InnoDB; \ ALTER TABLE `enum` ENGINE = InnoDB; \ ALTER TABLE `milestone` ENGINE = InnoDB; \ ALTER TABLE `node_change` ENGINE = InnoDB; \ ALTER TABLE `permission` ENGINE = InnoDB; \ ALTER TABLE `report` ENGINE = InnoDB; \ ALTER TABLE `repository` ENGINE = InnoDB; \ ALTER TABLE `revision` ENGINE = InnoDB; \ ALTER TABLE `session` ENGINE = InnoDB; \ ALTER TABLE `session_attribute` ENGINE = InnoDB; \ ALTER TABLE `system` ENGINE = InnoDB; \ ALTER TABLE `ticket` ENGINE = InnoDB; \ ALTER TABLE `ticket_change` ENGINE = InnoDB; \ ALTER TABLE `ticket_custom` ENGINE = InnoDB; \ ALTER TABLE `version` ENGINE = InnoDB; \ ALTER TABLE `wiki` ENGINE = InnoDB;
Note: For more information about the Explicit Syncronization method, please read TracRepositoryAdmin#ExplicitSync
First, edit the trac.ini file, located in /var/lib/trac/YourProject/conf/. Modify the "repository_sync_per_request" directive and set it to an empty value.
Now you need to create the Subversion hooks. First, create the post-commit hook the in /var/lib/svn/YourProject/hooks/ directory, with the following content:
#!/bin/sh export PYTHON_EGG_CACHE="/path/to/cache/dir" /usr/bin/trac-admin /var/lib/trac/YourProject changeset added "$1" "$2"
Make sure you give the script execution perms:
sudo chmod +x /var/lib/svn/YourProject/hooks/post-commit
Another important hook script to have is post-revprop-change. The procedures are similiar as above, but the script is slightly different:
#!/bin/sh export PYTHON_EGG_CACHE="/path/to/cache/dir" /usr/bin/trac-admin /var/lib/trac/YourProject changeset modified "$1" "$2"
Again, make sure you give exec perms to the script.
Now Trac will be notified about changes you make to your repository and will make them availables in the timeline.
Set up Trac handling
Apache needs to know how to handle Trac. Use the following block to set up Trac handling:
<Location /projects> #set up Trac handling SetHandler mod_python PythonHandler trac.web.modpython_frontend PythonOption TracEnvParentDir /var/lib/trac PythonOption TracUriRoot /projects </Location>
Note that it's pointing to the main Trac directory. It means it will expose all of your Trac projects. You may need to apply this configuration to Apache:
sudo a2ensite trac
In order to allow users to log in Trac, you will need the following section in your Apache configuration:
<LocationMatch "/projects/[^/]+/login"> AuthType Basic AuthName "Trac" AuthUserFile /var/lib/trac/.htpasswd Require valid-user </LocationMatch>
You should now create the .htpasswd file, with an admin user:
sudo htpasswd -c .htpasswd admin
Go ahead and restart Apache:
sudo /etc/init.d/apache2 restart
Finally, you must grant admin rights to the user you've just created:
sudo trac-admin /var/lib/trac/YourProject permission add admin TRAC_ADMIN
Automatic reference to the SVN changesets in Trac tickets
Something useful is to reference tickets on your commits. That way you can keep a better track of them and also easly access them from the timeline.
Make sure you have the following line in your trac.ini configuration file:
[components] tracopt.ticket.commit_updater.* = enabled
Now, whenever you commit some change related to a ticket, use Refs #tn to reference this changeset in #tn ticket. For example:
svn commit -m "Refs #123 - added this and that"
In order to mark a ticket as fixed, use the following:
svn commit -m "Fixes #123 - Removed an infinite loop which was causing the application to freeze"