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.
- Enable the universe repository:
sudo add-apt-repository universe sudo apt update
- 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
- 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 usingapt-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:
- Add the deadsnakes PPA:
sudo add-apt-repository ppa:deadsnakes/ppa sudo apt update
- Install the desired Python version:Let’s say you want to install Python 3.10:
sudo apt install python3.10
- 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.
- 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
- 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
- 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.
- Install Python using dnf:
sudo dnf install python3.10
- Verify the installation:
python3.10 --version
You should see:
Python 3.10.0
- 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.
- Enable EPEL and IUS repositories:
sudo yum install -y epel-release sudo yum install -y https://repo.ius.io/ius-release-el7.rpm
- Install Python:
sudo yum install -y python3.10
- Check the installed version:
python3.10 --version
- 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.