How to install and configure Mercurial on Ubuntu?

Bitbucket allows us to create a private remote repository based on Mercurial. If you don’t care that the source code can be stolen, you can use his service as your distributed revision control tool. However, if your code contains sensitive data and you would like to host a Mercurial on your server, you can follow the steps below to install and configure Mercurial on Ubuntu.

1. Switch user

The commands must be run under superuser access. So remember to write sudo before each command mentioned below.

2. Installation

2.1. Install Mercurial and Apache

apt-get update
apt-get install apache2
apt-get install apache2-utils
apt-get install mercurial

To test if Apache was successfully installed, use command below

wget localhost

Test apache

To test if Mercurial was successfully installed, use command below

hg version

Mercurial version

2.2 Install Keyring

To save a user from entering the password each time he pushes changeset, we can use extension Keyring to store password.

apt-get install python-pip
pip install keyring
pip install mercurial_keyring

3. Configure

3.1 Configure Mercurial

3.1.1 Create main folder

Create folder for hosting all repositories and change owner to www-data user.

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

Create hgweb.cgi to list all repositories over web interface and allow the hgweb.cgi to be able to execute.

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

Edit config of hgweb.cgi as following

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

Create hgweb.config to configure where hgweb.cgi should look for the repositories.

cd /var/hg
nano hgweb.config

Enter following lines

/ = /var/hg/repos/*

3.1.2 Extended configurations

Allow pushing changeset via HTTP (not only HTTPS)

nano /etc/mercurial/hgrc

Add following lines

allow_push = *
push_ssl = false

3.2 Configure Apache

Configure Apache to map hgweb.cgi to a relative path of server.

cd /etc/apache2/site-available
nano 000-default.conf

Add following part right before node

ScriptAlias /hg "/var/hg/hgweb.cgi"
<Location /hg>
AuthType Basic
AuthName "Mercurial repositories"
AuthUserFile /var/hg/hgusers
Require valid-user


Enable CGI module in Apache

a2enmod cgi

Restart Apache

service apache2 restart

3.3 Create users

Create users who can access the repositories

cd /var/hg
htpasswd -mc hgusers user1

Enter the password for the user. For next users, don’t use argument c

cd /var/hg
htpasswd -m hgusers user2

Test if the created user can access the repositories. Use your browser to browse to your_server/hg, you will be asked for username and password.


Mercurial web interface

4. Repository

4.1 Create

cd /var/hg/repos
mkdir hintdesk
cd hintdesk
hg init

4.2 Give permission

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

If you get “Permission denied” at TortoiseHg, then execute these commands

cd /var/hg/repos/hintdesk
chmod -R 777 .hg

5. Configure client

On the client site where you check out source code with TortoisHg, browse to the folder where you have your code. Go to .hg, edit hgrc file. Add following lines

default.schemes = http
default.prefix = your_server/hg
default.username = user_name_defined_in_hgusers

mercurial_keyring = 


Leave a Reply to isaloi Cancel reply

Your email address will not be published. Required fields are marked *