DEV Community

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

Posted on • Originally published at Medium

1

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.

Quickstart image

Django MongoDB Backend Quickstart! A Step-by-Step Tutorial

Get up and running with the new Django MongoDB Backend Python library! This tutorial covers creating a Django application, connecting it to MongoDB Atlas, performing CRUD operations, and configuring the Django admin for MongoDB.

Watch full video →

Top comments (0)

Neon image

Set up a Neon project in seconds and connect from a Python application

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay