Python is one of the most widely used programming languages in the world. The syntax is simple and easy to learn, and Python is a popular choice for beginners and those with work experience. Python is a versatile programming language. It can be used to build various applications, from simple scripts to complex machine learning algorithms.
CentOS 8 includes Python 3.6. You can install or upgrade Python to this version through the dnf
tool.
At the time of writing, Python 3.8 is the latest major release of the Python language. It contains many new features, including assignment expressions, positional-only parameters, f-strings support, etc. Python 3.8 is not available in the standard CentOS 8 software source.
This guide explains how to build Python 3.8 from source on CentOS 8. We will also show you how to create a virtual environment.
Compiling Python from source code requires a C/C++ compiler and other development software packages. The first thing is to install the necessary packages on CentOS 8 to build Python from source code. To do this, run the following command as root or another user with sudo privileges:
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
Use wget
to download the latest source code from Python download page. Now, the latest version number of Python 3.8 is: 3.8.1
. If there is a newer version for download, modify the VERSION
variable in the following command line:
VERSION=3.8.1
wget https://www.python.org/ftp/python/${VERSION}/Python-${VERSION}.tgz
When the download is complete, unzip the compressed package:
tar -xf Python-${VERSION}.tgz
Switch to the Python source directory and run the configure
script. It will perform a series of tests on your current system to ensure that all dependencies have been met:
cd Python-${VERSION}./configure --enable-optimizations
- - The enable-optimizations
option optimizes Python binaries by running multiple tests. This will make the build process slower.
Start the build of Python 3.8 by running the following command:
make -j 4
Modify this -j
parameter according to your processor core number. You can find your processor core count by running nproc
.
Once the build process is over, install the Python binaries:
sudo make altinstall
Please do not use the standard make install
, because it will overwrite the default system Python binary package.
that's it. Python 3.8 has been installed on your CentOS system, and you can start using it. Enter the following command to verify:
python3.8--version
The output will show the version number of Python:
Python 3.8.1
The Python virtual environment is a self-contained directory tree that contains a Python installation and a series of additional packages. It allows you to install Python modules in a separate location in the specified project instead of installing it globally. In this way, you don't have to worry about affecting other Python projects.
In this example, we will create a new Python 3.8 project named my_app
in the user's home directory.
First, create the project directory and switch to it:
mkdir ~/my_app && cd ~/my_app
Run the following command in the project root directory to create a virtual environment named my_app_venv
:
python3.8-m venv my_app_venv
Activate this environment:
source my_app_venv/bin/activate
Once activated, the script prompt will add the name of the environment as a prefix. Starting with Python 3.4, when creating a virtual environment, pip and Python's package manager will be installed by default.
In a virtual environment, you can use pip
instead of pip 3.8
and python
instead of python3.8
:
python -v
Python 3.8.1
Once you have done all the work, you can deactivate this environment. Type deactivate
and you will return to your normal shell.
deactivate
We have shown you how to install Python 3.8 on a CentOS 8 machine and create a virtual environment.
Now you can start developing your Python3 project
Recommended Posts