How To Update Python Version Linux : Compiling From Source Code

Updating the Python version on Linux can be done through the official repositories or by adding a Personal Package Archive. If you are wondering how to update python version linux, you have come to the right place. This guide walks you through every method, from simple commands to compiling from source, so you can get the latest Python running smoothly on your system.

Python is a dynamic language that evolves quickly. New versions bring better performance, security fixes, and fresh features. Sticking with an outdated version can leave you vulnerable or unable to run modern libraries. Whether you are a developer, a data scientist, or just a hobbyist, keeping Python up to date is essential.

Linux distributions often ship with a stable but older Python version. For example, Ubuntu 20.04 comes with Python 3.8, while the latest stable release is Python 3.12. Upgrading manually gives you access to new syntax, improved error messages, and faster execution.

Below, we cover multiple approaches. Choose the one that fits your comfort level and system setup. No prior expert knowledge is needed—just follow the steps carefully.

How To Update Python Version Linux

This section provides the core instructions for updating Python on various Linux distributions. We break it down by package manager and method.

Check Your Current Python Version

Before making any changes, know what you are working with. Open a terminal and run:

python3 --version

Or, if you have Python 2 installed:

python --version

Note the output. It will look something like Python 3.8.10. This tells you the exact version and patch level. Write it down for reference.

You can also check if Python is linked to a specific location:

which python3

This shows the path, usually /usr/bin/python3. Knowing this helps avoid conflicts later.

Update Python Using APT (Debian/Ubuntu)

For Debian-based systems like Ubuntu, the easiest method is using the Advanced Package Tool (APT). However, the default repositories often lag behind the latest release. Here is how to get a newer version.

Step 1: Update Package List

sudo apt update

This refreshes the list of available packages from your configured repositories.

Step 2: Install Python from Repos

sudo apt install python3

This command installs the latest Python version available in your distribution’s official repos. On Ubuntu 22.04, you might get Python 3.10. For newer versions, you need a PPA.

Step 3: Add the DeadSnakes PPA

The DeadSnakes team maintains a Personal Package Archive with multiple Python versions. Add it with:

sudo add-apt-repository ppa:deadsnakes/ppa

Press Enter when prompted. Then update again:

sudo apt update

Now you can install a specific version, like Python 3.12:

sudo apt install python3.12

After installation, verify it:

python3.12 --version

Step 4: Set as Default (Optional)

If you want python3 to point to the new version, use update-alternatives:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 1

Then configure the priority:

sudo update-alternatives --config python3

Select the number corresponding to Python 3.12. Be cautious—some system tools rely on the default Python version. Changing it may break things like apt.

Update Python Using YUM/DNF (Fedora/RHEL/CentOS)

Red Hat-based distributions use YUM or DNF. The process is similar but uses different repositories.

Step 1: Check Available Versions

dnf list available python3*

This shows all Python packages in the enabled repos.

Step 2: Install the Latest

sudo dnf install python3

On Fedora, this often gives you the latest stable release. For example, Fedora 39 includes Python 3.12.

Step 3: Enable EPEL for Older RHEL/CentOS

If you are on CentOS 7 or RHEL 8, enable the Extra Packages for Enterprise Linux (EPEL) repository:

sudo yum install epel-release

Then install Python:

sudo yum install python3

For even newer versions, consider using Software Collections (SCL) or compiling from source.

Update Python Using Pacman (Arch Linux)

Arch Linux users enjoy rolling releases with the latest software. Updating Python is straightforward.

Step 1: Full System Update

sudo pacman -Syu

This updates all packages, including Python, to the latest versions in the repositories.

Step 2: Install Python if Missing

sudo pacman -S python

Arch typically has the newest stable Python. Check the version after update:

python --version

Compile Python From Source

If your distribution does not offer the version you need, compiling from source gives you full control. This method works on any Linux distro.

Step 1: Install Build Dependencies

First, install the tools required to compile Python:

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget

For Fedora/RHEL, use:

sudo dnf groupinstall "Development Tools"
sudo dnf install openssl-devel bzip2-devel libffi-devel

Step 2: Download the Source Code

Visit the official Python downloads page or use wget:

wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz

Replace the version number with the one you want. Extract the tarball:

tar -xf Python-3.12.0.tgz
cd Python-3.12.0

Step 3: Configure and Compile

Run the configure script to check dependencies:

./configure --enable-optimizations

The --enable-optimizations flag enables Profile Guided Optimization (PGO), which makes Python run faster. It takes longer to compile but is worth it.

Then compile:

make -j $(nproc)

The -j $(nproc) option uses all CPU cores to speed up compilation.

Step 4: Install

sudo make altinstall

Using altinstall instead of install prevents overwriting the default python3 binary. This avoids breaking system tools. The new version will be available as python3.12.

Step 5: Verify Installation

python3.12 --version

You should see the version you compiled.

Using Pyenv To Manage Multiple Versions

Pyenv is a tool that lets you install and switch between multiple Python versions easily. It is perfect for developers who need different versions for different projects.

Step 1: Install Pyenv

Use the installer script:

curl https://pyenv.run | bash

Follow the on-screen instructions to add pyenv to your shell configuration file (~/.bashrc or ~/.zshrc).

Step 2: Install a New Python Version

List available versions:

pyenv install --list

Install a specific version, for example:

pyenv install 3.12.0

This downloads and compiles Python automatically. It may take a while.

Step 3: Set Global or Local Version

To make this version the default system-wide:

pyenv global 3.12.0

Or for a specific project directory:

cd myproject
pyenv local 3.12.0

Verify with:

python --version

Update Python Using Conda (For Data Science)

If you use Anaconda or Miniconda, you can update Python within the conda environment.

Step 1: Update Conda Itself

conda update conda

Step 2: Update Python

conda install python=3.12

Or update to the latest in the current major version:

conda update python

Conda will resolve dependencies and install the new version. This method is safe for data science workflows because it does not affect system Python.

Potential Issues And Fixes

Updating Python can sometimes cause problems. Here are common ones and how to solve them.

Broken System Tools

If you replace the default python3 with a newer version, tools like apt or yum may break because they rely on the original Python. Always use altinstall or update-alternatives carefully. If things break, reinstall the original Python:

sudo apt install --reinstall python3

Missing Modules

After updating, some Python packages may not work. Reinstall them using pip:

pip3 install --upgrade package_name

Or upgrade all packages:

pip3 list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip3 install -U

Virtual Environment Issues

Virtual environments created with an older Python version may not work with the new one. Create fresh environments:

python3.12 -m venv myenv

Then activate it and reinstall your dependencies.

Best Practices For Python Updates

Follow these tips to keep your system stable.

  • Always backup important projects before updating.
  • Test new Python versions in a virtual environment first.
  • Do not remove the system Python—just add new versions alongside it.
  • Use pip within virtual environments to avoid conflicts.
  • Keep an eye on deprecation warnings when running old code.

Frequently Asked Questions

How Do I Update Python To The Latest Version On Ubuntu?

You can use the DeadSnakes PPA as shown above. Add the PPA, update, and install the desired version like sudo apt install python3.12. Alternatively, compile from source or use pyenv.

Is It Safe To Update Python On Linux?

Yes, if you do it correctly. Avoid replacing the system Python binary. Use altinstall or version managers like pyenv. This prevents breaking system utilities that depend on the default Python.

Can I Have Multiple Python Versions On Linux?

Absolutely. Tools like pyenv, conda, or manual compilation allow you to keep several versions side by side. You can switch between them per project or globally.

What Is The Difference Between Python And Python3 On Linux?

On most modern Linux distros, python points to Python 2 (if installed), while python3 points to Python 3. Some newer distros have deprecated Python 2 entirely. Always use python3 for Python 3 commands.

Why Does My Linux Distribution Not Have The Latest Python?

Distributions prioritize stability over bleeding-edge software. They test packages thoroughly before releasing updates. To get the latest Python, you must use third-party repositories, compile from source, or use a version manager.

Updating Python on Linux is a straightforward process once you understand the options. Whether you choose APT, DNF, source compilation, or pyenv, the key is to avoid overwriting the system Python. By following the steps in this guide, you can enjoy the latest features and improvements without breaking your system. Remember to check your current version first, choose the method that suits your needs, and test thoroughly. Happy coding!