How to Set Up Python on AlmaLinux/Rocky Linux

AlmaLinux/Rocky Linux ship with Python 3 pre-installed for system use, but application development typically benefits from installing additional versions and proper tooling. This guide covers the complete setup.

Checking the Pre-Installed Python Version

python3 --version

Avoid modifying or replacing the system Python directly — it's used by dnf and other system tools; installing additional versions alongside it is the recommended approach.

Installing a Specific Additional Python Version

sudo dnf install python3.12 python3.12-pip -y

AlmaLinux/Rocky's repositories (including EPEL, see Understanding EPEL and Additional Repositories for AlmaLinux/Rocky Linux) often provide several specific Python versions as separate packages.

Using pyenv for More Flexible Version Management

sudo dnf groupinstall "Development Tools" -y
sudo dnf install openssl-devel bzip2-devel libffi-devel zlib-devel readline-devel sqlite-devel -y
curl https://pyenv.run | bash

See How to Manage Multiple Python Versions with pyenv — usage is identical once installed; these are the RHEL-family-specific build dependency packages needed before pyenv can compile Python from source.

Creating a Virtual Environment

python3.12 -m venv venv
source venv/bin/activate

See How to Set Up a Python Virtual Environment Correctly — identical concept and commands to other distributions.

Installing pip Packages

pip install django flask requests

Deploying a Django/Flask Application with Gunicorn

pip install gunicorn
gunicorn myapp:app --bind 0.0.0.0:8000

See How to Deploy a Django/Flask Application with Gunicorn and Nginx for the complete deployment pattern — the application-level setup is identical; only the initial OS-level package installation differs from Debian-based distributions.

Setting Up as a systemd Service

sudo nano /etc/systemd/system/myapp.service
[Unit]
Description=My Python Application

[Service]
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/venv/bin/gunicorn myapp:app --bind 127.0.0.1:8000
User=appuser
Restart=always

[Install]
WantedBy=multi-user.target

SELinux Considerations for Python Applications

See Understanding SELinux Basics on AlmaLinux/Rocky Linux — if your application needs to write to specific directories or bind to particular ports, verify SELinux policy allows this; audit.log denials are the primary diagnostic signal if something unexpectedly fails despite correct application-level configuration.

Configuring firewalld

sudo firewall-cmd --permanent --add-port=8000/tcp
sudo firewall-cmd --reload

Common Errors

pyenv compilation fails due to missing dependencies — ensure all the listed -devel packages are installed; RHEL-family distributions split development headers into separate packages from the runtime libraries, a common source of build failures if overlooked.

Continue Reading

Browse more articles in AlmaLinux & Rocky Linux.

  • python almalinux, python rocky linux, pyenv rhel, python virtual environment almalinux
  • 0 brukere syntes dette svaret var til hjelp
Var dette svaret til hjelp?

Relaterte artikler

AlmaLinux vs Rocky Linux vs CentOS: What Happened to CentOS?

If you're choosing a RHEL-compatible Linux distribution for your VPS, understanding the CentOS...

How to Get Started with AlmaLinux/Rocky Linux on a VPS

This guide covers the essential first steps after deploying a new AlmaLinux or Rocky Linux VPS...

How to Use dnf: The Package Manager for AlmaLinux & Rocky Linux

dnf (Dandified YUM) is the package manager for AlmaLinux, Rocky Linux, and other RHEL-family...

How to Configure firewalld on AlmaLinux/Rocky Linux

firewalld is the default firewall management tool on AlmaLinux and Rocky Linux, using a...

How to Install Nginx on AlmaLinux/Rocky Linux

This guide covers installing and configuring Nginx on AlmaLinux or Rocky Linux, including the...