I’ve got a spanking new install of Kubuntu 11.10, and I need to get it set up for Python data hacking.  Sure, I could spring for an Enthought Python Distrubution, but where would be the masochism in that?

Inspired by Zed, let’s do this the hard way.

The linux distro comes with Python 2.7.2. Perfect! Or, use Pythonbrew to set up a local Python build that you want to use. I presume you know how to get to the command line, as well as how to edit text files using emacs, vim, pico, whatever.

Let’s get some tools:

sudo apt-get install git gfortran g++

We need to compile against Python headers and get setuptools and pip:

sudo apt-get install python-dev python-pip

Let’s isolate our Python distro from carnage:

sudo apt-get python-virtualenv
sudo pip install virtualenvwrapper

Now these lines to your ~/.bashrc:

source /usr/local/bin/virtualenvwrapper.sh
export WORKON_HOME=$HOME/.virtualenvs

Now open a new terminal and establish a virtual environment, say “py27″:

mkvirtualenv py27
workon py27

We need some math libraries (ATLAS + LAPACK):

sudo apt-get install libatlas-base-dev liblapack-dev

Ok, now to install and build all the scientific python hotness:

pip install numpy scipy

For matplotlib, we need lots of libraries. This one is dependency heavy. Note we can ask Ubuntu what we need, what’s installed, and what is not:

apt-cache depends python-matplotlib | awk '/Depends:/{print $2}' | xargs dpkg --get-selections

Easiest thing to do is just build all the dependencies (just say yes if it asks to install deps of matplotlib instead of python-matplotlib):

sudo apt-get build-dep python-matplotlib

Ok, now this should work:

pip install matplotlib

Now, of course, we need the IPython interpreter. Don’t settle for 0.11!

pip install -e git+https://github.com/ipython/ipython.git#egg=ipython
cd ~/.virtualenvs/py27/src/ipython
python setupegg.py install

Note, you may need to sudo rm /usr/bin/ipython.py if there is a conflict.

Ok, let’s beef up the IPython interpreter. Note the pip commands FAIL. This is ok. We’ll do it by hand.

sudo apt-get install qt4-dev-tools

pip install sip
cd ~/.virtualenvs/py27/build/sip
python configure.py
make
sudo make install

pip install pyqt
cd ~/.virtualenvs/py27/build/pyqt
python configure.py
make
sudo make install

# clean up
cd ~/.virtualenvs/py27/
rm -rf build

Just a few more things, you won’t be disappointed.

sudo apt-get install libzmq-dev
pip install tornado pygments pyzmq

Alright, let’s get pandas. It’s under heavy development (Wes is a beast); so lets pull the latest from git.

pip install nose cython
pip install -e git+https://github.com/wesm/pandas#egg=pandas

# we should get statsmodels too
pip install -e git+https://github.com/statsmodels/statsmodels#egg=statsmodels

Btw, you’ll note this git stuff goes into your ~/.virtualenvs/py27/src directory, if you want to git pull and update.

OK! Phew! For the grand finale:

Run the amazing qtconsole:

ipython qtconsole --pylab=inline

Or the even more amazing WEB BROWSER:

ipython notebook --pylab=inline

Launch a browser and point to http://localhost:8888/. For kicks, try opening one of Wes’s tutorial workbooks, here. You may have to fiddle a bit, but it should work.

Enjoy!

  • Pingback: Software Carpentry » Quantifying Installation Costs

  • Naum Rusomarov

    I’ll try your instructions next Friday. :)

    • Anonymous

      Good luck! Or try the Enthought Python Distribution – it’ll save you so much headache :)

  • Jorge

    Hi, I wanted to try pip as an easy way to keep (scientific) python packages up-to-date, so thanks for this guide.

    python-matplotlib depends on numpy, so at least for me, running sudo apt-get build-dep python-matplotlib installs pyhon-numpy instead of using the existing (and newer) version installed through pip (I hadn’t installed it in a virtual environment). What’s really happening there?

    I’m very new to pip, so it may be something really basic, or some groundless worries… I usuallly install epd on windows and apt-get as needed on linux, but pip looks great.

  • Joseph8th

    This is great! The use of virtualenv brought me here… I use pythonbrew’s venv’s myself, but virtualenvwrapper is great, too. (In fact a side project of mine is forking a pybrew to venvwrap with project manager pre/post scripts.) I foresee a bright future for pip+virtualenv+pythonbrew in terms of streamlining python app development and launching the python language into the stratosphere as one of the all time most ingenious inventions ever.

    Of course, I’m a math person, so I might be biased.

    • AdamDKlein

      I like pythonbrew, it’s a great tool as well. I’ve yet to discover an easy way to clone an entire environment, which would be super useful to me.

      • Joseph8th

        Aye… that’s become a pet project of mine. I forked pythonbrew on github and am attempting to modify it to download, install and use virtualenvwrapper instead of using virtualenv directly. The benefit should be obvious — I could write pre-/post- scripts that would turn pybrew into a full-fledged project-environment manager, replete with venv switching on folder entry. :) I haven’t committed anything yet, but the fork I’m monkeyin’ with is at git@gihub.com:joseph8th/pythonbrew.git if you’re interested.

  • Rdeeban

    I just went through all the installations and everything worked perfectly and I was able to successfully execute the last command. I closed the terminal and now when I get back to to the virtual directory I am unable to start it again. It keeps saying multiple arguments for –pylab=inline

    • Rdeeban

      Nevermind I got it to work. I was going through the tutorial presented by Wes and I couldn’t import pandas even though the pandas directory existed. Is it just not finding the directory?

      • AdamDKlein

        Funny, my tutorial doesn’t say to install pandas, looks like. Anyway, you might want to paste the error message into a post on pydata google groups. Somewhere there will be sure to help out!

  • BoxBoy

    Hey Adam,
    Thanks for this tutorial. It all works very nicely. I only have one complaint and I’m wondering if it isn’t a simple fix that will make this perfect. If I run a command in debugging mode, i.e., %run -d foo.py, then tab completion no longer works. Any idea why?

  • syamsrikanth

    Hai AdamDKlein :- I had installed ipython notebook in ubuntu

    when i was ready to open notebook i am facing the error [ ImportError: The IPython Notebook requires tornado >= 3.1.0, but you have 2.1 ]

© 2014 Adam Klein's Blog Suffusion theme by Sayontan Sinha, modified by Adam :)