Python 3.12 brings exciting new features and performance improvements. This guide will walk you through installing Python 3.12 on AlmaLinux from source, ensuring you have the latest version with all optimizations enabled.
Prerequisites
Before starting, ensure you have:
- A fresh installation of AlmaLinux
- Root or sudo access
- Basic familiarity with the command line
Step 1: Update System Packages
First, update your system to ensure all packages are current:
sudo dnf update -y
This command updates all installed packages to their latest versions, ensuring compatibility and security.
Step 2: Install Development Tools
Python compilation requires several development tools and libraries. Install them with:
sudo dnf groupinstall "Development Tools" -y
Additionally, install specific dependencies needed for Python:
sudo dnf install wget openssl-devel libffi-devel bzip2-devel -y
These packages include:
wget
: For downloading Python source codeopenssl-devel
: For SSL/TLS supportlibffi-devel
: For ctypes Python modulebzip2-devel
: For bz2 compression support
Step 3: Download Python 3.12 Source Code
Navigate to a temporary directory and download the Python source:
cd /tmp
wget https://www.python.org/ftp/python/3.12.1/Python-3.12.1.tgz
Extract the downloaded archive:
tar xzf Python-3.12.1.tgz
cd Python-3.12.1
Step 4: Configure and Compile Python
Configure the build with optimizations:
./configure --enable-optimizations
The --enable-optimizations
flag enables Profile Guided Optimization (PGO) and Link Time Optimization (LTO), resulting in a Python binary that’s 10-20% faster.
Compile Python using multiple CPU cores:
make -j$(nproc)
The -j$(nproc)
flag uses all available CPU cores, significantly speeding up compilation.
Step 5: Install Python 3.12
Install Python using altinstall
to avoid overwriting the system Python:
sudo make altinstall
Using altinstall
instead of install
prevents replacing the default python3
binary, which could break system tools.
Step 6: Verify Installation
Check that Python 3.12 is installed correctly:
python3.12 -V
You should see:
Python 3.12.1
Step 7: Install pip for Python 3.12
Pip should be installed automatically. Verify with:
pip3.12 -V
Creating Virtual Environments
It’s best practice to use virtual environments for Python projects:
python3.12 -m venv myproject
source myproject/bin/activate
Installing Common Packages
With your virtual environment activated, install packages as needed:
pip install requests numpy pandas django flask
Setting Python 3.12 as Default (Optional)
If you want to use python3
to invoke Python 3.12:
sudo alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.12 1
Troubleshooting Common Issues
Missing Dependencies
If you encounter errors during compilation, you might be missing dependencies:
sudo dnf install zlib-devel readline-devel tk-devel \
sqlite-devel xz-devel gdbm-devel ncurses-devel
SSL Module Not Available
If the SSL module isn’t available, ensure OpenSSL development files are installed:
sudo dnf install openssl-devel
Then reconfigure and rebuild Python.
Permission Denied Errors
If you get permission errors during installation, ensure you’re using sudo
for the installation step.
Performance Tips
Enable JIT Compilation
Python 3.12 includes experimental JIT compilation. While not enabled by default, you can experiment with it in future releases.
Use Built-in Optimizations
Python 3.12 includes numerous performance improvements:
- Faster attribute access
- Optimized error handling
- Improved memory usage
Conclusion
You’ve successfully installed Python 3.12 on AlmaLinux! This latest version brings performance improvements, new syntax features, and better error messages. Whether you’re developing web applications, data science projects, or system scripts, Python 3.12 on AlmaLinux provides a robust and efficient platform.
Remember to always use virtual environments for your projects and keep your Python installation updated with the latest security patches.