How to create a python virtual environment on Ubuntu 22.04.

Agnes Mbiti
2 min readFeb 18, 2023

--

Most of the time when we are working on python projects, we are bound to install different dependencies that help us achieve different project goals. It is important that we create virtual environments for each and every python project so as to keep the specific project dependencies isolated from the rest.

Prerequisites.

Ubuntu 22.04

1. System update.

First and foremost, to ensure that your system is up to date, run the command below

sudo apt update

Next, run the following apt command

sudo apt upgrade

2. Pip install.

After making sure your system is up to date with the latest packages using the above commands, we will then install pip. We will install pip by running the command below

sudo apt-get install python3-pip

3. Virtualenv install.

Virtualenv is used to isolate virtual environments for python projects.We install it using pip3 by running the command below.

sudo pip3 install virtualenv 

4. Creating a virtual environment.

Next, we create the virtual environment, which is done by running the command below.

virtualenv venv 

Point to note: You can use your preferred name in place of “venv”.

After running the command above, a directory named venv appears at the root of your project. The said directory contains python executable files.

5. Activating virtual environment.

All we have left to do now is activate our virtual environment. Let’s run the final command to activate our environment .

source venv/bin/activate

Voila! Our python virtual environment is now set up and activated.

6. Deactivating the virtual environment.

For one reason or the other, we might need to deactivate our environment. To do this, we run the command below.

deactivate

Now you have a step by step guide on how to create, activate, and deactivate a python environment.

Good luck !

--

--