Home Programming How to Update and Manage Python Versions on Linux

How to Update and Manage Python Versions on Linux

Keeping Python versions up to date is essential for compatibility and security. This guide provides a step-by-step approach to updating and managing multiple Python versions on Linux, including installation, configuration, and switching between versions.

by John Horan
update python in linux

Updating Python on a Linux system is a crucial task for developers and system administrators. Keeping Python updated ensures you have the latest features, security patches, and performance improvements. Regardless of whether you are running scripts, developing applications, or managing servers, having the right version of Python is essential.

In this guide, I’ll walk you through updating Python on Ubuntu, Fedora, and CentOS. Each method includes detailed steps and examples, ensuring you can follow along regardless of your Linux distribution.

Checking your current Python version

Before we dive into the update process, let’s check which Python versions you currently have installed. Open your terminal and type:

python --version

or for Python 3:

python3 --version

You should see something like:

Python 3.8.10

Updating Python on Ubuntu

Method 1. Using the universe repository

You can use the universe repository to install newer Python versions without relying on external PPAs. This method ensures you’re using official Ubuntu repositories, which can be preferable for stability and security.

  1. Enable the universe repository:
    sudo add-apt-repository universe
    sudo apt update
    
  2. Install the desired Python version:Check which versions are available by searching the package index:
    apt-cache search python3
    

    Install the desired Python version, for example, Python 3.9:

    sudo apt install python3.9
    
  3. Set up alternatives to switch between Python versions:To manage multiple Python versions, use the update-alternatives tool:
    sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
    sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
    

    Now, you can switch between versions using:

    sudo update-alternatives --config python3
    

    You will see a list of installed Python versions. Choose the one you want to use.

Limitations on Ubuntu version

  • Older Ubuntu versions: If you’re running an older version of Ubuntu (e.g., 18.04), the universe repository may not include the latest Python versions. In such cases, you might need to upgrade your Ubuntu version or use the deadsnakes PPA for newer Python releases (Method 2 below).
  • Newer Ubuntu versions: Ubuntu 20.04 and later are more likely to have recent Python versions in the universe repository. However, it’s always a good idea to check the available packages using apt-cache search before proceeding.

Method 2: Using deadsnakes PPA (Personal Package Archive)

I love the simplicity of using PPAs for installing software on Ubuntu. The deadsnakes PPA is a great source for installing newer versions of Python.

Steps:

  1. Add the deadsnakes PPA:
    sudo add-apt-repository ppa:deadsnakes/ppa
    sudo apt update
    
  2. Install the desired Python version:Let’s say you want to install Python 3.10:
    sudo apt install python3.10
    
  3. Set up alternatives to switch between Python versions:I find update-alternatives very handy for managing multiple Python versions. Here’s how to configure it:
    sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
    sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2
    

    Now, you can switch between versions using:

    sudo update-alternatives --config python3
    

    You will see a list of installed Python versions. Choose the one you want to use.

Method 3: Building Python from source

If you are like me and enjoy the flexibility of customizing your software, building Python from source is a great option.

  1. Install dependencies:
    sudo apt update
    sudo apt install -y build-essential checkinstall
    sudo apt install -y libreadline-gplv2-dev libncursesw5-dev libssl-dev \
    libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev
    
  2. Download the source code:Go to the official Python website and download the desired version. For example, for Python 3.10:
    wget https://www.python.org/ftp/python/3.10.0/Python-3.10.0.tgz
    tar xzf Python-3.10.0.tgz
    
  3. Compile and install:
    cd Python-3.10.0
    ./configure --enable-optimizations
    make -j 4  # Use -j followed by the number of processor cores
    sudo make altinstall
    

    Note: Use altinstall to avoid replacing the default Python binary.

Updating Python on Fedora

Fedora is another favorite of mine for its cutting-edge features.

  1. Install Python using dnf:
    sudo dnf install python3.10
    
  2. Verify the installation:
    python3.10 --version
    

    You should see:

    Python 3.10.0
    
  3. Switching between Python versions: Fedora manages alternatives slightly differently. To switch between versions, you might use update-alternatives:
    sudo alternatives --set python3 /usr/bin/python3.10
    

Updating Python on CentOS

CentOS often requires manual updates because it focuses on stability, which means slower updates.

  1. Enable EPEL and IUS repositories:
    sudo yum install -y epel-release
    sudo yum install -y https://repo.ius.io/ius-release-el7.rpm
    
  2. Install Python:
    sudo yum install -y python3.10
    
  3. Check the installed version:
    python3.10 --version
    
  4. Switch between Python versions:Update alternatives as follows:
    sudo alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
    sudo alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2
    

    Switch versions with:

    sudo alternatives --config python3

Conclusion

Updating Python on a Linux system can be straightforward or a little complex, depending on your needs and the distribution you are using. Ubuntu offers the simplicity of PPAs, Fedora brings cutting-edge features, and CentOS provides stability. Feel free to choose to install from a repository or build from source. The key is understanding the process and selecting the method that best fits your workflow. With the steps provided, you should be well-equipped to handle any Python update, ensuring your environment remains secure and efficient.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.