Skip to content

Instantly share code, notes, and snippets.

@bendavis78
Created July 22, 2012 02:14
Show Gist options
  • Save bendavis78/3157948 to your computer and use it in GitHub Desktop.
Save bendavis78/3157948 to your computer and use it in GitHub Desktop.
Installing pygit2 on ubuntu

Download and build libgit2

$ cd ~/.local/src/
$ git clone git://github.com/libgit2/libgit2.git 
$ cd libgit2
$ mkdir build && cd build
$ cmake ..
$ cmake --build .

If debugging, use the following cmake commands instead:

$ cmake -DCMAKE_BUILD_TYPE=Debug ..
$ cmake --build .

Install libgit2

$ sudo cmake --build . --target install

This will install libgit2 in /usr/local. In my case, libgit2's install command did not call ldconfig, so I had to do it manually:

$ sudo ldconfig

Confirm that libgit2 can be found:

$ ldconfig -p | grep libgit

This should output the paths to libgit2.so.0 and libgit2.so.

Install pygit2

The pypi distribution of pygit2 should now work. So you can use easy_install or pip.

@mattmcla
Copy link

It's worth noting git clone git://github.com/libgit2/libgit2.git checks out the development branch. Be sure to specifically check out master in step #2 or the pygit2 build may fail.

git clone -b master git://github.com/libgit2/libgit2.git

@intuited
Copy link

Some may prefer to use software such as GNU stow to allow for a relatively litter-free installation. In this case (assuming that you are stowing the install under /usr/local):

Change the line

$ cmake ..

to

$ cmake -DCMAKE_INSTALL_PREFIX=/usr/local/stow/libgit2 ..

and, after you've run the --target install command, do

$ sudo stow -d /usr/local/stow libgit2

This will create symlinks in /usr/local/{include,lib} linking into /usr/local/stow/libgit2.

I also had to run sudo ldconfig afterwards.

@demonkoryu
Copy link

@mattmcla That finally got pygit2 to build. Thanks everybody!

@sandepp123
Copy link

Nicely written. Thanks for your effort it made my pygit running. But @mattmcia rightly pointed out about the master version problem.

@thecjharries
Copy link

thecjharries commented Nov 25, 2017

I ran into this today trying to install pygit2 on a newer version of Ubuntu. Running ldconfig alone wasn't enough to get it to work. The official docs provide a couple of extra steps that were necessary for me to get it to work:

# Export LIBGIT2, if it's not there already
$ env | grep LIBGIT2
# Or wherever you plan to stash it; /usr/local is the default on Ubuntu
$ export LIBGIT2=/usr/local
# Same steps as above, different order
$ wget https://github.com/libgit2/libgit2/archive/v0.26.0.tar.gz
$ tar xzf v0.26.0.tar.gz
$ cd libgit2-0.26.0/
# The install prefix is important
$ cmake . -DCMAKE_INSTALL_PREFIX=$LIBGIT2
$ make
$ sudo make install
# This is what finally made it work for me
$ export LDFLAGS="-Wl,-rpath='$LIBGIT2/lib',--enable-new-dtags $LDFLAGS"
$ pip install pygit2
$ python -c 'import pygit2'

@quater
Copy link

quater commented Apr 13, 2018

Thanks @thecjharries, this worked for me with pygit2 version 0.26.4.

For everyone else who follows thecjharries instructions, you have to explicitly specify the version with pip install pygit2==0.26.4 due to latest version being higher and beyond 0.26.

If you adapt this same set of commands with version 0.27, ensure that libssl-dev is installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment