Sunday, October 13, 2013

Setting up multiple versions of Python on Ubuntu

I recently switched from using a Mac back to a PC, I had originally planned to use both windows and linux via dual-boot, but having purchased a Radeon and Ubuntu not even starting from the bootable USB, I decided to try run my Python development environment on Windows. After playing with python on Windows, I found it quite tedious to have both a 2.7.5 and a 3.3.2 environment. I also didn't like having to rely on http://www.lfd.uci.edu/~gohlke/pythonlibs/ for all the 'pain' free install, since trying to compile some the libs with the required C++ compiler is even a bigger pain.

So I went with a colleagues suggestion of  VMWare Player 6, and installed Ubuntu.

After breaking a couple installs and recreating VMs left and right, I finally have a process to install and work with multiple versions of Python.

First up, get a whole bunch of dependencies:
sudo apt-get install python-dev build-essential  
sudo apt-get install python-pip
sudo apt-get install libsqlite3-dev sqlite3
sudo apt-get install libreadline-dev libncurses5-dev 
sudo apt-get install libssl1.0.0 tk8.5-dev zlib1g-dev liblzma-dev
sudo apt-get build-dep python2.7
sudo apt-get build-dep python3.3

sudo pip install virtualenv
sudo pip install virtualenvwrapper

Add the virtualenvwrapper settings to ~.bashrc:
export WORKON_HOME="$HOME/.virtualenvs"
source /usr/local/bin/virtualenvwrapper.sh

Then for Python 2.7:
sudo mkdir /opt/python2.7.5

wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tgz
tar xvfz Python-2.7.5.tgz
cd Python-2.7.5/
./configure --prefix=/opt/python2.7.5
make
sudo make install

mkvirtualenv --python /opt/python2.7.5/bin/python2 v-2.7.5

Then for Python 3.3:
sudo mkdir /opt/python3.3.2

wget http://python.org/ftp/python/3.3.2/Python-3.3.2.tgz
tar xvfz Python-3.3.2.tgz
cd Python-3.3.2
./configure --prefix=/opt/python3.3.2
make  
sudo make install

mkvirtualenv --python /opt/python3.3.2/bin/python3 v-3.3.2

To view the virtual environments:
lsvirtualenv

To change between them:
workon  [env name] e.g. v-3.3.2

Then to install some of the major scientific and machine learning related packages:
pip install numpy
pip install ipython[all]
pip install cython
sudo apt-get build-dep python-scipy
pip install scipy
pip install matplotlib
pip install scikit-learn
pip install pandas

To stop working on a particular version:
deactivate

5 comments:

Popular Posts

Followers