Python is one of the most popular programming languages used today for everything from web development to data analysis. In this comprehensive guide, we will cover how to install and configure Python on CentOS 8 Linux.
Prerequisites
Before we get started, you should have a CentOS 8 server up and running. You‘ll also need root access to be able to install packages.
To check your CentOS version:
cat /etc/centos-release
To switch to the root user:
sudo su
Installing Python 3
The latest versions of Python 3 are already included in the CentOS 8 repositories. To install Python 3:
dnf install python3
This will install Python 3.6 by default along with pip, the Python package manager.
To verify, check the version:
python3 --version
Python 3.6.8
Python 3 is now installed and ready to use!
Managing Python Versions
If you want to install a specific version of Python instead of the default, there are a few different ways to manage multiple Python versions.
pyenv
pyenv is a useful tool to install and manage multiple versions of Python.
To install pyenv:
dnf install -y pyenv
Once installed, you can list the versions available to install:
pyenv install --list
Then install the desired version:
pyenv install 3.7.4
Set the Python version globally:
pyenv global 3.7.4
Or to set it for your current shell session only:
pyenv shell 3.7.4
Check that it is installed properly:
python --version
And to uninstall at any time:
pyenv uninstall 3.7.4
This makes it easy to switch between Python versions for different projects on the same server.
Software Collections
Software Collections are another good option for installing multiple versions. Python36 for example provides Python 3.6 and pip3.6 outside of the system-wide packages.
To setup the SCL repository:
yum install centos-release-scl
Then install Python 3.6:
yum install rh-python36
The commands will now be accessible with the scl prefix:
scl enable rh-python36 bash
python --version
Exit back to system python when done:
exit
This keeps Python versions isolated in separate environments.
Building from Source
Of course you can always install Python by compiling from source code as well.
First install the development tools:
dnf groupinstall "Development Tools"
Then download the source, configure, make and install:
curl -O https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz
tar xzf Python-3.8.1.tgz
cd Python-3.8.1
./configure --enable-optimizations
make -j 8
make altinstall
This will keep Python 3.8 alongside the system Python 3.6 without overriding it.
Setting up a Virtual Environment
It‘s considered best practice to create isolated Python environments for each application or project you work on. This prevents package version conflicts as different apps may rely on different versions of dependencies.
To install virtualenv:
pip3 install virtualenv
Then create an environment by specifying the Python version and site packages needed:
virtualenv my_app -p python3.6
Activate the environment:
source my_app/bin/activate
Now any packages installed will be isolated in this environment:
pip install pandas
pip freeze
And deactivate when done:
deactivate
This virtual env can be deleted without impacting the system Python packages.
Configuring Python Environment Variables
Environment variables make it easier to access Python without specifying the full path.
To check the current paths:
echo $PATH
Then open /etc/profile and append the path to Python executables.
For Python 3:
export PATH=$PATH:/usr/local/bin/python3:/usr/bin/python3
And for pip:
export PATH=$PATH:/usr/local/bin/pip3:/usr/bin/pip3
Reload the profile changes:
source /etc/profile
Now Python 3 and pip3 will be available system-wide.
Setting the System Python Version
Some Linux distributions come with Python 2 preinstalled as the system default python. In CentOS and RHEL 8, Python 3 takes precedence. But if you need to change this, update the symlinks:
ls -l /bin/python*
lrwxrwxrwx. 1 root root 7 Mar 28 2019 /bin/python -> python2
lrwxrwxrwx. 1 root root 16 Mar 28 2019 /bin/python2 -> /usr/bin/python2
lrwxrwxrwx. 1 root root 16 Mar 28 2019 /bin/python2.7 -> /usr/bin/python2.7
lrwxrwxrwx. 1 root root 9 Mar 28 2019 /bin/python3 -> python3.6
lrwxrwxrwx. 1 root root 21 Mar 28 2019 /bin/python3.6 -> /usr/bin/python3.6
Here Python 2.7 is configured as the default.
To make Python 3.6 the system python:
ln -sf /usr/bin/python3.6 /bin/python
This will symbolic link python to point to the Python 3.6 executable instead.
Packaging and Distributing Python Applications
For packaging Python code into distributable units, there are a few main formats:
a. Zipapp
Zipapp bundles modules, scripts, and resources into a standard zip file with a __main__.py file to make it executable.
To distribute a project as Zipapp:
pip3 install zipapp
zipapp myapp.py --output myapp.pyz
python3 myapp.pyz
b. Wheel
The wheel format condenses metadata and modules into a zipped package. Wheels are the standard for distributing libraries.
To package a project as a wheel:
pip3 wheel myapp
pip3 install dist/myapp-1.0-py3-none-any.whl
c. Source Distribution
A source archive or sdist contains metadata and project files to distribute Python code for other developers to build upon:
pip3 install setuptools
python3 setup.py sdist
pip3 install dist/myapp-1.0.tar.gz
d. Conda Package
Conda is another popular Python package manager geared for data science. Conda packages allow reproducible installations by bundling dependencies:
conda create myapp
conda build myapp
conda install myapp-1.0-py36habc123_0.tar.bz2
Pick the distribution format that suits your use case best.
Setting up a WSGI Application Server
For deploying production Python apps, a WSGI application server will run the application efficiently.
Some top options are:
-
Gunicorn – A Flask & Django compatible WSGI server designed for speed & low overhead. Often run behind Nginx.
-
uWSGI – Supports multiple app frameworks and configuration modes for optimized performance. Can also function as a reverse proxy and load balancer.
-
mod_wsgi – Integrates Python apps directly into the Apache module system. Enables scaling with prefork/worker MPMs.
To install uWSGI with Python 3 support:
pip3 install uwsgi
A basic example config:
[uwsgi]
http-socket = :9090
wsgi-file = app.py
master = true
processes = 4
threads = 2
This will start the app on port 9090 to handle production traffic.
For other options like Nginx/Gunicorn or Apache/mod_wsgi deployments, see our detailed guides on How to Set Up Django with Nginx, Gunicorn, and Docker or How To Serve Flask Applications with Gunicorn and Nginx on Ubuntu 18.04.
Conclusion
That covers how to get Python installed and configured on CentOS! The built-in dnf package manager makes it pretty easy.
We looked at methods for managing multiple versions, setting up virtual environments, configuring environment variables, distribution packaging, and using a WSGI application server.
There are so many possibilities of things you can build with Python. A few ideas:
- Web applications and APIs
- Automation & system administration scripts
- Data analysis, visualization, and machine learning
- Scientific computing
- Image processing
-Games and 3D graphics
Let me know in the comments about any cool Python projects you‘ve worked on recently!


