How To Create Python Virtual Environments On Ubuntu

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,



Support Django Central

If you appreciate my work, or if it has helped you along your journey. It would mean a lot to me if you could write a message on my wall and share a cup of coffee (or tea) with me.
Buy Me a Coffee at

Leave a Comment