/Raspberry Pi, Python

Install latest Python version on Raspberry Pi

Recently I wanted to install a certain package on my Raspberry Pi that required Python 3.6 or higher. To my surprise the latest available Python version in the official Raspbian repo was 3.5.3 To install a newer one, I had to do a few more things than a simple apt install python3. If you're in the same boat as me, read on.

Find already installed Python versions

Multiple versions of Python can be installed at the same time. Usually you'll have both 2.x and 3.x You can easily check this by running below commands

pi@raspberrypi:~ $ python -V
Python 2.7.13
pi@raspberrypi:~ $ python3 -V
Python 3.5.3

I want to keep Python 2.7.13 as is and only update Python 3.5.3 to the latest 3.x version.

Find and install latest available Python version

Go to official Python download page and look under "Looking for a specific release?" section.

python-releases

Click on the version you want, I chose 3.10.2. On the next page scroll down to the bottom and in "Files" section right click on "Gzipped source tarball" and choose "Copy link address" from the browser context menu.
Go back to the command line on your Raspberry Pi

  • Download your chosen version
wget https://www.python.org/ftp/python/3.10.2/Python-3.10.2.tgz
  • Extract source files
tar -zxvf Python-3.10.2.tgz
  • Go into just created directory
cd Python-3.10.2
  • Run the configuration command
./configure --enable-optimizations
  • Install any missing dependencies
sudo apt update
sudo apt install -y build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libffi-dev
  • Compile Python
sudo make altinstall

This can take a few minutes, so just be patient. If you've followed all of the above steps, you should have no problems.

Make the new version default

Now you should have three versions of Python installed

  • python : The default Python 2.7.13 version.
  • python3 : The default Python 3.5.3 version.
  • python3.10 : The one we installed from source.

Usually Python 2 is linked to python command and Python 3 is linked to python3 command. I'd recommend leaving it like that and only change the python3 link to the version we just installed.

  • Go to /usr/bin
cd /usr/bin
  • Remove the current link. This does not delete your older Python 3 version, it only unlinks it from that command.
sudo rm python3
  • Link the version you want to use from now on
sudo ln -s /usr/local/bin/python3.10 python3

Check if everything is as expected. It should now display the version you just installed (3.10.2 for me).

pi@raspberrypi:~ $ python -V
Python 2.7.13
pi@raspberrypi:~ $ python3 -V
Python 3.10.2