81

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

edwinksl
  • 23,789

4 Answers4

110

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

Install virtualenv

Install virtualenv with

sudo apt install virtualenv

Install virtualenvwrapper

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

As Ubuntu package (from Ubuntu 16.04)

Run

sudo apt install virtualenvwrapper

then run

echo "source /usr/share/virtualenvwrapper/virtualenvwrapper.sh" >> ~/.bashrc

Using pip

  1. Install and/or update pip

    Install pip for Python 2 with

    sudo apt install python-pip
    

    or for Python 3

    sudo apt install python3-pip
    

    (if you use Python 3, you may need to use pip3 instead of pip in the rest of this guide).

    Optional (but recommended): Turn on bash autocomplete for pip

    Run

    pip completion --bash >> ~/.bashrc
    

    and run source ~/.bashrc to enable.

  2. Install virtualenvwrapper

    Because we want to avoid sudo pip we install virtualenvwrapper locally (by default under ~/.local) with:

    pip install --user virtualenvwrapper
    

    and

    echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> ~/.bashrc
    
  3. Source virtualenvwrapper in .bashrc

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

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.

  • 2
    python-pip and python-virtualenv are installable through the Ubuntu repsitory, no need for manual installation. – Timo Jan 18 '13 at 15:18
  • 2
    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 08:21
  • In case mkvirtualenv test doesn't work, see also: http://stackoverflow.com/questions/15608236/eclipse-and-google-app-engine-importerror-no-module-named-sysconfigdata-nd-u – Nikos Alexandris Mar 20 '14 at 08:40
  • I read here that Instructions telling sudo pip install are inherently wrong. What do you think about it? – floatingpurr Mar 11 '16 at 12:59
  • This is a very answer, but it only works for Python2. Could you expand it to include Python3? – Luís de Sousa Apr 07 '16 at 07:59
  • No more sudo pip, everybody should be happy now ;) – Gerhard Burger Jun 20 '16 at 18:24
  • @GerhardBurger ~/.local/bin/virtualenvwrapper.sh seems to have /usr/bin/python hardcoded, so your instructions as is won't work with python3 and pip3 install --user virtualenvwrapper – raphael Jun 22 '16 at 14:14
  • @raphael Thanks for the heads up! It's not hardcoded, but if VIRTUALENVWRAPPER_PYTHON is not set it executes which python which will return /usr/bin/python since that is the default python (https://www.python.org/dev/peps/pep-0394/). You need to export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 to make it work, I will add that to the instructions. – Gerhard Burger Jun 22 '16 at 16:45
  • Between steps 1 and 2, you can also pip install --user pip to get the latest version of pip as a user install. – mkasberg Mar 29 '17 at 02:04
  • I think pip install -U pip is wrong without the --user flag. (-U is --upgrade; there's no short option for --user.) If you don't do --user, it will mess with the installation that is supposed to be managed by apt, which could put things in a weird state and probably won't work without sudo, as you mention in step 2 with virtualenvwrapper. – mkasberg Mar 29 '17 at 15:29
  • @mkasberg You're right, I was hoping pip would resolve this automatically based on some discussions on github. Additionally, if you do upgrade pip you would need to prepend ~/.local/bin to your PATH otherwise pip will still execute the system pip. Taken together I don't think upgrading pip is worth the extra effort, so I decided to remove the upgrade pip instructions again. – Gerhard Burger Apr 06 '17 at 09:27
  • The path /var/www/html/python/check/python3 (from --python=/var/www/html/python/check/python3) does not exist – Saravanan Nandhan Nov 30 '17 at 07:55
  • I confirm this works for Ubuntu 20.04 LTS. Many other tutorials do not work. Thank you, @GerhardBurger – Stefan_EOX Sep 09 '20 at 10:29
9

Virtual environments offer a way for managing and isolating dependencies on a per-project basis. Moreover, they also avoid the whole sudo pip install situation, which is a security risk as I have explained in https://askubuntu.com/a/802594/15003. The official Python documentation also encourages the use of virtual environments.

The easiest way to create and use virtual environments for both Python 2 and Python 3 is to install virtualenv using apt or apt-get. For each Python project, create a virtualenv and then activate it. Note that the virtualenv is specific for a particular Python version. After activation, use pip to install Python packages as usual regardless of whether you are using Python 2 or 3; there is no need to use pip3 for Python 3. sudo is only used to install virtualenv and is not used with pip, therefore avoiding the aforementioned security risk. The commands to do so are:

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

Read more about various bells and whistles for virtualenv at https://virtualenv.pypa.io/en/stable/.

edwinksl
  • 23,789
5

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. You can then use

source myvirtualenv/bin/activate

to activate the virtual environment. To deactivate simply type

deactivate

EDIT: The pyvenv script has been deprecated in favour of python3 -m venv. This prevents confusion as to what Python interpreter pyvenv is connected to and thus what Python interpreter will be used by the virtual environment. (source)

4

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

tumbleweed
  • 8,026