virtualenv+pip in ubuntu

Posted on May 5, 2010

Skip everything and download a bash script which will run everything for you or read on to actually learn something.

This guide works for Ubuntus 9.10, 10.04, and 10.10.

Global installation

We will install pip, virtualenv, and virtualenvwrapper globally for anyone on the computer to use it. You will need the root privileges.

Switch to the root now:

sudo su -

Dependencies

Install setuptools with easy_install to boostrap pip, SCMs to use later with pip, and the python development environment should you need pip to install architecture dependent (binary) Python packages:

apt-get install python-setuptools subversion python-svn mercurial git-core python-git bzr python-dev

pip

Ubuntu 11.04 and above

Use the official repository:

apt-get install python-pip
Ubuntu 10.10 and below

I have created an Ubuntu package for the current version of pip:

add-apt-repository ppa:whiskybar/comoga
apt-get update
apt-get install python-pip

virtualenv & virtualenvwrapper

Trust me, you want virtualenvwrapper. It adds a few but very handy commands to virtualenv.

pip install virtualenv
pip install virtualenvwrapper

Personalisation

Your global setup is now complete. Every user, including yourself, needs to do a bit of bootstrapping before they can use the full potential of virtualenvwrapper. Run commands from the following section as yourself. Get out of the root:

exit

Personalize virtualenvwrapper

Create a directory which will hold all your virtualenvs:

mkdir ~/envs

Now, make the virtualenvwrapper available to your session:

export WORKON_HOME=~/envs
source $(which virtualenvwrapper.sh)

And make it persistent:

echo "export WORKON_HOME=~/envs" >>~/.bashrc
echo "source $(which virtualenvwrapper.sh)" >>~/.bashrc

Conclusion

Here is a very brief jump ahead for you to try out the virtualenv installation. Check out documentations to virtualenvwrapper, virtualenv, and pip to discover their full potentials!

mkvirtualenv PROJECT
to create a new virtualenv for your project.
workon PROJECT
to switch between projects' virtualenvs.
deactivate
to quit from the current virtualenv.