Subversion is an open-source version tracking system. It keeps files in a central repository and provides version control over directories or files. As a developer, you can pull project files from a repository, make changes to them, and push them back again.

Subversion also has a server component that you can use to host your projects. It works much like an ordinary file server.

How to Install Subversion

Subversion is straightforward to install. You can use the appropriate command for your distro to begin:

        # Debian, Ubuntu
sudo apt install subversion apache2 libapache2-mod-svn
 
# CentOS, Fedora, RHEL
sudo dnf install subversion apache2 mod_dav_svn

Note that this command also installs the Apache2 web server. You'll need a web server if you want to access the Subversion repository via HTTP or WebDAV. You can also use HTTPS, you'll just need to install and configure a digital certificate to do so.

Once you've run this command, you can check that subversion installed correctly. You should now be able to run the svn command and you can see the current version using the --version option:

Output from the ”svn version“ command showing version 1.14.1.

Server Configuration With Subversion

If the installation went smoothly, you can now begin working on the repository configuration that you'll use with svn. Of course, you'll need to create a new repository for this. First, create a folder in a root directory to place your repository:

        sudo mkdir /subversion

If you need to access your repository using WebDAV over HTTP, you'll need to give Apache ownership of its directory. Since Apache uses www-data user by default, authorize this user as follows:

        sudo chown www-data:www-data /subversion

You can now switch to the www-data user and start working on your repository. The command you would use to log in as the www-data user is:

        sudo su -s /bin/bash www-data

You are now an Apache user and after this step, you can create your Subversion repository as follows:

        svnadmin create /subversion/myrepo

You now have a Subversion repository. To make this repository a little more secure and to identify its users, the next step is to create a user and set a password for it. To do this, use the following command:

        htpasswd -cmb /subversion/passwd myadmin mypass

According to this command, your username will be myadmin and your password will be mypass. You can change these values to whatever is appropriate for your circumstances.

Your Subversion repository is now ready to use. At this stage, you can exit the www-data user by using the exit command.

subversion-repository-create-and-user-password

Now you can send a desired project or file to your repo using the command below:

        sudo svn import <your-project-address> file:///subversion/myrepo -m "First Commit"

This command uses the import parameter to push everything in your project folder to your Subversion repository. While doing this, you need to add the commit message with the -m parameter.

subversion-repo-first-commit-with-import-and-commit

Using Access Methods in Subversion

You may have noticed that you are using the file:// protocol to add a project to your repository. This is just one of several network protocols you can use to access an svn repository. You can also use the WebDAV protocol over HTTP or HTTPS, or subversion’s custom svn protocol.

Direct Access to the Repository

As you’ve seen, you can use the file:// protocol to access a local repository. Here’s how you can checkout a local repo to your current directory:

        svn co file:///subversion/myrepo
    

When you check out the repo, svn displays a list of the files it contains:

svn-repo-control-with-checkout-command

Accessing Using WebDAV

To integrate your Subversion repository with Apache, you'll need to configure some settings.

For the first step, activate the dav, dav_svn, and dav_fs modules using the following commands:

        sudo a2enmod dav dav_fs dav_svn

After activating the required modules, you can now edit the /etc/apache2/mods-enabled/dav_svn.conf file. Create the file if it doesn't already exist, and change its contents as follows:

        <Location /subversion>
  DAV svn
  SVNPath /subversion/myrepo
  AuthType Basic
  AuthName "Subversion Repository"
  AuthUserFile /subversion/passwd
  Require valid-user
</Location>

The AuthUserFile setting references the file that you created earlier using htpasswd. Adding it here will grant repository access to all users named in the file, provided they authenticate with a valid password.

Once you've created and saved this file, restart the Apache2 service with the following command:

        sudo systemctl restart apache2.service

Now, when you open a web browser and go to http://localhost/subversion/, you'll see the contents of your repository. When you go to this address, it will ask for your username and password:

subversion-repo-access-with-webdav-username-and-password-screen

Login with the username (e.g. myadmin) and password (mypass) that you set up earlier. You can access your Subversion repository using WebDAV after entering the username and password:

subversion-repo-content-after-login-page

Accessing With SSL and WebDAV

The https:// protocol has almost the same configuration as the http:// protocol, with just a few important differences. To give an example, the .conf file you need to use during configuration is different. You also need to install a digital certificate. Because as you know, this is the working logic of the SSL encryption method.

You can create a digital certificate yourself, or you can install one issued by a competent authority. Assuming you have installed a digital certificate, what you need to do is simple.

The difference in this step is the changes you will make in the configuration file. Below is a sample configuration file for SSL and WebDAV usage:

        <Location /subversion>
    DAV On
    SSLRequireSSL
    Options None
    AuthType Basic
    AuthName "Subversion Repository"
    AuthUserFile /subversion/passwd
    Require valid-user
</Location>

As you can see, the difference between https:// and http:// protocols is not that much in terms of changes in the configuration file. After these changes, you'll need to restart the apache2 service. Run systemctl restart apache2.service to do so.

Why Use Subversion?

As a version tracker, Subversion remembers every change made to files and directories. It lets you access old versions of software or documents you're working with and find their differences. This makes it easier to manage projects, especially if your development team is large or distributed.

There are many version control systems for Linux aside from Subversion, which you may want to consider.