TroubleChute Logo
RASPBERRY PI - SOFTWARE

Update/Install specific Python on Raspberry Pi


Published: Jan 5, 2023
Last Edit: Jan 12, 2023
Raspberry Pi Python
616 Words, 3 Minutes.

Watch the video:


Why run different Python versions

Why install a 64-bit Raspberry Pi OS? Same reason. Everyone has their own wants and needs, and so do some bits of software. You may need to update Python to get compatible packages installed, or downgrade to get older projects working. This guide shows you how.

Updating your Pi

The first thing to do is make sure your Pi is up to date.

1
sudo apt-get update

and optionally

1
sudo apt-get upgrade

Installing required packages

A few other programs and packages are needed to make sure the installation goes smoothly.

1
sudo apt-get 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

Downloading Python

We need to download the Python source, build and then install it. To download Python, check the official python.org/downloads/ page to see what versions are available. We will need to remember a version number.

I have version 3.11.1 available, so I’ll download and install that.

1
2
cd ~/Downloads
wget https://www.python.org/ftp/python/VERSION/Python-VERSION.tgz`

Just replace VERSION with 3.11.1, or whatever version you’re looking to install. It appears twice (2 times) in the above command.

We are working in the Downloads folder just to keep your drive clean. You can delete the .tgz and source files later once the installation is complete.

Building Python

Extract the Python source files:

1
sudo tar zxf Python-VERSION.tgz

Once again replace VERSION with 3.11.1, or your required version.

Then navigate into the new folder and configure the source files for building:

1
2
cd Python-VERSION
sudo ./configure --enable-optimizations

Now comes the building section. This can take some time. We can halve, or even further improve build time by telling it to use more cores. We can see how many cores our Pi has by running lscpu. I have 4 cores in my Raspberry Pi 4.

Run

1
sudo make -j 4

4 is the number of cores. If yours freezes later, stop it using Ctrl+C and run it with just 2 cores.

If you’re fine with it building source files slow as molasses, just skip the -j part entirely, and run only sudo make.

Even though it may look frozen, it may just be taking a long time to process.

Optional: Monitor using HTOP

If you’re worried about it getting stuck, you can entertain yourself by monitoring the CPU usage stats for your pi. HTOP is a simple tool that we can use here.

Open a new terminal and run sudo apt install htop, then htop to bring up a nice GUI.

Installing Python

Finally, once the build process is complete (This can take half an hour or so), we can install Python.

Run

1
sudo make altinstall

Finally, once it’s complete, run python3.11 -V (or whatever version you’re using. Skip the minor number after the second dot if there is one).

You should now see your new Python installation running properly.

Just make sure to precede commands with python3.11, or your version, otherwise, you may be using a different python version that’s installed, such as the default one.

Optional (and only if you know what you’re doing)

If you know what you’re doing, we can tell Raspberry Pi OS what version of python to use.

This may mess with things, so instead, you should just use python3.11, or whatever version, as that will allow everything else to run happily as it is now.

1
2
#echo "alias python=/usr/local/bin/python3.11" >> ~/.bashrc
#source ~/.bashrc

Just remove the # before the command. This is so people don’t copy/paste without reading. Obviously, you will also need to replace 3.121 with your version, ignoring the minor version again.

Now, running just python -V should return your new, preferred Python version.

TCNO TechNobo / TroubleChute © Wesley Pyburn (TechNobo / TroubleChute)