Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

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

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read