In this tutorial, we will show you how to create Python virtual environment on Ubuntu 22.04 LTS. For those of you who didn’t know, Python virtual environments are a way to create isolated Python environments, each with its own set of installed packages, for different projects. This allows you to have different versions of packages for different projects and prevents conflicts between packages that might have different dependencies.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation of the Python virtual environment open-source photo editing software on Ubuntu 22.04 (Jammy Jellyfish). You can follow the same instructions for Ubuntu 22.04 and any other Debian-based distribution like Linux Mint, Elementary OS, Pop!_OS, and more as well.
Prerequisites
- A server running one of the following operating systems: Ubuntu 22.04, 20.04, and any other Debian-based distribution like Linux Mint.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies for Python.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Create Python Virtual Environment on Ubuntu 22.04 LTS Jammy Jellyfish
Step 1. First, make sure that all your system packages are up-to-date by running the following apt
commands in the terminal.
sudo apt update sudo apt upgrade sudo apt install wget apt-transport-https gnupg2 software-properties-common
Step 2. Installing Python.
Before you can create a virtual environment, you need to have Python installed on your system. If you do not have Python installed, you can follow our guide here.
Step 3. Installing Python Virtual Environment.
You can install this package by running the following command below:
sudo apt install python3-venv
Step 3. Create Python Virtual Environment on Ubuntu 22.04.
After you have a virtual environment installed, you can create a virtual environment by running the following command:
python3 -m venv myenv
The above command creates a directory named ‘my-env
‘ in the current directory, which contains pip, interpreter, scripts, and libraries:
ls my-env
Output:
bin include lib lib64 pyvenv.cfg share
Now, activate the environment with the name “my-env
” and use the command:
source my-env/bin/activate
After you have activated the virtual environment, you will notice that your prompt changes to indicate that you are now in the virtual environment. The virtual environment will inherit the environment variables and path of your system but will have its own separate set of installed packages:
(my-env) idroot.us@ubuntu:~$
Once you have finished working in your virtual environment, you can deactivate it by running the following command:
deactivate
This will return you to the system Python environment, and you will no longer have access to the packages installed in the virtual environment.
Congratulations! You have successfully installed python. Thanks for using this tutorial to create Python virtual environment on Ubuntu 22.04 LTS Jammy Jellyfish system. For additional help or useful information, we recommend you check the official Python website.