Python is one of the most popular, versatile and beginner-friendly programming languages used by millions of developers worldwide. Known for its simplicity, vast library ecosystem and cross-platform compatibility, Python serves diverse use cases – from automating mundane tasks and developing websites to building complex machine learning models.
The latest major release – Python 3.11 – was made available on October 17th, 2022 packed with performance improvements, new features and library upgrades to help Python programmers be more productive.
Compared to previous versions, key highlights of Python 3.11 include:
-
Faster speed – Improved CPython interpreter performance thanks to optimizations in garbage collector, faster function calling and new JIT compiler.
-
Enhanced syntax – New syntax elements like structural pattern matching to simplify complex data processing.
-
Better memory management – Lower memory usage with lazy stdlib imports and smaller .pyc files.
-
Expanded core libraries – Updates in built-in modules like statistics, asyncio, dataclasses etc.
For Python developers, upgrading to the latest feature-rich Python 3.11 can lead to faster program execution, simpler coding with new language capabilities and access to improved libraries. This tutorial provides step-by-step instructions for compiling and setting up Python 3.11 from source on Linux Mint 21.
Why Compile from Source?
Instead of installing Python from default OS repositories, compiling it from the official source code comes with advantages like:
-
Easy upgrades to latest Python versions compared to waiting for distro packages.
-
Fine-tuned control over compilation flags for performance gains.
-
Build optimized Python for your specific system hardware.
-
Isolate multiple side-by-side Python installs more easily.
-
Take advantage of latest bug fixes and features faster.
-
Customize linked libraries based on needs.
Of course, compiling does take more effort compared to apt installs. But the flexibility and control gains are worth it for many Python developers, especially those relying on new language features or key performance optimizations shipped with latest releases like Python 3.11.
Prerequisites
Update package index and install compiler toolchain and Python build dependencies:
sudo apt update
sudo apt install build-essential checkinstall libreadline-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev
Download Python 3.11 Source Code
Download latest Python source tarball from python.org site:
cd /tmp
wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tgz
Validate integrity via SHA-256 hash:
echo "d7837121dd5652a05fef932ba96691bd6ce67d36dfc4f460b63be7aa80fc8b95 Python-3.11.0.tgz" | sha256sum -c
Compile and Install Python 3.11
Extract source, configure build, compile:
tar -xf Python-3.11.*
cd Python-3.11.*
./configure --enable-optimizations
make -j 8
<- Screenshot showing configure and make commands ->
The key steps here are:
-
Enable optimizations via configure flags to boost Python performance.
-
Parallel compile with multiple jobs for faster build process.
Finally install the freshly built Python:
sudo make altinstall
The altinstall ensures that /usr/bin/python still points to the system Python instead of being replaced by the new 3.11 install.
Set Up Virtual Environment
While the python3.11 binary is now available globally, it‘s good practice to encapsulate Python projects and their dependencies by creating virtual environments.
Install virtualenv via pip:
python3.11 -m pip install --user virtualenv
Then create a project-specific virtual env:
python3.11 -m venv myprojectenv
Activate the environment:
source myprojectenv/bin/activate
Later you can use deactivate to exit the virtual env, but still keep it around for that project‘s purposes whenever needed again.
Such virtual environments provide isolation and avoid polluting global Python site-packages space.
Make Python 3.11 the Default System Version
If aiming to make Python 3.11 the default interpreter instead of older distro-provided Python, update alternatives:
sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.11 1
Then give higher priority to the new alternative:
sudo update-alternatives --set python /usr/local/bin/python3.11
Verify that python --version now shows 3.11 as expected.
Verify Successful Python 3.11 Installation
Check newly set up Python 3.11 install via interpreter:
python3.11
>>> import sys
>>> print(sys.version)
Output should indicate active Python 3.11:
3.11.0 (default, Oct 17 2022, 10:03:40)
[GCC 11.3.0]
<- Screenshots showing python3.11 version ->
Additionally verify linkage of system libraries against the latest Python 3.11:
ldd $(which python3.11)
This helps catch any missing shared library dependencies.
With the base install completed, Python 3.11 is ready for action!
Python 3.11 in Action
Let‘s take a quick look at Python 3.11‘s new structural pattern matching capability:
match points:
case [x, y]:
print(f"Coord: {x}, {y}")
case []:
print("No points given!")
points = [1, 2]
# Prints Coord: 1, 2
The match-case constructs make matching and extracting data from complex structures more convenient.
Compared to prior workarounds like repetitive isinstance() checks, the new matching syntax makes intention clearer and logic simpler.
Managing Python Packages
The Python Package Index (PyPI) contains over 300,000 packages covering needs from machine learning to data analysis, web frameworks and beyond.
It‘s recommended to isolate project-specific dependencies via virtual environments instead of system-wide installs.
Use pip to fetch packages:
pip install numpy scipy pandas
For more advanced management needs, consider tools like:
- poetry – Dependency management and publishing
- pip-tools – Deterministic dependency pins
- pur – APK-style Python packages
Such tools help overcome issues like "dependency hell", ensuring repeatable installations across environments.
Alternative Python Install Options
While this guide covers compiling latest Python from source, other installation options include:
Conda
Anaconda and Miniconda provide popular Python data science distributions with the conda package manager. Conda natively handles binary packages, virtual envs and language runtimes.
apt packages
Debian/Ubuntu distros serve Python via apt repos, but usually lag behind latest versions. Also lacks virtual env capabilities.
Cross-platform installers
Python.org offers user-friendly all-in-one Windows and Mac installers with bundled packages and virtual environment capabilities.
So in summary, compiling Python 3.11 from source helps unlock the latest performance and features on Linux systems while enabling advanced deployment and management approaches for production needs.
Conclusion
Upgrading to modern Python releases like 3.11 is highly recommended for taking advantage of faster execution via under-the-hood optimizations along with new syntax for simplified coding. By compiling Python from source, developers gain more control over tuning the build to their specific hardware and use cases.
This step-by-step guide covered downloading, compiling and installing the latest Python 3.11 from source on Linux Mint 21 – including tips for managing virtual environments and packages. Python 3.11‘s pattern matching capability was also demonstrated as a glimpse into modern language features that can streamline development.
For keeping up with the fast pace of Python evolution across data science, web and automation use cases – developing against latest stable versions is advised. Hopefully this tutorial helps you get the most recent Python 3.11 up for increased productivity.


