Build python 3.11 on raspberry pi from source

The recent version of Python is 3.11 and was released in December 2022. Typical the raspbian distribution ships with lot older python releases for example buster comes with python 3.7.
Of course there are many cases where you want to use the most recent version of python. For example comes with a significant speed optimization.
This post gives you a very compressed script to be able to build python on you own.

First we should start with a updating all packages

sudo apt update
sudo apt upgrade

Now we need to install all dependencies required to compile python from source.

sudo apt install -y libffi-dev libbz2-dev liblzma-dev \
    libsqlite3-dev libncurses5-dev libgdbm-dev zlib1g-dev \
    libreadline-dev libssl-dev tk-dev build-essential \
    libncursesw5-dev libc6-dev openssl git

Download the current version from python.org. In this example I’m downloading 3.11.1.

wget https://www.python.org/ftp/python/3.11.1/Python-3.11.1.tar.gz
tar zxf Python-3.11.1.tgz
cd Python-3.11.1

Build the makefile (will take several minutes)

./configure --enable-optimizations

We are ready to make python (compile, link, test etc.). The below shown arguments of make should help to compile as fast as possible. But it still will take between .5 to 1 hour. Also, like in my case, it could be possible that rpi hangs to dead. Restart and repeat.

make -j 4 -l 4

make will inform you about which python modules where not compile able. Check if this is relevant for your case. In most cases this means that some dependencies (libraries) are missing.

Test the build shortly.

cd python
python -V
>>> Python 3.11.1
cd ..

With that we are ready to install python regular onto the rpi

make install

Python 3.11.1 should be now you default Python 3 release. You may clean up remove the temporary folder and remove also above dependencies if you like.

python3
>>> import sys
>>> sys.version
# 3.11.1 ...

Leave a Comment

Your email address will not be published. Required fields are marked *