Mercurial on Ubuntu behind a proxy

I tried out Mercurial on Ubuntu 8.10, and was immediately defeated by my corporate proxy:


$ hg clone https://wave-protocol.googlecode.com/hg/ wave-protocol
abort: HTTP Error 501: Not Implemented

Here is how I fixed this.

Step 1: Configure Mercurial proxy settings

Make the following file look like this:


$ cat /etc/mercurial/hgrc
[http_proxy]
host=yourproxyhost:yourproxyport

See man hgrc for other options including name and password.

You can verify the proxy is configured using the --debug flag:


$ hg --debug clone https://wave-protocol.googlecode.com/hg/ wave-protocol
using https://wave-protocol.googlecode.com/hg/
proxying through yourproxyhost:yourproxyport
sending between command
abort: HTTP Error 501: Not Implemented

The proxy server is configured, but as you can see, it still fails.

Step 2: Install Python 2.6

Mercurial is written in Python. Earlier versions of Python, like 2.4 which is still the default in Ubuntu 8.10, have a bug in which urllib/urllib2 doesn't use proxy correctly. You must upgrade to Python 2.6 or better.

Python 2.6 is not in the Ubuntu 8.10 repositories, so the easiest thing to do is upgrade to Ubuntu 9.04.

Make sure you also install the python2.6-dev package, as it is needed in the next step.


$ sudo apt-get install python2.6 python2.6-dev

Step 3: Install Mercurial 1.3.1

Sadly, the version of Mercurial in Ubuntu 9.04 is 1.1.2, which still cannot use the proxy correctly it seems. So, uninstall mercurial, then upgrade to 1.3.1:


$ sudo apt-get remove mercurial
$ wget http://mercurial.selenic.com/release/mercurial-1.3.1.tar.gz
$ tar -xzf mercurial-1.3.1.tar.gz
$ cd mercurial-1.3.1
$ sudo make install

Now you might still have a problem like this:


$ hg debuginstall
abort: couldn't find mercurial libraries in [/usr/local/bin ... ]
(check your install and PYTHONPATH)

Put the following in your /etc/bash.bashrc or ~/.bashrc file:


export PYTHONPATH=/usr/local/lib/python2.6/site-packages

Now:


$ hg debuginstall
Checking encoding (UTF-8)...
Checking extensions...
Checking templates...
Checking patch...
Checking commit editor...
Checking username...
No username found, using '...' instead
(specify a username in your .hgrc file)
No problems detected

And the pull should work.


$ hg clone https://wave-protocol.googlecode.com/hg/ wave-protocol
requesting all changes
adding changesets
adding manifests
adding file changes
added 97 changesets with 608 changes to 322 files
updating working directory
312 files updated, 0 files merged, 0 files removed, 0 files unresolved

References: http://mercurial.selenic.com/bts/issue967