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 60 down vote accepted

With virtualenvwrapper (user friendly wrappers for the functionality of virtualenv)

Install pip

Install pip for Python 2 with

sudo apt-get install python-pip

or for Python 3

sudo apt-get install python3-pip

Optional: Turn on bash autocomplete for pip

Run

pip completion --bash >> ~/.bashrc

and run source ~/.bashrc to enable.

Install virtualenv

Install virtualenv with

sudo apt-get install virtualenv

Use pip to install virtualenvwrapper

The reason we are also installing virtualenvwrapper is because it offers nice and simple commands to manage your virtual environments. Because we want to avoid sudo pip we install virtualenvwrapper locally (by default under ~/.local):

pip install --user virtualenvwrapper

or for Python 3

pip3 install --user virtualenvwrapper

and

echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> ~/.bashrc

Setup virtualenv and virtualenvwrapper

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

mkdir $WORKON_HOME

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

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

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

echo "source ~/.local/bin/virtualenvwrapper.sh" >> ~/.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 for example.

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

deactivate

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

workon test

Finally, if you want to delete your environment, type

rmvirtualenv test

Enjoy!


Thanks to the author of this blogpost.

share|improve this answer
2  
python-pip and python-virtualenv are installable through the Ubuntu repsitory, no need for manual installation. – Timo Jan 18 '13 at 15:18
1  
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
1  
@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
    
looks like alias is not needed now: --no-site-packages is default and --distribute deprecated – int_ua Nov 27 '13 at 8:21
    
In case mkvirtualenv test doesn't work, see also: stackoverflow.com/questions/15608236/… – Nikos Alexandris Mar 20 '14 at 8:40

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

The correct way is to do this is to install virtualenv using apt, create a virtualenv and then activate it. After activation, use pip to install Python packages as usual. sudo is only used to install virtualenv and not on pip, therefore preventing pip from competing with apt to manage Python packages at the system level. The code to do so is:

sudo apt update
sudo apt install virtualenv
cd ~/desired_directory  # cd to desired_directory
virtualenv venv  # create virtualenv named venv for default system Python, which is Python 2 for Ubuntu
source venv/bin/activate  # activate virtualenv
pip install -U pip  # upgrade pip in case it is outdated
pip install desired_package  # install desired_package

If you would like to create a virtualenv for Python 3, replace virtualenv venv with:

virtualenv venv -p python3
share|improve this answer
1  
source saves me a lot of commands – vnpnlz Jun 14 at 8:13

With the venv module available from Python 3.3 (and Ubuntu 15.10)

Virtual environments (venvs) are so popular that the functionality is now included in python itself (from 3.3 onwards). To use it on Ubuntu you need to install python3-venv (since the ensurepip module is not available):

sudo apt-get install python3-venv

After that you can use

pyvenv myvirtualenv

to create a virtual environment called myvirtualenv. With pyvenv you can also use the -p flag to specify the python version, so for Python 3 you would use

pyvenv -p python3 myvirtualenv

You can then use

source myvirtualenv/bin/activate

to activate the virtual environment. To deactivate simply type

deactivate
share|improve this answer

Your Answer

 
discard

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.