01.08.2023

Python 3 virtual environment on Ubuntu 22.04

Introduction

Python is an interpreted programming language. The author is Guido van Rossum, a developer from Holland. Python is a feature-rich language, and beginners quickly get used to the syntax of the language and program in a convenient text editor or use an integrated development environment. Using the Python language, you can get information about errors in the written code. With this guide, you will be able to install the latest version of Python on Ubuntu Server 22.04.

Preparing for installation

Before installing the packages, you need to follow our guide to running Ubuntu Server 22.04 as a standard user.

Download Python 3

Let's update the package index and run the command to update the packages to the latest releases:

sudo apt update && sudo apt upgrade -y

The “-y” key means to force update.
Checking the Python version goes like this:

python3 --version

Output is going to be like this:

#Output
Python 3.10.6

The next step is to install python3-pip in order to manage Python packages. Let's use the built-in command:

sudo apt install python3-pip -y

To install the matplotlib library, you must run the following command and the result is shown in Screen 1:

pip3 install matplotlib

Screen 1 - Installing the matplotlib library

To make sure the software environment is reliable, you need to install several packages

sudo apt install build-essential libssl-dev libffi-dev python3-dev

The first stage has been completed. We have updated the package index and updated obsolete packages, the current version of the pip3 package management system is installed.

Setting up a virtual environment

A virtual development environment on a production server is considered a great solution compared to running in a main development environment. In a virtual environment, you can edit and not damage the files of the main development environment. We can create as many virtual environments as we need. Each virtual environment is deployed in different directories on our server. The directories contain files for initializing the virtual environment.
The virtual environment is deployed using the installed venv (virtual environment) package:

sudo apt install python3-venv -y

Then let's create a directory called test:

mkdir test
cd test

Change to the first directory and use the following command to create a virtual environment called test_env:

python3 -m venv test_env

The result is shown in Screen 2.

Screen 2 - Create a virtual environment

The generated files configure the virtual environment to work separately from our host files. Activation of the environment is as follows, and to disable the environment, you must run the deactivate command:

source test/test_env/bin/activate

To disable the virtual environment, run the command:

deactivate

The results are shown in Screen 3.

Screen 3 - Activating and deactivating a virtual environment

In the figure, you can see that after launch, an inscription appears in front of the user name (test_env) indicating that all commands are executed in a virtual environment, the next step is to consider running a regular code written in the Python programming language.

Testing the virtual environment

After activation, you need to create a file with the extension .py:

vim thanks.py

И вставим следующий кусок кода:

print("Dear User,\n"
"Thank you for using tutorials from \n"
"Serverspace Team")

To run the program, do the following:

python3 thanks.py

And we get the following result, as shown in Screen 4.

Screen 4 - Running code in a virtual environment

At this point, the stage ends and in order to complete the

process of working in the virtual environment, we will execute the “deactivate” command and return to the normal environment.

Conclusions

In this instruction: