Tutorial Home‎ > ‎

Setting up Subversion in Linux

Introduction

Subversion is a free/open-source version control system, usually used for keep track of changes to source code. Subversion manages files and directories over time and increments revisions made to the file system. A tree of files is placed into a central repository. This repository is much like an ordinary file server, except that it remembers every change ever made to your files and directories. This allows you to recover older versions of your files and examine the history of how the data has changed.

Subversion allows access to its repository via the network, which allows it to be used by people on different computers at the same time. This ability for various people to modify and manage the same set of data from their respective locations fosters collaboration on projects. This can allow progress on a project to occur more quickly by creating work that is versioned. By using revision history, contributors can easily revert or undo changes in an error or incorrect change is made to the data repository.

Subversion also saves space, because it stores deltas from one version to the next. Users can make a change to a versioned file, it needs only enough extra space to store the changes rather than a complete second copy of individual files. Also, unlike most CVS systems, delta storage on Subversion also applies to binary files as well as text files.

Subversion will work on most modern distribututions of BSD or GNU/Linux, but for our article here we will be using a Debian based distribution of GNU/Linux. The distribution(s) of choice here at Section6 happen to be

Installing Subversion

Installing Subversion is generally pretty easy. As stated earlier, most modern Linux or BSD distributions include a package management system for administrators to add and remove programs from the system. Chances are that a Subversion package exists for your distribution. In most cases, we would also want to run a web server (such as Apache 2) as a frontend for easy user access to a Subversion repository. We can easily get these packages installed on our distributions with the package manager that ships with the operating system. For example, on Debian GNU/Linux we would simply add the packages with apt-get:

root@host# apt-get install subversion
Reading package lists... Done
Building dependency tree... Done
The following extra packages will be installed:
 db4.2-util libsvn0
Suggested packages:
 subversion-tools
The following NEW packages will be installed:
 db4.2-util libsvn0 subversion
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 1821kB of archives.
After unpacking 6279kB of additional disk space will be used.
Do you want to continue [Y/n]?

We would of course reply by hitting the Y key, thus installing the subversion package. Subsequently it would be a good idea to install the subversion-tools and apache2 packages via the same method id they are not installed automatically

root@host# apt-get install apache2
root@host# apt-get install subversion-tools
root@hotst apt-get install libsvn0

For more infomration about installing Subversion for your distribution, please consult the documentation on your particular operating system package management options Once you have Subversion installed, it's time to create a repository. Let us say we want to create a repository for source files in the /src directory on this machine. We would first make sure that a directory exists called /src:

root@host# mkdir /src

Then we would issue the svnadmin command that creates the structure within this directory that allows us to use it as a repository for files:

root@host# svnadmin create /src

This should create a directory structure that looks similar to the following:

root@host# ls -al /cvs
total 36
drwxrwxrwx   7 root root 4096 2006-05-19 17:18 .
drwxrwxrwx  14 root root 4096 2006-05-19 17:18 ..
drwxrwxrwx   2 root root 4096 2006-05-19 17:18 conf
drwxrwxrwx   2 root root 4096 2006-05-19 17:18 dav
drwxrwxrwx   2 root root 4096 2006-05-19 17:18 db
-rwxrwxrwx   1 root root    2 2006-05-19 17:18 format
drwxrwxrwx   2 root root 4096 2006-05-19 17:18 hooks
drwxrwxrwx   2 root root 4096 2006-05-19 17:18 locks
-rwxrwxrwx   1 root root  379 2006-05-19 17:18 README.txt

Now that we have a single repository created, we need to allow remote access to it so that users can import their files. Let use start off with a simple example by allowing all users to import/export from the repository.

Warning! Allowing full access to everyone on a repository is obviously not the most secure method of managing your Subversion repository. Setting account, permissions, and access methods is covered later in this document

We will start off by editing the svnserve.conf for this repository, and allowing guest accounts write access to the repository. In the repository's conf directory, a file called svnserve.conf exists. We should edit the file to look like the following example:

### This file controls the configuration of the svnserve daemon, if you
### use it to allow access to this repository.  (If you only allow
### access through http: and/or file: URLs, then this file is
### irrelevant.)
### Visit http://subversion.tigris.org/ for more information.
[general]
### These options control access to the repository for unauthenticated
### and authenticated users.  Valid values are "write", "read",
### and "none".  The sample settings below are the defaults.
anon-access = write
### auth-access = write
### The password-db option controls the location of the password
### database file.  Unless you specify a path starting with a /,
### the file's location is relative to the conf directory.
### The format of the password database is similar to this file.
### It contains one section labelled [users]. The name and
### password for each user follow, one account per line. The
### format is
### USERNAME = PASSWORD
### Please note that both the user name and password are case
### sensitive. There is no default for the password file.
### password-db = passwd
### This option specifies the authentication realm of the repository.
### If two repositories have the same authentication realm, they should
### have the same password database, and vice versa.  The default realm
### is repository's uuid.
### realm = My First Repository

Next we need to start the Subversion Server. On some systems (like Debian GNU/Linux) this requires us to write an init script so that the server starts up every time the system is rebooted. We would do this by creating a file called svnserve in the /etc/init.d directory, and then editing it to look like the following example file:

#! /bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/svnserve
NAME=svnserve
DESC="SVN Repository Server Daemon"
test -x $DAEMON || exit 0
OPTIONS="-d -r /cvs/src"
# Get lsb functions
#. /lib/lsb/init-functions
. /etc/default/rcS
start() {
       echo "Starting $DESC... "
       #       echo "Starting $DESC: "
       if ! start-stop-daemon --start --quiet --oknodo --exec $DAEMON -- $OPTIONS
>/dev/null 2>&1; then
               status=$?
               echo $status
               return $status
       fi
       log_end_msg 0
       return 0
}
case "$1" in
 start)
       start
       ;;
 stop)
       echo "Stopping $DESC: "
       start-stop-daemon --stop --quiet --oknodo --exec $DAEMON
       echo $?
       ;;
 restart|force-reload)
       $0 stop
       sleep 1
       start
       #echo "$NAME."
       ;;
 *)
       N=/etc/init.d/$NAME
       echo "Usage: $N {start|stop|restart|force-reload}" >&2
       exit 1
       ;;
esac
exit 0

This script will also allow us to manually start and stop the svnserver service whenever neeeded (The svnserve daemon is the Subversion server). Note: be sure and change the OPTIONS line to reflect the location of your repository; in our previous example this location was /src. Once this file is created and edited to look like the previous example, we need to chmod the script to be executable and also set it to startup at boot time:

root@host# chmod +x /etc/init.d/svnserve
root@host# update-rc.d svnserve defaults

Now simply start the service by issuing the following command:

root@host# /etc/init.d/svnserve start
Starting SVN Repository Server Daemon...

We should now be able to connect to the Subversion repository remotely so that we can import files

Locally importing files into the repository

Now we need to import our desired files into the newly created repository. This might take some careful planning, as the entire directory structure whill be imported into the repository.

There are a couple of ways to import data into the repositories. One way would be to import the data locally into the repository (Remote importing is coverred in the next section). For example, if the directory structure that we want to import exists on the server itself under a directory such as /home/dev/code, then we would issue the svn import locally on the Subversion server:

root@host# svn import /home/dev/code file://src

When we run the import command, Subversion opens an editor and asks us for a log message. This log message is optional and whatever text we enter will be associated with the newly created repository revision and can be seen by examining the repository history logs. We would ususally enter something brief, such as "imported src". As soon as we save the log message and leave the editor, Subversion performs the import and outputs something like the following example:

root@host# svn import /home/dev/code file://src
Adding      /home/dev/code/file1.txt
Adding      /home/dev/code/file2.txt
Adding      /home/dev/code/file3.txt
Committed revision 1.

We can now safely remove the original /home/dev/code directory if we wanted to, and then perform a checkout in order to stay current with the repository by using the svn checkout command:

root@host# rm -rf /home/dev/code
root@host# svn checkout file://src /home/dev/src

This will create a src under /home/dev and checkout the files from the repository. Now all work can be done in the /home/dev/src directory and simply commited and merged back up to the repository when desired

Remotely importing files into the repository

This is the real advantage of using a CVS system like Subversion. We can remotely import, checkout, and merge files into the repository using a Subversion client for our platform.

Using the UNIX-based Subversion client

On most Linux or BSD systems, again this is a package we can install with our package management system. As discussed earlier, the subversion and subversion-tools would install the required binaries we would need. The only difference in between local and remote operations of performing importing is the access convention. Where as we used the file:// convention earlier to access the repository locally, we would now use the svn:// convention to perform this action:

root@host# svn import /home/dev/code svn://server/src
Adding     		/home/dev/code/file1.txt
Adding         	/home/dev/code/file2.txt
Adding         	/home/dev/code/file3.txt
Committed revision 1.

Using the Windows-based Subversion client

Tigris.Org, the developers of Subversion, have provided an intuitive and easy to use Windows-based Subverion client. This client installs and integrates as a part of the Windows Explorer interface and allows us to easily and almost transparently check-in and check-out files from a subversion repository.

This client is a free download from the following location:

http://tortoisesvn.tigris.org/

When using the Windows client, be sure and use the same access convetion of :

svn://server/repository

Where server is the resolvable servername of the remote Subversion server, and repostiory is of course the name of the repository.

Using Subversion with Apache Web-Dav

The Apache Webdav module will aloow us to access the repository via the HTTP protocol method. This is handy for administrator who do not wish top open and forward additional ports such as the SVN protocol access method. From a basic Apache installation, getting WebDAV to work is fairly simple. First, you need to make sure that mod_dav and mod_dav_svn are being loaded in the configuration file for Apache:

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

Note: In Debian GNU/Linux, the mod_dav is automatically loaded and a part of the Apache2 web server. One simply needs to install the mod_dev_svn module with apt:

root@host# apt-get install libapache2-svn

This will install and automatically configure the module for the Apache2 web server Next we will need to set up a Location directive to point to the repository. For example, if we want the repository to be referenced with the URL http://server/repository, and the repository is located in /src, you could use the following location directive in the apache2.conf file:

<Location /src>
  DAV svn
  SVNPath /home/dev/code
  AuthType None
</Location>

Afterwards, simply restart eh Apache2 webserver:

root@host# /etc/init.d/apache2 restart

This will allow us to access the repository remotely with the Subversion client via the webdav protocol. We simply would substitute the svn:// access method with http://. Note: Each additional repository that is setup and configured needs its own location directive in the Apache2 configuration file.

Note: If we want to use Autoversioning with the webdav protocol, we must enable it in Apache. To do this we must add the SVNAutoversioning option to your <Location> directive for the Subversion repository.

With Autoversioning via webdav, we can mount the remote repository and work out of it as if it were a locally mounted file system. This can be a handy feature for some Integrated Delopement Environments in handling files and projects. New versions of files are created every time a file is saved. That way, we have a complete save history of the files without worrying about whether we've done a commit recently. We also add files to the repository merely by creating them, and can do copies, moves or deletes with the standard filesystem commands. Furthermore, if we access the repository from multiple computers, we always know that we're accessing the most recent version without worrying about the last time we updated.

Of course, webdav mounted autoversioning does have its downsides. For one, it requires a reasonably fast network connection to the computer that is serving the repository, so it may not be practical for a laptop that might frequently be used away from work on a slower network connection; although if we have access to a network connection back to the server, it is always possible to copy files to our local hard drive, edit them and then copy them back to the repository. Another downside to autoversioning is that we can access only the most recent repository revision. If we want to access older revisions of files, we have to download them locally by check out the repository again

Basic Subversion client commands

svn help

Get more help on Subversion commands

svn add

Add files to the repository

svn status

Status of files in the repository

svn update

Updates your local directory with the repository

svn commit

Commit changes from you local directory to the repository