How To Create Python Virtual Environments On Ubuntu

2 min read

It is recommended to use virtual environments to create isolated Python environments so that you can use different package versions for various projects, which is far more practical than installing Python packages system-wide.

A virtual environment is a self-contained directory tree that contains dependencies required by different projects isolated to existing packages.

By using virtual Python environments, applications can run in their own ‘sandbox’ in isolation of other Python applications

In this article, we will go through the creation of isolated, python virtual environments using Python's venv module.

Alternatively, you can create virtual environments in python using the virtualenv package, follow this guide to learn more

However, since Python 3.3 it's recommended to use venv because being part of Python it has access to the internals of Python which means it can do things the right way with far fewer hacks.

For example, virtualenv has to copy the Python interpreter binary into the virtual environment to trick it into thinking it's isolated, whereas venv can just use a configuration file that is read by the Python binary in its normal location for it to know it's supposed to act like it's in a virtual environment.

So venv can be thought of virtualenv done right, with the blessing and support of the Python developers.

Creating Python Virtual Environment On Ubuntu

First, we need to install the python3-venv package which has the venv module, So run the following command on your terminal.

sudo apt install python3-venv

Next switch to the directory where you would like to store your virtual environments then run the following command to create your new virtual environment.

Note that here myenv ,is the name of my virtual environment it could be anything.

python3 -m venv myenv

This creates a brand new virtual environment which doesn't have any Python package or module from our existing global python installation you can verify it by navigating into the site package directory of the environment as follows.

Note this is just for verification, not a mandatory step.

cd /myenv/lib/python3.6/site-packages

Now let's activate our environment. In your terminal run the following command to activate the newly created environment.

source myenv/bin/activate

If everything went right, you should see your terminal prefixed by the Environment name which is, in this case, myenv.

Now any package you install will be isolated to other python installation on your system. To deactivate the environment anytime simply run,




Latest Articles

Latest from djangocentral

Django 4.1 adds async-compatible interface to QuerySet

The much-awaited pull request for an async-compatible interface to Queryset just got merged into the main branch of Django.Pull Request - The Django core team has been progressively adding async suppor…
Read more →

3 min read

Making Django Admin Jazzy With django-jazzmin

Django admin is undoubtedly one of the most useful apps of Django. Over the years there has been very little change in the admin app as far as the UX is concerned and it's not a bad thing at all. Django admin was designed to provide a simple and minimali…
Read more →

4 min read