Skip to content

Instantly share code, notes, and snippets.

@olivier-m
Last active November 9, 2017 05:56
Show Gist options
  • Save olivier-m/5755638 to your computer and use it in GitHub Desktop.
Save olivier-m/5755638 to your computer and use it in GitHub Desktop.
Install pygit2 in a Python VirtualEnv
#!/bin/sh
set -e
if [ "${VIRTUAL_ENV}" = "" ]; then
echo "Error: Not in a virtual env"
exit 1
fi
OS=$(uname -s)
set -u
SRC="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/_tmp"
TARGET=${VIRTUAL_ENV}
test -d ${SRC} || mkdir ${SRC}
cd ${SRC}
test -d libgit2 || git clone git://github.com/libgit2/libgit2.git
test -d pygit2 || git clone git://github.com/libgit2/pygit2.git
# Building libgit2
cd ${SRC}/libgit2
git checkout v0.19.0
rm -rf build && mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=${TARGET}
cmake --build . --target install
# Building pygit2
cd ${SRC}/pygit2
git checkout v0.19.0
LIBGIT2=${TARGET} python setup.py build_ext -R ${TARGET}/lib
python setup.py build
if [ "${OS}" = "Darwin" ]; then
install_name_tool -add_rpath ${TARGET}/lib $(find build -name '_pygit2.so')
install_name_tool -change libgit2.0.dylib @rpath/libgit2.0.dylib $(find build -name '_pygit2.so')
fi
python setup.py install
rm -rf ${SRC}
Copy link

ghost commented Jan 20, 2014

Bella! One small improvement would be to place the library version number in a var at the top of the script.

@yonglin
Copy link

yonglin commented Oct 13, 2014

nice! thanks a lot

@naiquevin
Copy link

I am trying to use this script to install pygit2 and libgit2 v0.22.0 on OSX for which I changed the version on line 25 and 33 and libgit2.0.dylib on line 39 to libgit2.22.dylib. Got the following error:

HEAD is now at 1263084... Get ready to release v0.22.0
running build_ext
Traceback (most recent call last):
  File "setup.py", line 197, in <module>
    cmdclass=cmdclass)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/core.py", line 152, in setup
    dist.run_commands()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 971, in run_command
    cmd_obj.ensure_finalized()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/cmd.py", line 109, in ensure_finalized
    self.finalize_options()
  File "/Users/naiquevin/.virtualenvs/autorel/lib/python2.7/site-packages/setuptools/command/build_ext.py", line 105, in finalize_options
    _build_ext.finalize_options(self)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/command/build_ext.py", line 140, in finalize_options
    ('plat_name', 'plat_name'),
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/cmd.py", line 298, in set_undefined_options
    src_cmd_obj.ensure_finalized()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/cmd.py", line 109, in ensure_finalized
    self.finalize_options()
  File "setup.py", line 103, in finalize_options
    ffi, C = get_ffi()
  File "pygit2/_utils.py", line 101, in get_ffi
    include_dirs=[libgit2_include], library_dirs=[libgit2_lib])
  File "/Users/naiquevin/helpshift/code/autorelease/_tmp/pygit2/.eggs/cffi-0.9.2-py2.7-macosx-10.9-intel.egg/cffi/api.py", line 367, in verify
    lib = self.verifier.load_library()
  File "/Users/naiquevin/helpshift/code/autorelease/_tmp/pygit2/.eggs/cffi-0.9.2-py2.7-macosx-10.9-intel.egg/cffi/verifier.py", line 97, in load_library
    return self._load_library()
  File "/Users/naiquevin/helpshift/code/autorelease/_tmp/pygit2/.eggs/cffi-0.9.2-py2.7-macosx-10.9-intel.egg/cffi/verifier.py", line 207, in _load_library
    return self._vengine.load_library()
  File "/Users/naiquevin/helpshift/code/autorelease/_tmp/pygit2/.eggs/cffi-0.9.2-py2.7-macosx-10.9-intel.egg/cffi/vengine_cpy.py", line 153, in load_library
    raise ffiplatform.VerificationError(error)
cffi.ffiplatform.VerificationError: importing '/Users/naiquevin/helpshift/code/autorelease/_tmp/pygit2/pygit2/__pycache__/pygit2_cffi_93ecb84x5470904.so': dlopen(/Users/naiquevin/helpshift/code/autoreleas
e/_tmp/pygit2/pygit2/__pycache__/pygit2_cffi_93ecb84x5470904.so, 2): Library not loaded: libgit2.22.dylib
  Referenced from: /Users/naiquevin/helpshift/code/autorelease/_tmp/pygit2/pygit2/__pycache__/pygit2_cffi_93ecb84x5470904.so
  Reason: image not found

Any idea about this?

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