Setting up your own Mercurial server

11/25/13, 6:10:51 PM

This tutorial assumes that you have a machine with Ubuntu (Server) installed. I prefer the LTS version, as it is for 12.04 in this case.

On your fresh Ubuntu server machine all the further steps are made as root-user. I prefer the root-user flavor instead of typing sudo before every statement though someone may find this a security issue - I don't. If you want to use sudo instead, please "think" yourself a "sudo" before every line of shell code here. Don't use it in editors (the text will say it if you now have to use an editor).

Notice: the webserver runs without SSL, so if you want to setup SSL on apache refer to the apache manuals.

First we have to install the required packages from the repos, as we do with:

apt-get update && apt-get install apache2 apache2-doc mercurial

After that we prepare some directories for using with Mercurial and Apache webserver:

cd /var/
mkdir hg
mkdir hg/repos
chown -R www-data:www-data hg/repos

After that we have to configure some files for the Apache Webserver to be used with the Mercurial binary:

cd /var/hg
sudo cp /usr/share/doc/mercurial/examples/hgweb.cgi .
sudo chmod a+x hgweb.cgi

The named file does not exist, so we have to create it e.g. with

nano hgweb.cgi

and put the following line in it (editor):

config = "/var/hg/hgweb.config"

Save and close the file.

Repeat this with the command

nano hgweb.config

and paste these lines into it and save it (editor):

/var/hg/repos = /var/hg/repos

The next part is to tell Apache how to handle the URL to your HG script by issuing the following commands:

cd /etc/apache2/sites-available
nano default

Place this into the file right before the line </VirtualHost> and save it (editor):

ScriptAlias /hg “/var/hg/hgweb.cgi”

<Location /hg>
AuthType Basic
AuthName “Mercurial repositories”
AuthUserFile /var/hg/hgusers
Require valid-user

Now the time has come to restart the web server with

apache2ctl restart

You may want to create an admin user to manage the repositories. Mercirual got it's own users so we create a new admin user and set a password with:

cd /var/hg
htpasswd -mc hgusers admin

You have to type the new password twice. The "c" is used to inititally create the hgusers file. After that you just have to create other users with the same command line except the "c" switch like that:

htpasswd -m hgusers oliver

Open your browser now and navigate to your webserver at http://yourhost/hg. You should be prompted to authenticate. Use the admin user and password you just created before. then you will see the repository startpage of the Mercurial server.

To allow pushing updates to Mercurial via HTTP (not only HTTPS) open up the file


in an editor and paste these lines and save it:

allow_push = *
push_ssl = false

That's it, you're done!

Now let's create the first test repository:

cd /var/hg/repos
mkdir test
chown www-data:www-data test -R
cd test
hg init

The hg init command issues all the stuff that has to be done to use that folder as a Mercurial repo. If you refresh your browser to open up the index of your HG-server, you will see that repo instantly. Now you can connect with e.g. SourceTree to manage and control the repo. Remember: issue the commands above to create a new repository as the server is not able to be controlled externally to create them.

Original footage found on, slightly adapted due errors/missing things.

Latest comments to this post

Oliver Busse wrote on 15.01.2015, 16:25

Staun, I amfraid I can't help you with this as it seems to be a security issue/problem that should be answered on Stackoverflow I guess.

 Link to this comment
Staun wrote on 15.01.2015, 16:08

Excellent guide, working perfect except that when I try to push my changes I get this error:

abort: destination does not support push


Any idea why?

 Link to this comment

Leave a comment right here