How to Update and Upgrade Python3 Version on Ubuntu 22.04|20.04

Posted April 3, 2024
Update and Upgrade Python3 Version on Ubuntu 22.04|20.04

Python updates its versions to add more advanced features. Ubuntu comes installed with a Python3 version (not all). Now, if you have an existing Python3 version on Ubuntu, you need to update and update to a newer version that lets you use the version’s new feature.

This guide teaches how to correctly update and Upgrade the Python3 Version on Ubuntu 22.04|20.04. To update or upgrade Python on Ubuntu 22.04|20.04, you’ll have different approaches. Here is what this guide will cover:

  • How to update and update Python3 on Ubuntu by downloading Python binaries from the source.
  • What you need to Update Python to a specific version (let’s say Python 3.12.0) within Ubuntu.
  • An alternative to update and use the latest Python versions using Pyenv.
  • How to completely uninstall and remove an updated/upgraded Python version on Ubuntu.
  • The possibilities of using PPA deadsnakes to Update and Upgrade Python3 Version on Ubuntu 22.04|20.04.
  • Ensuring the updated Python version has pip running on your Ubuntu machine.

Ready? Now, dive in and Update|Upgrade Python3 Version on Ubuntu 22.04|20.04 like a Pro.

Ubuntu Python Update/Upgrade Requisites

To get Python correctly updated on your Ubuntu machine ensure:

  • You have internet connections.
  • Access to a terminal with sudo privileges.
  • An older version of Python installed on Ubuntu

Related How to Install Python 3.11 on Ubuntu 22.04|20.04

Checking Python Version to Upgrade

Now, you must have an existing Python3 version that you want to attempt and upgrade. In this case, Ubuntu 22.04|20.04 comes with Python3 already installed. On a quick look, your Ubuntu machine will have an older version as follows:

python3 --version

Update and Upgrade Python3 Version on Ubuntu

In this case, I’m running Ubuntu 3.10. I need to update it to a later version and let Ubuntu know that.

How to Select the Python Version to Update/Upgrade for Ubuntu

Python has its versions readily available and you can download the version of choice that fits your Ubuntu system. If you check the Python download page, you will get the latest Python version available as such. At the time of writing this post. Python has 3.12.2 as the latest version:

Update and Upgrade Python3 Version on Ubuntu

Based on this, the best way to update and upgrade Python 3.10 to 3.12.2 is to download this latest version and install it on Ubuntu 22.04|20.04.

However, to do so, you need access to the Python source and select the stable version. Go to Ubuntu Python source and you should have a list of Python Stable Source Releases:

Update and Upgrade Python3 Version on Ubuntu

These are all the versions you can upgrade to within your Ubuntu 22.04|20.0. This example is using terminal Ubuntu terminal to get these versions. This means you will use the Ubuntu wget command and get your selected version ready on your Ubuntu machine. Let’s dive in and do so.

Updating Python Version from the Source on Ubuntu

Before attempting to get the latest Python version, ensure your Ubuntu has the latest package index. Run the following commands:

# Check packages to Update
sudo apt update
# Let Ubuntu upgrade the package index
sudo apt upgrade

The following necessary dependencies must be installed before updating Python3:

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

Next, you will use wget to install the updated Python version to Ubuntu as follows:

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

Here, 3.12.2 reflects the version you want to download:

Update and Upgrade Python3 Version on Ubuntu

This file will then need to be extracted. In your directory, use ls to make sure the file and ready and run the following command to unzip it:

sudo tar -xvf Python-3.12.2.tgz

A ls command shows you have a directory Python-3.12.2. Change path to this directory:

cd Python-3.12.2

Update and Upgrade Python3 Version on Ubuntu

Installing Updated/Upgraded Python3 Version on Ubuntu

Ubuntu will check this Python-3.12.2 to add the latest Python binaries. If you ls, you should the Python 3.12.2 files as such:

Update and Upgrade Python3 Version on Ubuntu

You specifically need to execute the install-sh file and let Ubuntu handle the rest. But first, you must provide a script to Compile the source code. You will use the following command:

sudo ./configure --enable-optimizations

Ubuntu will check if Python version 3.12.2 is installed and show no if not. This will happen until you get the current version running on your machine.

Update and Upgrade Python3 Version on Ubuntu

Give the command a few minutes to finish:

Now, Ubuntu will Compile the new version source code and create the Python Makefile to build Python version 3.12.2. ls the directory as such:

Update and Upgrade Python3 Version on Ubuntu

Upgrading Python Version with Ubuntu

To upgrade to this version, use the make command to run the Makefile:

sudo make install

Be patient once you run this command.

Ubuntu will now grab the Python version 3.12.2 into your Ubuntu system. Every Updated file for this version should be ready as such:

Update and Upgrade Python3 Version on Ubuntu

At the same time, you will note that Ubuntu will get the pip version compatible with the Updated Python version (Once the sudo make install is done):

Update and Upgrade Python3 Version on Ubuntu

As expected, check the available version and ensure Ubuntu has successfully Upgraded your version:

python3 --version

Update and Upgrade Python3 Version on Ubuntu

Likewise, check if the updated Python version is pip ready:

sudo pip3 --version

You should get a pip version that should work based on the updated version.

Update and Upgrade Python3 Version on Ubuntu

Related: How to APT Install Python pip2|pip3| on Ubuntu 22.04|20.04

That’s all the steps you need to update and upgrade Python to the latest version on Ubuntu 22.04|20.04.

Updating Python to a Specific Version Within Ubuntu 22.04|20.04

Based on the above steps, a Python version 3.12.2 is Specific. However, if you want to install a different version on your Ubuntu system, you need:

  • Identify the stable Python version you want to update to, for example, Python 3.11.0.
  • Next, add this version to the wget command and run it:
wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tgz
  • The above command will download Python 3.11.0. extract its files:
sudo tar -xvf Python-3.11.0.tgz
  • Access the Extracted Python-3.11.0 directory
cd Python-3.11.0
  • Create an Ubuntu Makefile file for Python version 3.11.0 within this path:
sudo ./configure --enable-optimizations
  • Finally, run the make command to update your Python version:
sudo make install
  • And remember to verify if the Python version is now upgraded:
root@:~# python3 --version
Python 3.11.0

How to Completely Uninstall Updated/Upgraded Python Version on Ubuntu

Let’s say you now want to uninstall this Python version from Ubuntu 22.04|20.04. Here are the steps you need to take.

The default python is installed on /usr/bin/python*. However, the version you just updated will be saved on the /usr/local/bin/python3 path. Confirm this by running the which python3 command:

ubuntu@:~$ which python3
/usr/local/bin/python3
  • This means you uninstall updated Python3 from this path. ls to make sure you are uninstalling the upgraded Python version
ls /usr/local/bin/python*

This is where I have my updated Python 3.12.2 version:

Update and Upgrade Python3 Version on Ubuntu

Now, you need to remove the above two files.

  • Proceed and uninstall your Python based on the Updated version you have:
sudo rm /usr/local/bin/python3.12
sudo rm /usr/local/bin/python3.12-config
sudo rm /usr/local/bin/python3
sudo rm /usr/local/bin/python3-config 
  • At the same time, you must remove pip that was associated with your added version. In this case, check pip as follows:
ls /usr/local/bin/pip*

Update and Upgrade Python3 Version on Ubuntu

  • Remove these files as well:
sudo rm /usr/local/bin/pip3 
sudo rm /usr/local/bin/pip3.12

Rerun python3 --version command:

python3 --version

You should get the Python version which was installed on your Ubuntu by default you upgraded:

Update and Upgrade Python3 Version on Ubuntu

To use the default version with pip, you will have to install pip afresh:

sudo apt install python3-pip -y

Finally, confirm it is ready:

sudo pip3 --version

Update and Upgrade Python3 Version on Ubuntu

Exploring Python Update/Upgrade with Ubuntu Python Pyenv Library

What if you don’t just want to update the Python3 version? How about having the capacity to run and manage multiple Python3 versions on the same Ubuntu system? Well, this is easily possible.

At times, you need to probably use different Python3 versions to manage different applications. In this case, instead of Upgrading to a Specific version, you need to manage two versions.

Pyenv is the Library you need here. It lets you:

  • Have more than one Python version on Ubuntu.
  • Switch between Python Versions side by side.

If this is what you need, check the following steps:

  • Add Pyenv necessary dependencies:
sudo apt install -y build-essential git libssl-dev zlib1g-dev libbz2-dev \
    libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev \
    libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python3-openssl
  • Go ahead and install Pyenv using this command:
curl https://pyenv.run | bash
  • Based on the above command, you will need to add the Pyenv paths. Copy the path commands and run them:

Update and Upgrade Python3 Version on Ubuntu

  • Reload your shell to make these changes ready:
source ~/.bashrc
  • Now, you need to select your Python version and install it as such:
pyenv install 3.12.2
  • Try to add another stable version of your choice:
pyenv install 3.11.0

Update and Upgrade Python3 Version on Ubuntu

  • Check all the Python Versions that are ready on your Ubuntu system:
pyenv versions

You should have the Python3 versions that you can use on your Ubuntu:

Update and Upgrade Python3 Version on Ubuntu

  • All you need now is to switch to the version of Python you want to use. For example:
pyenv global 3.11.0

The setting will apply globally and you should have Python 3.11.0 as your current working version. Confirm so using the following command:

python3 --version

Update and Upgrade Python3 Version on Ubuntu

Conclusion

You have successfully Updated and Upgraded the Python3 Version on Ubuntu 22.04|20.04. you learned:

  • How to update and update Python3 on Ubuntu by downloading Python binaries from the source.
  • How to update and upgrade Python to a specific version within Ubuntu.
  • Using Pyenv to update and use the latest Python versions.
  • How to uninstall an updated/upgraded Python version on Ubuntu.
How to Update and Upgrade Python3 Version on Ubuntu 22.04|20.04

Written By:

Joseph Chege