DEV Community

Cover image for How to create a python virtual environment on Ubuntu 22.04.
Agnes
Agnes

Posted on • Originally published at Medium

How to create a python virtual environment on Ubuntu 22.04.

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
Enter fullscreen mode Exit fullscreen mode

Next, run the following apt command

sudo apt upgrade
Enter fullscreen mode Exit fullscreen mode

2. Pip install.

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

sudo apt-get install python3-pip
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

4. Creating a virtual environment.

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

virtualenv venv 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)