Installing python-glpk on OS X 10.8


I have been using PuLP for linear optimisation. By default, on OS X, the library used the packaged COIN command line program. This tends to be slow if you are many optimisations as I was doing. Luckily PuLP can handle other optimisers as well like GLPK or paid ones like CPLEX or GUROBI.

So I decided to install GLPK on my computer. This proved harded than I initially imagined.

Installing GLPK Homebrew and swig:

brew install homebrew/science/glpk
brew install swig

Now to install python bindings for GLPK. I tried out two binding for python (more details here):

  • Python-GLPK
  • PyGLPK

I didn’t have any success with PyGLPK. So I stuck with Python-GLPK.

My instructions:

Firstly install ‘ply’

pip install ply

Then download the code and unzip it.

wget http://www.dcc.fc.up.pt/~jpp/code/python-glpk/python-glpk_0.4.43.orig.tar.gz
tar xf python-glpk_0.4.43.orig.tar.gz
cd python-glpk-0.4.43/src/

In swig/Makefile replace line number 1 to

PYVERS := 'python2.7'

In folder ‘src’, run

make

I got these errors and I ignored them.

make -C swig all
swig -python  glpkpi.i
./glpk.h:916: Warning 314: 'in' is a python keyword, renaming to '_in'
sed -i 's/:in /:_in /g' glpkpi.py
sed: 1: "glpkpi.py": extra characters at the end of g command
make[1]: *** [glpkpi.py] Error 1
make: *** [all] Error 2

Now run:

cp swig/glpkpi.py python/glpkpi.py
python setup.py install

This should install the library. To test if the install worked, go to the examples directory

cd ../examples
python test.py