Tuesday, June 26, 2012

Setting up scientific computing using Python on Ubuntu 12.04

First day starting installation on my new Toshiba Z835 ultrabook! I already installed Ubuntu 12.04 on a partition using the windows installer (after creating a startup disk on a 16 Gb usb drive, 8 Gb is not enough.)

Setting up numpy, matplotlib, wxPython, ipython, etc.


I found dealing with virtual environments just too annoying (see below). I opted to reinstall ubuntu 12.04 fresh, and start the process over with no virtual environments. First thing, I ran update manager (click the icon at the top of the dock and type 'update manager.' To add the terminal to the dock, type 'terminal' and drag the icon to the dock, this is where we will type the following commands.) After all the updates, I restarted.
In a terminal, I typed:
sudo apt-get update
sudo apt-get install python-numpy

(This resulted in installing 1.6.1, which I am not thrilled about, since 1.6.2 is already out.)
sudo apt-get install python-scipy
sudo apt-get install python-matplotlib
sudo apt-get install python-wxtools
sudo apt-get install ipython

testing ipython, numpy, and pylab (matplotlib):
import pylab
pylab.ion()
import numpy as np
pylab.plot(np.arange(10),np.arange(10)**2)

Looks good!
Wow, that was a lot easier than virtualenv.

Handy note:
use apt-cache show ipython to show package information about ipython (or any other package)
apt-cache depends ipython to show what packages ipython depends on.
apt-cache search ipython to search available packages for 'ipython'


Since I'm on a roll, I decided to install two new fancy tools that look cool:
sudo apt-get install ipython-qtconsole
sudo apt-get install ipython-notebook

Both of them work, and ipython notebook looks dope!


OLD: My first attempt, using virtualenv

I've decided to keep python packages compartmentalized from the system installation by using virtualenv, so my first step will be to install it (instructions at http://www.virtualenv.org/en/latest/index.html). I'm going to use pip to download and install python packages inside the virtual environment, so I'll use easy_install to set up my virtualenv. This way, any pip installation I have should only reside inside a particular virtualenv (not sure this is true, but worth a try...)

 

Setting up a virtual environment


sudo apt-get install python-setuptools
Now I'll install virtualenv:
sudo easy_install virtualenv
OK, that appears to have worked.

mkdir ~/src/virtualEnvironments
cd ~/src/virtualEnvironments
virtualenv --no-site-packages firstEnv

It turns out that the --no-site-packages is deprecated, since this is now the default behavior, but it doesn't seem to have done any harm.
Now, to activate the virtualEnv, type source firstEnv/bin/activate. Typing pip now shows that it is installed. If I type deactivate, then type pip, we see that pip is only installed in the virtual environment, sweet!

 

Installing numpy in the virtual environment

Let's get down to the business of installing some packages in our virtual environment:
pip install numpy
Error! Looks like I first have to install python-dev:
sudo apt-get install python-dev trying to install numpy again: pip install numpy works!

 

Installing matplotlib in the virtual environment

Next let's install matplotlib. It has some non-python dependencies, so we'll install these with apt-get: sudo apt-get update
sudo apt-get install libpng-dev
sudo apt-get install libfreetype6-dev
sudo apt-get install libjpeg8-dev
sudo apt-get install g++

To be honest, I'm not sure if all of these are necessary, but the last one (g++) definitely is. Supposedly there can be problems if gcc and g++ are not the same version, which you can check by typing gcc --version and g++ --version.
Finally, we are ready to get matplotlib:
pip install matplotlib
So that is ready to go.

 

Installing ipython in the virtual environment

Final step, let's get ipython working.
sudo apt-get install libzmq-dev
pip install pyzmq
pip install ipython[zmq,qtconsole,notebook,test]
sudo apt-get install libqt4-*
(this probably installs more than we need, installation took a LONG time.)
Download SIP and PyQt from http://www.riverbankcomputing.co.uk/software/sip/download and http://www.riverbankcomputing.co.uk/software/pyqt/download.
cd ~/Downloads/sip-4.13.3/
python configure.py
make
sudo make install
cd ../PyQt-x11-gpl-4.9.3/
python configure.py
make
sudo make install
at this point ipython, ipython qtconsole, and ipython notebook all seem to be working, but no windows show up for pylab plotting.
Trying this:

sudo apt-get install python-wxgtk2.8 python-wxtools wx2.8-i18n
But now wx is installed outside of virtual environment... Instead I will try to build it from source:
download the development (2.9) version wxPython-src from http://wxpython.org/download.php#stable
Following these instructions (http://wxpython.org/BUILD.html I did this:
cd ~/Downloads/wxPython-src-2.9.3.1/wxPython
sudo apt-get install gtk+3
sudo apt-get install gstreamer0.10
This takes a LONG time
python build-wxpython.py --install --build_dir=~/src/wx --installdir=~/src/wx --wxpy_installdir=~/src/wx THIS DID NOT WORK.
This link contains some info to get it working: http://codersbuffet.blogspot.com/2009/09/wxpython-in-virtualenv.html
might try it later...
Or possibly something like this:
http://wiki.wxpython.org/wxPythonVirtualenvOnMac

1 comment:

Edward said...

Hi Peter,

I followed your instructions and got this error. Can you help me out?

Thanks,
Ed

>>> import pylab
>>> pylab.ion()
>>> import numpy as np
>>> pylab.plot(np.arange(10),np.arange(10)**2)
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2460, in plot
ax = gca()
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 701, in gca
ax = gcf().gca(**kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 369, in gcf
return figure()
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 343, in figure
**kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 80, in new_figure_manager
window = Tk.Tk()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1688, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable