How to Create Python Virtual Environments on Ubuntu or any Linux distribution
What is a virtual environment in Python?
A virtual environment in Python is an isolated workspace specifically for the language. Packages installed within this environment do not interfere with globally installed Python packages, ensuring a clean separation of dependencies for different projects.
This tutorial guides you through the step-by-step process to create a virtual environment in Python on Ubuntu or any other Linux distribution.
The steps to create a virtual environment are as follows :
First, we need to install the venv
package on the Linux OS. To install, use the following command:
sudo apt install python3-venv
Next, we need to create the virtual environment in the location where you want to establish the Python project. To set up the virtual environment, use the following command:
python3 -m venv [GIVE VIRTUAL ENVIRONMENT NAME]
Once the virtual environment is created, we then have to activate it. To activate the virtual environment, use the following command:
source [VIRTUAL ENVIRONMENT NAME]/bin/activate
To exit the virtual environment and return to the global Python environment, just type:
deactivate
Remember, each virtual environment is self-contained, so you'll need to activate it whenever you're working on a project that requires it.
If you want to create a virtual environment in Python on the Windows operating system, then you can follow this tutorial below:
Subscribe to my newsletter
Read articles from Faizan Alam directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by