How to install Python 3.8 on CentOS 8
Python is one of the most widely used programming languages in the world. With a simple and easy-to-learn syntax, Python is a popular choice for both beginners and experienced developers. Python is a fairly versatile programming language. It can be used to create all kinds of applications, from simple scripts to complex machine learning algorithms.
CentOS 8 includes Python 3.6, which can be installed or upgraded using the dnf tool.
At the time of writing, Python 3.8 is the latest major release of the Python language. It includes many new features such as assignment expressions, positional only parameters, f-string support, and more. Python 3.8 is not available in the standard CentOS 8 repositories.
This article explains how to build Python 3.8 on CentOS 8 from source. We’ll also show you how to create a virtual environment.
Installing Python 3.8 on CentOS 8
Compiling Python from source requires a C / C ++ compiler and other developer packages. The first thing to do is install the packages required to build Python from source on CentOS 8. To do this, run the following commands as root or sudo:
sudo dnf groupinstall 'development tools'
sudo dnf install bzip2-devel expat-devel gdbm-devel ncurses-devel openssl-devel readline-devel sqlite-devel tk-devel xz-devel zlib-devel wget
Download the latest source code from the Python download page using wget. Currently the latest version of Python is 3.8 3.8.1. If a newer version is available for download, change the VERSION variable in the command below:
VERSION=3.8.1wget https://www.python.org/ftp/python/${VERSION}/Python-${VERSION}.tgz
When the download is complete, unpack the gzip archive:
tar -xf Python-${VERSION}.tgz
Change to your Python source directory and run the configure script, which performs a series of checks to ensure that all dependencies are present on your system:
cd Python-${VERSION}./configure --enable-optimizations
The –enable-optimizations option optimizes binary Python by running multiple tests. This slows down the build process.
Start the Python 3.8 build process by running:
make -j 4
Change -j to match the number of cores in your processor. You can find the number by typing nproc.
After completing the build process, install the Python binaries:
sudo make altinstall
Please do not use the make install standard as it will overwrite the default system Python binary.
That’s all. Python 3.8 has been installed on your CentOS system and you can start using it. Check it out by typing:
python3.8 --version
The output should show the Python version:
Python 3.8.1
Creating a virtual environment
The Python virtual environment is a self-contained directory tree that includes Python installation and a number of additional packages. This allows you to install Python modules in a project-specific isolated location rather than installing globally. This way, you don’t have to worry about the impact on other Python projects.
In this example, we will create a new Python 3.8 project named my_app inside the user’s home directory.
First, create a project directory and switch to it:
mkdir ~/my_app && cd ~/my_app
From within the project root, run the following command to create a virtual environment named my_app_venv:
python3.8 -m venv my_app_venv
Activate environment:
source my_app_venv/bin/activate
Upon activation, the shell prompt will be prefixed with the environment name. Starting with Python 3.4, when pip building virtual environments, the package manager for Python is installed by default.
In a virtual environment, you can use pip instead of pip3.8 and python instead of python3.8:
python -v
Python 3.8.1
Once you’re done deactivating the environment, type deactivate and you will be returned to your normal shell.
deactivate
Output
We showed you how to install Python 3.8 on your CentOS 8 machine and how to create a virtual environment. You can now start developing your Python 3 projects.
If you have any questions or feedback, feel free to comment below.