Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Python virtual environments are used to create isolated python environments to avoid dependency and version conflicts, and also indirectly takes care of permission issues. But what is the easiest way to set it up, and use it, in Ubuntu?

share|improve this question
up vote 56 down vote accepted

Installation using setuptools and pip

NOTE: If you don't want to touch system files or you don't have root, please see this question on stackoverflow. For everyone else please read on...

Install setuptools and pip

According to the distribute website setuptools and easy_install are old and busted (the version included in Ubuntu 12.04 doesn't work with python3), and distribute and pip are the new hotness. So we will use those:

EDIT: Since September 2014, distribute has been merged back into setuptools. So we can use setuptools and easy_install again to install pip. Use:

wget -O - | sudo python


wget -O - | sudo python3

if you're using python 3, then install pip with

sudo easy_install pip

Optional: Turn on bash autocomplete for pip

pip completion --bash >> ~/.bashrc

and run source ~/.bashrc to enable

Use pip to install virtualenv and virtualenvwrapper

The reason we are also installing virtualenvwrapper is because it offers nice and simple commands to manage your virtual environments.

sudo pip install virtualenv
sudo pip install virtualenvwrapper

Setup virtualenv

First we export the WORKON_HOME variable which contains the directory in which our virtual environments are to be stored. Let's make this ~/.virtualenvs

export WORKON_HOME=~/.virtualenvs

now also create this directory


and put this export in our ~/.bashrc file so this variable gets automatically defined

echo "export WORKON_HOME=$WORKON_HOME" >> ~/.bashrc

Setup virtualenvwrapper

To use virtualenvwrapper we need to import its functions in our ~/.bashrc

echo "source /usr/local/bin/" >> ~/.bashrc

We can also add some extra tricks like the following, which makes sure that if pip creates an extra virtual environment, it is also placed in our WORKON_HOME directory:

echo "export PIP_VIRTUALENV_BASE=$WORKON_HOME" >> ~/.bashrc 

Source ~/.bashrc to load the changes

source ~/.bashrc

Test if it works

Now we create our first virtual environment. The -p argument is optional, it is used to set the Python version to use; it can also be python3.4.

mkvirtualenv -p python2.7 test

You will see that the environment will be set up, and your prompt now includes the name of your active environment in parentheses. Also if you now run

python -c "import sys; print sys.path"

you should see a lot of /home/user/.virtualenv/... because it now doesn't use your system site-packages.

You can deactivate your environment by running


and if you want to work on it again, simply type

workon test

Finally, if you want to delete your environment, type

rmvirtualenv test


Important note:

  • All virtual environments have pip automatically installed, never call pip with sudo in your virtual environment, because this will use the system wide pip.

Thanks to the author of this blogpost.

share|improve this answer
python-pip and python-virtualenv are installable through the Ubuntu repsitory, no need for manual installation. – Timo Jan 18 '13 at 15:18
@Timo I know, but the python packages in Ubuntu are outdated. Maintaining python packages with pip is much easier. Also the pip from Ubuntu (12.04) is from setup-tools which doesn't support python3. – Gerhard Burger Jan 18 '13 at 17:30
However, in Ubuntu, we recommend Ubuntu packages, for many reasons. There's nothing wrong with older versions of pip or virtualenv, they are perfectly capable. – tumbleweed Jan 18 '13 at 20:04
@tumbleweed Like a already said, the pip version in ubuntu does not work with python3, doesn't seem "perfectly capable" to me. Besides, the packages you will install IN your virtualenv will not be Ubuntu packages. So if you only want Ubuntu packages you better not install virtualenv... – Gerhard Burger Jan 18 '13 at 21:14
@GerhardBurger: If you create a python3 virtualenv, it'll get a pip that works in python3. If you use Ubuntu's virtualenv to create the virtualenv, everything you've done will be contained within the virtualenv. If you start sudo easy_installing stuff, it'll leave a mess all over /usr/local, that's non-trivial to clean up, without much gain. – tumbleweed Jan 19 '13 at 13:30

It's easy, you install python-virtualenv. Then you can create a virtualenv with the virtualenv command. See their documentation for more.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.