Install python 3.10.4 from source on Rocky Linux or CentOS

The following commands are executed as a root user.

dnf -y install openssh-server policycoreutils-gui sshpass sqlite-devel
dnf groupinstall "Development Tools" -y
dnf install openssl-devel libffi-devel bzip2-devel -y
md5sum Python-3.10.4.tgz # in order to verify the integrity of your download
tar -xvzf Python-3.10.4.tgz
cd Python-3.10.4
./configure --enable-optimizations && make altinstall
pip3.10 -V
# pip 22.1 from /usr/local/lib/python3.10/site-packages/pip (python 3.10)
python3.10 --version
# Python 3.10.4

Virtual Python environment

You can install several virtual python environments on your Linux OS. It will allow you to deploy a distinct version of python for each of these virtual environments and also distinct packages with distinct versions.

It’s very interesting for developers who have to work with several python versions, or for servers which hosts several python applications that required specific python and specific packages.

What’s about pip vs pipenv vs poetry vs pdm?

What have pip, pipenv, poetry and pdm in common? the four ensure the management of the dependencies

pip is the “old way” to do it, but still used, and you can use it combined to python module venv in order to create virtual environment.

Warning: you must be aware that calling pip install <package> or pip update <package> outsite of a virtual environment, will have an action on the packages system-wide. Then it will affect all applications of your OS that depends on your OS python version. Maybe what you want is calling pip install/update inside a virtual environment in order to affect only the virtual environment

pipenv appeared in 2017 and is the official way to manage package and is recommended on

The advantages of pipenv over pip:

  • pip and python module venv are included, they work together
  • managing a requirements.txt file as you had to do with pip, was sometimes problematic, so Pipenv uses the upcoming Pipfile and Pipfile.lock instead, which is superior for basic use cases. For example you use a package A that uses package D version 1.0 and you use a package B that uses the package D version 2.1, it quickly becomes a brainteaser => A better pip workflow – Kenneth Reitz (creator of pipenv)
  • security enhance: hases are used everywhere
  • you can have a look to your dependency graph with pipenv graph
  • Streamline development workflow by loading .env files (environment variables in local development)

it’s also well explained here

poetry and pdm are recents, recommend their use only if pipenv doesn’t respond to your needs (only use them for very specific cases).

How create your virtual environment and manage your dependencies with pipenv vs pip + python venv module?

The best way: pipenv

As explained before the best solution is to use pipenv, but we will see also how it was before pipenv

# suppose that you have a user called ansible
su - ansible
# Create your project directory and go inside
mkdir projectdirectory
cd projectdirectory
pipenv --python 3.10

The output:

[ansible@ansible ansible]$ pipenv --python 3.10
Creating a virtualenv for this project...
Pipfile: /home/ansible/ansible/Pipfile
Using /usr/local/bin/python3.10 (3.10.4) to create virtualenv...
⠇ Creating virtual environment...created virtual environment in 1395ms
  creator CPython3Posix(dest=/home/ansible/.local/share/virtualenvs/ansible-ZNHM7g56, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/ansible/.local/share/virtualenv)
    added seed packages: pip==22.0.4, setuptools==62.1.0, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
✔ Successfully created virtual environment!
Virtualenv location: /home/ansible/.local/share/virtualenvs/ansible-ZNHM7g56
Creating a Pipfile for this project...

pipenv –python 3.10: this create the virtual environment, we indicate that we want to use python3.10 in this virtualenv. This means that you need a version of python3.10 installed on your server, for example on my server python 3.10.4 is installed (but if you dig in my server you’ll see other python versions like 3.6).

Note: this command doesn’t create the virtual environment inside the project directory (called here ansible) but inside a “.local/share/virtualenvs/<directory>” (here in the output you can see .local/share/virtualenvs/ansible-ZNHM7g56)

[ansible@ansible ansible]$ ls
[ansible@ansible ansible]$

How to use your virtual environment?

it’s simple, go inside your directory where you have executed pipenv –python <python_version> and you can execute this command in order to open a shell in the virtual environment:

pipenv shell
[ansible@ansible ansible]$ pipenv shell
Launching subshell in virtual environment...
 . /home/ansible/.local/share/virtualenvs/ansible-ZNHM7g56/bin/activate
[ansible@ansible ansible]$  . /home/ansible/.local/share/virtualenvs/ansible-ZNHM7g56/bin/activate
(ansible) [ansible@ansible ansible]$ python --version
Python 3.10.4
(ansible) [ansible@ansible ansible]$ exit
[ansible@ansible ansible]$

as you can see in the output, the bin/activate of your virtual environment is called (it’s similar as you have to do manually with pip + python venv module), and then the display of your shell prompt has changed and is now prefixed with (ansible)

Here in the example ansible is the user login and also the project directory, but what is between parentheses is the project directory (the one containing the Pipfile)

When we execute python –version it will display the python version configured in our virtual environment.

exit allow us to exit our virtual environment

You can alors simply execute on command inside our virtual environment without activating it (you still need to be inside the directory containing the Pipfile):

[ansible@ansible ansible]$ pipenv run python --version
Python 3.10.4
[ansible@ansible ansible]$

How to deploy your dependencies in other environment like production?

pipenv lock -r > requirements.txt

And then in your production host:

pipenv install -r requirements.txt

The standard way (the old way): venv + pip

Note: before python 3.3 you had to use the command virtualenv. With python 3.3 and later, you have to use the module venv of python

# before python 3.3
virtualenv --python=/usr/bin/python2.6 <path/to/new/virtualenv>
# since python 3.3
python<version> -m venv <virtualDirEnvName>
# create your project directory
mkdir project_directory
cd project_directory
# inside, create your virtual environment with the command:
# python<version> -m venv <virtualDirEnvName>
# python<version> is the python version you want to use inside your virtual environment
python3.9 -m venv myvenv

Your virtual environment is now created, and it has been created inside the directory, you can ls and see the <virtualDirEnvName> has been created, here it’s myvenv. To activate it and use commands inside:

source myvenv/bin/activate


$> source myvenv/bin/activate
(env) $> pip --version
pip 21.2.3 from /home/ansible/myproject_directory/myvenv/lib/python3.9/site-packages/pip (python 3.9)
(env) $> pip install ansible
(env) $> deactivate

Note: to exit the virtual environment you call ‘deactivate’

How deploy your dependencies in other environment like production?

inside your virtual environment execute the following:

pip freeze > requirements.txt
(env) $> pip freeze > requirements.txt

In your target environment (for example production environment), you can deploy the dependencies like this:

(env) $> pip install -r requirements.txt

where requirements.txt is your file created in the previous command pip freeze.