How To Upgrade Python On Linux – Using Apt Repository Method

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.

  1. Update your package list first:
  2. sudo apt update
  3. Upgrade existing Python packages:
  4. sudo apt upgrade python3
  5. If a newer version is available in the repositories, this will install it. Check the version again:
  6. 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.

  1. Add the PPA:
  2. sudo add-apt-repository ppa:deadsnakes/ppa
  3. Update your package list:
  4. sudo apt update
  5. Install the desired Python version, for example 3.12:
  6. sudo apt install python3.12
  7. Set it as the default (optional but common):
  8. 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.

  1. For Fedora, update the system first:
  2. sudo dnf update
  3. Check available Python versions:
  4. dnf search python3
  5. Install a specific version, like Python 3.12:
  6. sudo dnf install python3.12
  7. For CentOS 7 with YUM:
  8. 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.

  1. Enable the SCL repository:
  2. sudo yum install centos-release-scl
  3. Install Python 3.8 from SCL:
  4. sudo yum install rh-python38
  5. Enable it for your session:
  6. 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.

  1. Install build dependencies first:
  2. 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
  3. Download the latest Python source from python.org:
  4. wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz
  5. Extract the archive:
  6. tar -xf Python-3.12.0.tgz
  7. Navigate into the directory:
  8. cd Python-3.12.0
  9. Configure the build:
  10. ./configure --enable-optimizations
  11. Compile using multiple cores:
  12. make -j $(nproc)
  13. Install the compiled Python:
  14. 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.

  1. Install pyenv dependencies:
  2. 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
  3. Install pyenv using the installer script:
  4. curl https://pyenv.run | bash
  5. Add pyenv to your shell configuration (~/.bashrc or ~/.zshrc):
  6. echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc
    echo 'eval "$(pyenv init -)"' >> ~/.bashrc
    source ~/.bashrc
  7. List available Python versions:
  8. pyenv install --list
  9. Install a specific version, like 3.12.0:
  10. pyenv install 3.12.0
  11. Set it as the global default:
  12. 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.

  1. Check the Python version:
  2. python3 --version
  3. Test pip:
  4. pip3 --version
  5. Run a simple Python script:
  6. python3 -c "print('Hello, upgraded Python!')"
  7. Check that system tools still work:
  8. 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.