Ubuntu 12 - Set Up An SVN Server

Introduction

This tutorial aims to set up an SVN service on an Ubuntu 12.04 server, which will be accessed remotely with the SVN protocol. E.g you would put something like svn://my-site.com/my-project-name instead of http://my-site.com/my-project.

    Install all the necessary packages and dependencies:
    sudo apt-get install subversion
    Create a directory for where you want to store your repos. Personally, I use /home/svn
    sudo mkdir $SVN-ROOT-DIRECTORY
    Create a repository
    cd $SVN-ROOT-DIRECTORY
    sudo svnadmin create "my-new-project"
    Start the SVN service and specify the root directory where your repos are located:
    svnserve -d -r $SVN-ROOT-DIRECTORY
    To automatically start the service on boot so that you dont have to run the previous step every time you reboot:
    crontab -e
    Add the following line to the bottom of the file:
    @reboot /usr/bin/svnserve -d --root $SVN-ROOT-DIRECTORY
    Thats it! Now you can checkout the repository by entering the following command on your computer:
    svn checkout svn://$SERVER-IP-ADDRESS/my-new-project
    Obviously if you create a subdirectory in future and then put a repo in there, it would be something like this:
    svn checkout svn://$SERVER-IP-ADDRESS/my-sub-directory/my-new-project

    Optional Configurations

    Obviously, just doing the steps above isn't very safe. Depending on how you set up, almost anyone might be able to checkout/manipulate your code! I will now show you various ways to protect your code.

    Use a Firewall To Block Access

    You could use a VPN to allow you to have a 'static ip', then set up a firewall to block everything except traffic from that IP.

    sudo apt-get install ufw
    sudo ufw allow from xxx.xxx.xxx.xxx
    sudo ufw default deny
    sudo ufw enable

    Creating SVN Users

    The following steps will set it up so that only users you create can checkout and update to your repository. I have left out lots of other possible configurations, as this is the basic one people will probably want. Just note that it is possible to set it up so that anonymous users can checkout your repo, but not commit to it, should you desire (if so just read the comments in the files)

    Navigate to the configuration files inside your repository
    cd $SVN-ROOT-DIRECTORY/$MY-REPO-NAME/conf
    Edit the authz file to be something like this (only these lines need to be uncommented):
    [/]
    $authenticated = rw
    This will set up the repo so that only authenticated users can check it out or commit to it.
    Edit the passwd file and add users (plaintext password). E.g This added the 'stuart' user with the password 'test'
    [users]
    stuart=test
    Edit the svnserve file so that the daemon will make use of the previous two files we just changed.
    vim svnserve.conf
    Make sure these lines are uncommented and have these values (helps if you use vim, then uncommented lines are green):
    anon-access = none
    auth-access = write
    password-db = passwd
    authz-db = authz

References

13 comments:

  1. Thank you for posting this, it allowed me to get a svn server up and running fast.

    ReplyDelete
    Replies
    1. Thanks for commenting. It's always nice to see that I have helped someone out.

      Delete
  2. Hi i followed the steps, i can access the repo, i created a trunk and branch, how can i know the location of my committed files? will i see that under the repo i created? thanks

    ReplyDelete
    Replies
    1. I'm afraid that I don't understand your comment but will guess.

      As far as I know, you cannot "see" your commited files from navigating the files in your SVN repo on the server. They are stored in a compressed form. However that is not to say that they are "safe" as I believe a hacker could still access your files without knowing your password, if he has access to the repo.

      You will only know the location of the repo, and can then check out subfolders/files of the repo if desired based on already knowing what is in your repo.
      E.g. if my repo was at svn://svn.programster.com/my-repo then if I just wanted to check out the trunk I would perform
      svn checkout svn://svn.programster.com/my-repo/trunk etc.

      Does that help?

      Delete
  3. I followed this up to the optional part but I could not connect to the repository remotely. I'm probably writing the url wrong but is this not the proper way to access from a windows machine? something like: http://example.com/SVN-ROOT-DIRECTORY or svn://example.com/SVN-ROOT-DIRECTORY .

    ReplyDelete
    Replies
    1. The address should be svn://example.com/path/to/your/repo
      Please make sure that if you set up the firewall, you changed the x's to your client's ip here (not the server): sudo ufw allow from xxx.xxx.xxx.xxx
      Also make sure your client IP hasn't changed (this happens if you are connecting from a home connection and not through a VPN or business connection which tend to be static).

      You may have gotten your path wrong. e.g. if your repo is at /home/user/svn/my-repo, make sure your svnserve serves up from /home/user/svn which would mean the url is svn://example.com/my-repo

      Delete
  4. You did not mentioned how to configure SVN so that it will be access from Browser url as well

    ReplyDelete
    Replies
    1. Thanks Subodh, I'm sure others are probably thinking the same thing and I just never realized it until you mentioned it. I'll be sure to make another post on how to enable SVN access through http and https.

      The reason that I did not include it in this tutorial (and will be linking to the new post rather than including it) is because it is beyond the scope, just as setting up phpmyadmin would be on a tutorial about how to set install a mysql server. e.g. both are separate tools that allow a user to interface with the service in a new/different way.

      Delete
    2. Agree but it will be grate for a reader if he/she find whole information one place and maxmim people thinking basic thing should be one place ..Just a suggestion rest on you

      Delete
  5. Hi, thanks for writing this! I'm new to server stuff so this is super helpful. I followed each step and my svn root is under /home/svn but I’m having an issue with step 5. I'm unable to checkout, it just hangs. When I cancel the checkout, I’m getting this message: Unable to connect to a repository at URL.

    The funny thing is, that I was able to get this working yesterday, but when I set my domain I was no longer able to checkout. I pretty much started from scratch today. I’m running Ubuntu 14.04 with a LAMP stack installed and my domain is set.

    Is there something I’m missing?

    Thanks!

    ReplyDelete
    Replies
    1. Hey Gabrielle,
      I'll start by saying that I don't recommend running svn under root, but that you create a new subuser and create the repos in their home folder.

      However, for the purposes of my response, I am going to act as if you have a folder in /root called my_repos and your repo is called my_first_repo.

      I hope that you created the repo already by going to /root/my_repos and then running the command svnadmin create my_first_repo.
      Ensure your svn service is running correctly with:
      sudo killall svnserve
      svnserve -d -r /root/my_repos

      Now try to checkout on your client machine with:
      svn checkout svn://xxx.xxx.xxx.xxx/my_first_repo
      where xxx.xxx.xxx.xxx represents the server's IP. Note that you do not put the full path to the repo, only relative path to the repository from the folder that you designated as the repositories folder.

      I hope that helps.
      Progger

      Delete
  6. Hi, I had actually set it up with a subuser and it is under /home, not /root. Anyway, I was able to fix it. The problem wasn't really the setup, I had to allow the proper port in my firewall. :)

    Thanks!

    ReplyDelete
    Replies
    1. No worries! Hopefully your comments will help others who experience the same issue.

      Delete