Installing the Latest Python Version on Ubuntu 23.10, Mint 21.3

Mint, Ubuntu, and Python splash
✪ Ryan's Profile Image ✪ Ryan   |   March 27, 2024,  Last edited March 27, 2024

It's no secret that the python3 repository package is always a few versions behind the Python Software Foundation's current main release. At my time of writing, I am using Linux Mint 21.3 and running python3 --version reveals that I'm using v3.10.12 while Python's current main release is v3.12.2.

So how can you install the latest stable Python version on Ubuntu or Mint?

Credit

I followed along with Radwane Lourhmati's guide on Medium, and it was extremely helpul. I wrote this guide based directly on Lourhmati's. I often pull my information from others more knowledgable than myself and then tweak or generalize the information to better fit my needs.

Prerequisites

  • Ubuntu 23.10, Linux Mint 21.3, or a similar Linux distribution (some packages necessary for compilation and installation may vary).
  • Basic BASH terminal know-how.

Steps

1. Update and upgrade

sudo apt update && sudo apt upgrade -y

2. Install compilation/installation dependencies

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. Obtain an archive of the latest version

First, visit https://www.python.org/downloads/ and select the Linux/UNIX link.

Next, select the latest main release at the top.

On the resulting page, scroll down to the Files section and select "Gzipped source tarball" to begin downloading the compressed archive.

4. Extract and configure

In the terminal navigate to the folder containing the Python source archive (likely the Downloads folder) and extract the archive.

tar -xzvf Python-3.xx.x.tgz

Now cd into the archive folder.

cd Python-3.xx.x

Configure the build with optimization flags:

sudo ./configure --enable-optimizations

5. Build and install

Now it's time to build. I'm really glad I came across Lourhmati's guide (which this guide is based on) because I wouldn't have bothered to figure out how to leverage all my logical processors to speed up the build when invoking make. Run the following build command to leverage all of your logical processors:

sudo make -j$(nproc)

And now to install...

sudo make altinstall

6. Verify your installation and set an alias

Once the previous make job has exited, check your Python version to verify the installation:

python3.xx --version (where xx is the minor version number)

 

Assuming the version prints, e.g., Python 3.12.2, you're good to go. Before you're ready to use your shiny new Python version, set an alias in your .bashrc file, this way you can invoke the newer Python version when you invoke python3 in your terminal.

First, open .bashrc:

nano ~/.bashrc or xdg-open ~/.bashrc

At the end of the file, add:

alias python3='/usr/local/bin/python3.xx' (where xx is the minor version number)

 

Save the file and launch a new terminal session. This will allow you to test your alias. If it works as expected, running python3 --version should now list the version number you just installed, rather than the installed repository version. I recommend keeping the stable version of python3 in place for your current dependencies, rather than replacing it with the newer version in your local bin folder, unless you know what you're doing.

I hope that helped!