To upgrade Python on Linux, you can use `apt` for Debian-based systems or `yum` for Red Hat-based distributions. This guide walks you through the process step by step, covering multiple methods to ensure you get the latest Python version installed safely.
Python is a core tool for many developers and system administrators. Keeping it updated gives you access to new features, security patches, and better performance. Whether you’re running Ubuntu, Fedora, or CentOS, upgrading Python doesn’t have to be complicated.
Let’s start with the basics and move into detailed instructions for each major Linux family. You’ll learn how to check your current version, choose the right upgrade method, and avoid common pitfalls.
Why Upgrade Python On Linux
Python releases new versions regularly. Each update brings bug fixes, improved speed, and sometimes breaking changes. If you’re stuck on an old version like 3.6 or 3.7, you might miss out on important security updates.
Many modern libraries and frameworks require Python 3.8 or higher. For example, TensorFlow and PyTorch often drop support for older versions. Upgrading ensures compatibility with the latest tools.
Also, Python 2 reached end-of-life in 2020. If you’re still using Python 2, upgrading to Python 3 is essential for security and support.
How To Upgrade Python On Linux
This section covers the main methods for upgrading Python across different Linux distributions. Choose the one that matches your system.
Check Your Current Python Version
Before upgrading, see what you have installed. Open a terminal and run:
python3 --version
Or for Python 2:
python --version
This tells you the exact version number, like Python 3.8.10 or Python 2.7.18. Write this down so you know where you’re starting from.
Upgrade Python On Debian Or Ubuntu Systems
Debian-based distributions use the Advanced Package Tool (APT). This method is straightforward and safe for most users.
- Update your package list first:
- Upgrade existing Python packages:
- If a newer version is available in the repositories, this will install it. Check the version again:
sudo apt update
sudo apt upgrade python3
python3 --version
However, official Ubuntu repos often lag behind the latest Python release. For example, Ubuntu 20.04 LTS ships with Python 3.8, but the latest stable Python might be 3.12. In that case, you need alternative methods.
Using A Personal Package Archive (PPA)
PPAs provide newer software versions for Ubuntu. The deadsnakes PPA is a popular choice for Python.
- Add the PPA:
- Update your package list:
- Install the desired Python version, for example 3.12:
- Set it as the default (optional but common):
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.12
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 1
This method gives you a fresh Python installation without breaking system tools that depend on the default version.
Upgrade Python On Red Hat, CentOS, Or Fedora Systems
Red Hat-based systems use YUM or DNF. Fedora uses DNF by default, while CentOS 7 uses YUM.
- For Fedora, update the system first:
- Check available Python versions:
- Install a specific version, like Python 3.12:
- For CentOS 7 with YUM:
sudo dnf update
dnf search python3
sudo dnf install python3.12
sudo yum install python3
Fedora usually has newer packages in its repositories. CentOS 7 might only offer Python 3.6 by default. For newer versions, consider using Software Collections (SCL) or building from source.
Using Software Collections (SCL) On CentOS
SCL lets you install multiple Python versions side by side.
- Enable the SCL repository:
- Install Python 3.8 from SCL:
- Enable it for your session:
sudo yum install centos-release-scl
sudo yum install rh-python38
scl enable rh-python38 bash
This method keeps your system Python untouched while giving you access to a newer version.
Upgrade Python Using The Source Code
Building from source gives you the absolute latest Python release, regardless of your distribution. It’s more involved but gives you full control.
- Install build dependencies first:
- Download the latest Python source from python.org:
- Extract the archive:
- Navigate into the directory:
- Configure the build:
- Compile using multiple cores:
- Install the compiled Python:
sudo apt update
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev
wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz
tar -xf Python-3.12.0.tgz
cd Python-3.12.0
./configure --enable-optimizations
make -j $(nproc)
sudo make altinstall
Using altinstall instead of install prevents overwriting your system’s default Python binary. The new version will be available as python3.12.
Upgrade Python Using Pyenv
Pyenv is a version management tool that lets you install and switch between multiple Python versions easily. It’s ideal for developers who need different versions for different projects.
- Install pyenv dependencies:
- Install pyenv using the installer script:
- Add pyenv to your shell configuration (~/.bashrc or ~/.zshrc):
- List available Python versions:
- Install a specific version, like 3.12.0:
- Set it as the global default:
sudo apt update
sudo apt install curl git build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
curl https://pyenv.run | bash
echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
source ~/.bashrc
pyenv install --list
pyenv install 3.12.0
pyenv global 3.12.0
Pyenv keeps each version isolated in your home directory. You can switch between versions per directory or globally.
Common Issues During Python Upgrade
Upgrading Python can sometimes cause problems. Here are the most frequent ones and how to fix them.
Broken System Tools
Many Linux system tools depend on the default Python installation. If you replace it with a newer version, tools like apt or yum might break.
Solution: Never remove the system Python. Instead, install additional versions alongside it. Use altinstall or pyenv to keep the system version intact.
Missing Modules Or Libraries
After upgrading, some Python modules might not be available. For example, distutils was removed in Python 3.12.
Solution: Install missing modules via pip. For system-wide packages, use sudo apt install python3-xyz for Debian-based systems.
Path Conflicts
If you have multiple Python versions, the wrong one might be called when you type python3. This can cause confusion.
Solution: Use update-alternatives on Debian or adjust your PATH variable. With pyenv, the pyenv global command handles this automatically.
Verify The Upgrade
After upgrading, confirm everything works correctly.
- Check the Python version:
- Test pip:
- Run a simple Python script:
- Check that system tools still work:
python3 --version
pip3 --version
python3 -c "print('Hello, upgraded Python!')"
sudo apt update
If all these commands run without errors, your upgrade was successful.
Best Practices For Python Version Management
To avoid headaches in the future, follow these guidelines.
- Use virtual environments for each project. This isolates dependencies and prevents conflicts.
- Keep your system Python untouched. Only upgrade it through official distribution updates.
- Use pyenv or similar tools for development work. They give you flexibility without risking system stability.
- Regularly check for Python updates. Subscribe to Python release announcements or use a package manager that notifies you.
- Test your applications after upgrading. Some code might break due to deprecated features.
Frequently Asked Questions
Can I Upgrade Python Without Sudo?
Yes, using pyenv or building from source in your home directory. These methods don’t require root privileges.
Will Upgrading Python Break My Existing Projects?
It can, if you replace the system Python. Using side-by-side installations with pyenv or virtual environments prevents this.
How Do I Upgrade Pip After Upgrading Python?
Run python3 -m pip install --upgrade pip. This ensures pip matches your new Python version.
What’s The Difference Between Python And Python3?
On most modern Linux systems, python points to Python 2 (if installed), while python3 points to Python 3. Always use python3 for clarity.
Is It Safe To Remove Old Python Versions?
Only if you’re certain no system tools depend on them. Use apt remove or yum remove with caution. Better to leave old versions untouched.
Conclusion
Upgrading Python on Linux is a straightforward process once you understand the tools available. Whether you use APT, YUM, source compilation, or pyenv, the key is to keep your system Python separate from your development versions.
Start by checking your current version, then choose the method that fits your distribution and needs. Test everything after the upgrade, and use virtual environments to keep projects organized.
With these steps, you can confidently upgrade Python and take advantage of the latest features and security improvements. Your Linux system will thank you.