How to build Mercurial on Windows

This how-to is intended to give some hints about how to build Mercurial on Windows from the Mercurial sources. If you don't intend to fiddle with the sources, then simply use a pre-built binary package for Windows (for example [:TortoiseHg], or, see [:BinaryPackages]). If you want to build an installer or Py2exe distribution, see BuildingWindowsInstaller.

Mercurial is mostly programmed in Python ( The Python sources don't need a compilation step, but a few Mercurial modules are programmed in C: base85.c, bdiff.c, diffhelpers.c, mpatch.c and osutil.c ( These must be compiled with a C-compiler.

The steps below worked on a Windows XP SP2, that had a Microsoft Visual C++ installed.

Install Python 2.5, available from

Default install path is C:\Python25. Best leave it at that.

Get the Mercurial sources (assuming you already have a working "hg" installed, see for example

>cd C:\tmp\repos
>hg clone mercurial

See [:DeveloperRepos] for other common repositories (the most other interesting one is the [:CrewRepository]).

Mercurial uses the "Python Distribution Utilities (Distutils)" install process, which also covers building of the extension modules (the files written in C-Code). See in the Python docs at

The first step is doing a python build at the top level directory of the Mercurial package:

>cd mercurial
>python build
running build
running build_py
creating build
creating build\lib.win32-2.5
creating build\lib.win32-2.5\mercurial
copying mercurial\ -> build\lib.win32-2.5\mercurial
copying mercurial\ -> build\lib.win32-2.5\mercurial
copying mercurial\ -> build\lib.win32-2.5\mercurial
building 'mercurial.mpatch' extension
creating build\temp.win32-2.5
creating build\temp.win32-2.5\Release
creating build\temp.win32-2.5\Release\mercurial
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin\cl.exe /c /nologo /Ox /MD /W3 /GX /DNDEBUG -IC:\Python25\include -IC:\Python25\PC /Tcmercurial/mpatch.c /Fobuild\temp.win32-2.5\Release\mercurial/mpatch.obj mpatch.c
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin\link.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:C:\Python25\libs /LIBPATH:C:\Python25\PCBuild /EXPORT:initmpatch build\temp.win32-2.5\Release\mercurial/mpatch.obj /OUT:build\lib.win32-2.5\mercurial\mpatch.pyd /IMPLIB:build\temp.win32-2.5\Release\mercurial\mpatch.lib
   Creating library build\temp.win32-2.5\Release\mercurial\mpatch.lib and object build\temp.win32-2.5\Release\mercurial\mpatch.exp
running build_scripts
creating build\scripts-2.5
copying and adjusting hg -> build\scripts-2.5

In the above example, Python found and used an installed Microsoft C compiler. The C-source files are compiled and linked into windows dll files using the file extension ".pyd". See also "[ Extending and Embedding the Python Interpreter]" and the more specific "[ Building C and C++ Extensions on Windows]" in the Python docs.

1. Global install

Execute python install

> python install
running install
running build
running build_py
running build_ext
running build_scripts
running install_lib
copying build\lib.win32-2.5\hgext\ -> C:\Python25\Lib\site-packages\hgext
copying build\lib.win32-2.5\mercurial\ -> C:\Python25\Lib\site-packages\mercurial
byte-compiling C:\Python25\Lib\site-packages\hgext\ to acl.pyc
byte-compiling C:\Python25\Lib\site-packages\mercurial\ to ancestor.pyc
running install_scripts
copying build\scripts-2.5\hg -> C:\Python25\Scripts
running install_data
creating C:\Python25\Lib\site-packages\mercurial\templates
copying templates\changelog.tmpl -> C:\Python25\Lib\site-packages\mercurial\templates
copying templates\changelogentry.tmpl -> C:\Python25\Lib\site-packages\mercurial\templates
running install_egg_info
Writing C:\Python25\Lib\site-packages\mercurial-fb259a3572e9-py2.5.egg-info

This installs hg in C:\Python25\Scripts:

> python C:\Python25\Scripts\hg version
Mercurial Distributed SCM (version fb259a3572e9)

Copyright (C) 2005-2008 Matt Mackall <> and others
This is free software; see the source for copying conditions. There is NO

2. Local install for testing/development

If you want to have a locally ready to run Mercurial (for example for testing your patches, etc.), you can use the following batch file (named it "build-win32-2.5.cmd"):

del mercurial\*.pyd
rd /S /Q build
python build
copy build\lib.win32-2.5\mercurial\*.pyd mercurial

Run it from the working directory of your cloned repo. This will copy the compiled C modules (the *.pyd files) into the mercurial directory, giving you a ready to run local hg command.

CategoryContributing CategoryWindows CategoryHowTo