How To Create Python Virtual Environment on Ubuntu 22.04 LTS
In this tutorial, we will show you how to create a 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
Step 1. To ensure your Ubuntu 22.04 LTS system is up-to-date and secure, it’s crucial to update the system packages before proceeding with the virtual environment setup. Updating packages ensures that you have the latest security patches, bug fixes, and compatibility improvements. To update your system packages, open a terminal and run the following command:
sudo apt update sudo apt upgrade sudo apt install wget apt-transport-https gnupg2 software-properties-common
This command will fetch the latest package information and upgrade any outdated packages to their latest versions. Once the update process is complete, you can move on to the next step.
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:~$
When you’re done working on your project, it’s a good practice to deactivate the virtual environment to return to your system’s default Python installation. To deactivate the virtual environment, simply run:
deactivate
This command will deactivate the virtual environment, and your command prompt will return to its original state, indicating that you’re no longer working within the isolated environment.
Congratulations! You have successfully installed Python. Thanks for using this tutorial to create a Python virtual environment on the Ubuntu 22.04 LTS Jammy Jellyfish system. For additional help or useful information, we recommend you check the official Python website.