# Building M2Crypto from source

This page contains instructions on how to build M2Crypto from source.

Note there is also a patched Ubuntu version available from the Downloads site.

To build M2Crypto from source install the following packages on a Debian-based system:

    sudo apt-get install build-essential python-dev swig libssl-dev

Get the M2Crypto source from http://chandlerproject.org/bin/view/Projects/MeTooCrypto

    M2Crypto-0.21.1.tar.gz

Get the m2crypto-fixes.patch from https://drive.google.com/folderview?id=0B1wsLqFoT7i2aW5mWXNDX1NtTnc&usp=sharing or https://bugzilla.osafoundation.org/show_bug.cgi?id=13005

Apply the patch

    patch -u -p0 < ../m2crypto-fixes.patch

Make the following changes to M2Crypto so that it works on your system. See the sections below for more information:

Make sure __m2crypto.so is put in the right location, change the line 130 in setup.py from:

    m2crypto = Extension(name = 'M2Crypto.__m2crypto',

to:

    m2crypto = Extension(name = '__m2crypto',

Remove the lines that contain the SSLv2_method from:

    SWIG/_ssl.i

This should be lines 51 and 52.

To build M2Crypto:

    python setup.py build

To install M2Crypto:

    sudo python setup.py install

To make sure M2Crypto works on your system start the interactive Python shell and run:

    import M2Crypto

This should complete without any error, otherwise see the Troubleshooting section below.

To exit the interactive Python shell run:

    exit()

If you need to rebuild M2Crypto because you ran into one of the issues mentioned below make sure to clean-up the M2Crypto build directory before running "setup.py build" by running:

    sudo rm -rf build/ dist/

## Troubleshooting

### No module named __m2crypto

When I run:

    import M2Crypto

From the interactive Python shell I get:

    ImportError: No module named __m2crypto

Make sure __m2crypto.so is put in the right location, change the line 130 in setup.py from:

    m2crypto = Extension(name = 'M2Crypto.__m2crypto',

to:

    m2crypto = Extension(name = '__m2crypto',

### Undefined symbol: SSLv2_method

When I run:

    import M2Crypto

From the interactive Python shell I get:

    ImportError: /usr/local/lib/python2.7/dist-packages/M2Crypto-0.21.1-py2.7-linux-x86_64.exx/__m2crypto.so: undefined symbol: SSLv2_method

This is because M2Crypto tries to link the SSLv2_method function from OpenSSL libcrypto, but the function is defined conditionally by OPENSSL_NO_SSL2 in:

    /usr/include/openssl/ssl.h

Remove the lines that contain the SSLv2_method from:

    SWIG/_ssl.i

This should be lines 51 and 52.