Preparing a Python Virtual Environment on Ubuntu

Share this post on:

Ubuntu comes with Python installed by default. For example, in the version of Ubuntu we will be working with in this article, 22.04, the default Python version installed is 3.10.

root@01b5f3a5c984:/# python3 -V
Python 3.10.12

Checking the installed Python version.

So what happens if we need to run our project with a Python version different from 3.10? Furthermore, how would we resolve running our application with Python version 3.10 but with library versions different from the default ones? It’s important to note that changing these versions could lead to malfunctioning of the operating system, as many of its functionalities rely on them. To address these issues, Python virtual environments come into play.

A Python virtual environment is an isolated environment that allows you to install and manage packages and dependencies independently for a specific project. This means that each project can have its own set of packages, even if different projects require different versions of the same packages or Python itself.

Key Concepts of a Virtual Environment:

  1. Dependency Isolation:
    • The virtual environment is an independent space where you can install specific packages without affecting other projects or the operating system. This is crucial when working on multiple projects that may require different versions of the same libraries.
  2. Python Version Independence:
    • You can have a virtual environment that uses a specific version of Python, even if the operating system has a different default version. This is useful for testing and developing across different versions of Python.
  3. Avoids Conflicts:
    • By having a virtual environment, you avoid dependency conflicts between projects. For example, one project might need Django 3.0 while another requires Django 4.0. Virtual environments allow both projects to coexist on the same system without interference.
  4. Portability:
    • A virtual environment facilitates the portability of projects between different systems. You can share your project with its virtual environment (often via a requirements.txt file that lists all dependencies) so that others can replicate the exact same environment on their systems.
  5. Easy Package Management:
    • Within a virtual environment, you can use tools like pip to install, update, or remove packages without worrying about affecting the global system environment.

How It Works:

When you create a virtual environment, Python generates a directory where the Python binaries, pip, and an independent space for installing packages are copied. When you activate the environment, the operating system temporarily changes the PATH so that the python and pip commands refer to the binaries within the virtual environment instead of the system-wide ones.

Let’s get practical and use commands on the CLI. Assuming you already have Ubuntu ready, if not, I suggest using a Docker container of Ubuntu to carry out this practice.

Installing Python

First, we will add the Python repository to the operating system in order to install it. To do this, run:

sudo add-apt-repository ppa:deadsnakes/ppa

Next, proceed with updating the repositories and installing Python. In our example, we will be using a version newer than the existing one, and we will install version 3.11.

sudo apt update
sudo apt install python3.11

Now, if you check the default version available, you will notice that it is still 3.10. What’s happening here? If I just installed version 3.11, why can’t I see it as the default version if it’s a newer version? The answer is that Python doesn’t work this way. Multiple versions of Python can coexist on the same operating system, but only the one we decide to activate will be active. We don’t actually need to activate our installed version because we will be using virtual environments, but if you want to set it as active, here’s how you can do it. Simply unmark the previous version as the default and set the new one as the default.

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2

Preparing Our Environment

Now proceed with the installation of the following packages:

sudo apt-get install python3.11-venv python3.11-dev python3.11-distutils 
sudo curl -sS https://bootstrap.pypa.io/get-pip.py | python3.11

At this point, we are ready to create our first virtual environment. To do this, navigate to your project directory and then run:

python3.11 -m venv my-venv

What does this mean? With this command, we are instructing Python version 3.11 to create a virtual environment named my-venv in the directory where we are located. Let’s verify that a directory named my-venv has been created and see what it contains.

root@01b5f3a5c984:/var/tmp/python-app# ls -lt
total 4
drwxr-xr-x 5 root root 4096 Aug 10 21:16 my-venv
root@01b5f3a5c984:/var/tmp/python-app# ls -lt my-venv/
total 16
drwxr-xr-x 2 root root 4096 Aug 10 21:16 bin
drwxr-xr-x 3 root root 4096 Aug 10 21:16 lib
lrwxrwxrwx 1 root root    3 Aug 10 21:16 lib64 -> lib
-rw-r--r-- 1 root root  169 Aug 10 21:16 pyvenv.cfg
drwxr-xr-x 3 root root 4096 Aug 10 21:16 include

Now let’s check which Python libraries are installed by default on the operating system.

root@01b5f3a5c984:/var/tmp/python-app# pip list
Package            Version
------------------ -------------
blinker            1.4
cryptography       3.4.8
dbus-python        1.2.18
distro             1.7.0
httplib2           0.20.2
importlib-metadata 4.6.4
jeepney            0.7.1
keyring            23.5.0
launchpadlib       1.10.16
lazr.restfulclient 0.14.4
lazr.uri           1.0.6
more-itertools     8.10.0
oauthlib           3.2.0
pip                24.2
PyGObject          3.42.1
PyJWT              2.3.0
pyparsing          2.4.7
python-apt         2.4.0+ubuntu3
SecretStorage      3.3.1
setuptools         72.1.0
six                1.16.0
wadllib            1.3.6
wheel              0.44.0
zipp               1.0.0

Let’s also check the libraries installed in our virtual environment. But first, we need to activate it with:

root@01b5f3a5c984:/var/tmp/python-app# source my-venv/bin/activate
(my-venv) root@01b5f3a5c984:/var/tmp/python-app#

After activating the virtual environment, you should see the environment’s name (e.g., (my-venv)) preceding the terminal prompt, indicating that you are working within the virtual environment. Let’s now check the libraries installed in it:

(my-venv) root@01b5f3a5c984:/var/tmp/python-app# pip list
Package    Version
---------- -------
pip        24.0
setuptools 65.5.0

[notice] A new release of pip is available: 24.0 -> 24.2
[notice] To update, run: pip install --upgrade pip
(my-venv) root@01b5f3a5c984:/var/tmp/python-app#

Perfect, at this point you are fully set up to install project dependencies and run it with Python version 3.11. Everything done from now on will only affect our environment and will not impact the operating system. For example, if we want to install the pandas library:

pip install pandas

Next, let’s verify if it is actually installed for us:

(my-venv) root@01b5f3a5c984:/var/tmp/python-app# pip list
Package         Version
--------------- -----------
numpy           2.0.1
pandas          2.2.2
pip             24.0
python-dateutil 2.9.0.post0
pytz            2024.1
setuptools      65.5.0
six             1.16.0
tzdata          2024.1

It’s important to note that once you want to exit this environment, you only need to execute:

desactivate

Summary

In this way, we successfully created our virtual environment, ready to be used on Ubuntu, and avoided conflicts with the operating system’s library versions. This is a practical and useful method for running our projects in isolated Python environments. I hope this has been helpful, and I wish you success in your projects. Good luck.

Yuniel Alvarez

Leave a Reply