Installing matplotlib - the hard way

I recently installed matplotlib from the source. It was quite an experience that I thought I would share my experience so that others don't have to waste time searching how to do that. So here it is!

What is matplotlib?
Matplotlib is a libarary to plot figures from your Python program. It has much more features than just plotting. You can read more about that from the library's home page.

If you are planning to install matplotlib on Linux everything from the source by building everything yourself, this guide is for you. Please read on.

You will have to install the dependencies first before you can install matplotlib. Matplotlib depends on numpy, zlib, libpng and FreeType libraries. You can get the full dependency list (including the optional dependent libraries) from here. Let us see how to install each one of these components.

Phase 1: Installing numpy
Numpy requires the same Fortran compiler that was used to build blas. There are two flavors of Fortran compilers possible: f77 or gfortran. Unfortunately, these two are not ABI compatible, hence make sure you identify which version you are looking for. It is easy to identify the Fortran compiler used by using ldd on the blas library:

roy@roy-debian:~$ ldd /usr/lib/libblas.so.3gf
linux-gate.so.1 => (0xb7f51000)
libgfortran.so.3 => /usr/lib/libgfortran.so.3 (0xb7e09000)
libm.so.6 => /lib/i686/cmov/libm.so.6 (0xb7de3000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb7dd5000)
libc.so.6 => /lib/i686/cmov/libc.so.6 (0xb7c7a000)
/lib/ld-linux.so.2 (0xb7f52000)


Hence in my case I know that it is gfortran that I should be making use of. Install gfortran, by following the steps below:
aptitude search gfortran
sudo apt-get install gfortran-multilib

Then download the Numpy from here. Once you have downloaded, untar the tar file and follow the steps below:
roy@roy-debian:~/Desktop$ tar xvfz numpy-1.3.0.tar.gz
roy@roy-debian:~/Desktop$ cd numpy-1.3.0/
roy@roy-debian:~/Desktop/numpy-1.3.0$ python setup.py build --fcompiler=gnu95
roy@roy-debian:~/Desktop/numpy-1.3.0$ sudo python setup.py install


Once you are done with compiling and installing numpy module, you come out of the numpy-1.3.0 directory and check if everything is okay:
roy@roy-debian:~/Desktop/numpy-1.3.0$ cd ..
roy@roy-debian:~/Desktop$ python
Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> numpy.version.version
'1.3.0'


Phase 2: Installing zlib and libpng
You can downlod the zlib from here. This is required for libpng. Once you download the zlib, do the following:
roy@roy-debian:~/Desktop$ tar xvfz zlib-1.2.3.tar.gz
roy@roy-debian:~/Desktop$ cd zlib-1.2.3/
roy@roy-debian:~/Desktop/zlib-1.2.3$ ./configure
roy@roy-debian:~/Desktop/zlib-1.2.3$ make test
roy@roy-debian:~/Desktop/zlib-1.2.3$ sudo make install

Once this is done, you are ready to install libpng. You can download libpng from here. Once you have downloaded, follow similar instructions:
roy@roy-debian:~/Desktop$ tar xvfz libpng-1.2.35.tar.gz
roy@roy-debian:~/Desktop$ cd libpng-1.2.35/
roy@roy-debian:~/Desktop/libpng-1.2.35$ ./configure
roy@roy-debian:~/Desktop/libpng-1.2.35$ make check
roy@roy-debian:~/Desktop/libpng-1.2.35$ sudo make install

Phase 3: Installing FreeType library
You can download FreeType library from here. Then follow the instructions below:
roy@roy-debian:~/Desktop$ tar xvfz freetype-2.3.9.tar.gz
roy@roy-debian:~/Desktop$ cd freetype-2.3.9/
roy@roy-debian:~/Desktop/freetype-2.3.9$ ./configure
roy@roy-debian:~/Desktop/freetype-2.3.9$ make
roy@roy-debian:~/Desktop/freetype-2.3.9$ sudo make install


Final phase: Installing matplotlib
Oooh ... Now we come the final phase of our installation. Yes, we are actually going to install matplotlib.

You can download the matplotlib from here. Once you have downloaded the tar file, follow the instructions below:
roy@roy-debian:~/Desktop$ tar xvfz matplotlib-0.98.5.2.tar.gz
roy@roy-debian:~/Desktop$ cd matplotlib-0.98.5.2/
roy@roy-debian:~/Desktop/matplotlib-0.98.5.2$ python setup.py build
roy@roy-debian:~/Desktop/matplotlib-0.98.5.2$ sudo python setup.py install

Testing the installation
Well, that was quite a long process. Now let us check if everything went well.

roy@roy-debian:~/Desktop$ python
Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.use('Agg')
>>> import matplotlib.pyplot as plt
>>> plt.plot(range(10))
[]
>>> plt.savefig('myfig')
>>> exit()

'Agg' backend saves the file in the png format. (Remember we installed the libpng not long time ago!) To know more about back ends, please refer here. Once you come out of Python shell, you can view myfig.png by giving the following command:
gimp myfig.png


For some reason, if you don't have gimp installed, you can make use of firefox or iceweasel to view the png file file.

Comments

Roy Emmerich said…
Roy I just wanted to say a very big thanks for this wonderful post. It has helped me immensely!!

Just one thing. I'm on Ubuntu 8.04 and I found that after going through your recipe I didn't have wx installed. The home page for this project is here:

www.wxpython.org

but I found, after doing:

aptitude search wx

that the latest build was in the repositories anyway so I just typed:

sudo apt-get install python-wxtools

Once again thanks a lot for you great post.

Regards
Roy Emmerich
Roy said…
Thanks a lot for letting me know.
Roy Emmerich said…
I then had further problems, identical to this posting:

http://old.nabble.com/cannot-install-kubuntu-9.04-mpl-0.99.1.1,-python-2.6-td25879372.html

basically had to do three things:

1. sudo apt-get install tk-dev
2. sudo apt-get install tk8.5-dev tcl8.5-dev
3. comment out the line:
macosx = True
in the setup.cfg file in the matplotlib root directory of the extracted source tar.gz

and then it worked!
Anonymous said…
Just to say, the zlib 1.2.5 crashes Ubuntu 8.04 LTS.

with this message
"Nautilus can't be used now, due to an unexpected error from Bonobo when attempting to register the file manager view server."

the solution was to use the zlib in the version 1.2.3 exactly.

libpng 1.2.43 works correctly with zlib 1.2.5

Popular posts from this blog

The mysterious ORA-03111 error

Note on allocationSize parameter of @SequenceGenerator while using JPA

xVM's vboxmanage.exe command