51
pip install m2crypto

Generates the following output:

building 'M2Crypto.__m2crypto' extension
swigging SWIG/_m2crypto.i to SWIG/_m2crypto_wrap.c
swig -python -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -I/usr/include -I/usr/include/openssl -includeall -modern -o SWIG/_m2crypto_wrap.c SWIG/_m2crypto.i
SWIG/_m2crypto.i:30: Error: Unable to find 'openssl/opensslv.h'
SWIG/_m2crypto.i:33: Error: Unable to find 'openssl/safestack.h'
SWIG/_evp.i:12: Error: Unable to find 'openssl/opensslconf.h'
SWIG/_ec.i:7: Error: Unable to find 'openssl/opensslconf.h'
error: command 'swig' failed with exit status 1

I've run:

brew install swig
therealmarv
  • 3,692
  • 4
  • 24
  • 42
user5275692
  • 603
  • 1
  • 6
  • 11

7 Answers7

178

I wanted a nicer way without installing manually and using only Homebrew (which also does not link openssl by default). Also using pip was a requirement. This seems to work with newest m2crypto 0.22.5. I also tested it once with m2crypto 0.22.3 and seems also to work. The OpenSSL version here is 1.0.2d:

brew install openssl
brew install swig

Finally install m2crypto on macOS in your Bash. It is a long command but it changes SWIG and clang environment variables only during pip install so that m2crypto will get all OpenSSL requirements:

env LDFLAGS="-L$(brew --prefix openssl)/lib" \
CFLAGS="-I$(brew --prefix openssl)/include" \
SWIG_FEATURES="-cpperraswarn -includeall -I$(brew --prefix openssl)/include" \
pip install m2crypto

btw. the last command also works if you use e.g. a requirements.txt.

Update:
Additional also the command for fish shell users...

env LDFLAGS="-L"(brew --prefix openssl)"/lib" \
CFLAGS="-I"(brew --prefix openssl)"/include" \
SWIG_FEATURES="-cpperraswarn -includeall -I"(brew --prefix openssl)"/include" \
pip install m2crypto
therealmarv
  • 3,692
  • 4
  • 24
  • 42
11

thanks to therealmarv env flags i was able to get this to work with the macports version of openssl/swig, this is what i did:

sudo port install openssl
sudo port install swig
sudo port install swig-python

then use therealmarv lines but replace "$(brew --prefix openssl)" with the dir from macports which should be "/opt/local"

sudo env LDFLAGS="-L/opt/local/lib" \
CFLAGS="-I/opt/local/include" \
SWIG_FEATURES="-cpperraswarn -includeall -I/opt/local/include" \
pip install M2Crypto
hjjiang
  • 111
  • 2
5

I just went through a lot of pain getting this working in El Capitan. Here is what I had to do:

Install OpenSSL (you have to use an older version, m2crypto will not compile otherwise)

curl -O https://www.openssl.org/source/openssl-0.9.8zg.tar.gz
tar -xvzf openssl-0.9.8zg.tar.gz
cd openssl-0.9.8zg
./Configure --prefix=/usr/local darwin64-x86_64-cc
make && make test
sudo make install

Install m2crypto

git clone https://github.com/martinpaljak/M2Crypto.git    
cd M2Crypto
python setup.py build build_ext --openssl=/usr/local
sudo python setup.py install build_ext --openssl=/usr/local

AFAIK it is installed... I still have to do some testing though.

joebarbere
  • 10,055
  • 1
  • 16
  • 14
  • Also, this repo seems to be more active: https://gitlab.com/m2crypto/m2crypto.git I think I'll try installing this version as well. – joebarbere Oct 11 '15 at 04:07
  • I was able to use hjjiang answer from above with OpenSSL 1.0.2e – domoarigato Jan 13 '16 at 19:19
  • 2
    `python setup.py build build_ext --openssl=/usr/local/opt/openssl` `sudo python setup.py install build_ext --openssl=/usr/local/opt/openssl` works for me – J.Y Han Mar 24 '16 at 07:29
4

Got exact same issue on Ubuntu 18.04 LTS, while trying to execute

pip install M2Crypto==0.24.0

Did the following to get rid of it:

sudo apt-get install swig
sudo apt-get install libssl1.0-dev
1

This fixed my problem (Python 2.7):

export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages
gmm
  • 326
  • 3
  • 7
0

macos v10.15.6
m2crypto v0.35.6
pip v20

Solution by therealmarv works, If the program does not take effect, you can try to upgrade your brew packages. Before the solution.

brew update              # update homebrew self
brew upgrade             # upgrade all 
Euruson
  • 1
  • 3
-11

sudo apt-get install python-m2crypto

gaokun
  • 1