How to add Python 3.11 to Raspberry Pi OS 32 Lite

Install required packages

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev
    
# with all optional modules : 
sudo apt-get install build-essential gdb lcov pkg-config \
      libbz2-dev libffi-dev libgdbm-dev libgdbm-compat-dev liblzma-dev \
      libncurses5-dev libreadline6-dev libsqlite3-dev libssl-dev \
      lzma lzma-dev tk-dev uuid-dev zlib1g-dev

Download Python 3.11 sources :

wget https://www.python.org/ftp/python/3.11.6/Python-3.11.6.tgz
tar -xzf Python-3.11.6.tgz

Compile :

cd Python-3.11.6/
./configure --enable-optimizations

sudo make altinstall
# takes a very long time
sudo rm /usr/bin/python3
sudo ln -s /usr/local/bin/python3.11 /usr/bin/python3
sudo ln -s /usr/local/bin/pip3.11 /usr/bin/pip3
sudo ln -s /usr/bin/pip3 /usr/bin/pip

Tests :

$ which python
/usr/bin/python

$ python --version
Python 3.11.6

$ python3 --version
Python 3.11.6

$ pip --version
pip 23.2.1 from /usr/local/lib/python3.11/site-packages/pip (python 3.11)

$ pip3 --version
pip 23.2.1 from /usr/local/lib/python3.11/site-packages/pip (python 3.11)

Under the hood :

$ ls -l /usr/local/bin/
total 23388
-rwxr-xr-x 1 root root      102 Oct  7 17:45 2to3-3.11
-rwxr-xr-x 1 root root      100 Oct  7 17:45 idle3.11
-rwxr-xr-x 1 root root      230 Oct  7 17:46 pip3.11
-rwxr-xr-x 1 root root       85 Oct  7 17:45 pydoc3.11
-rwxr-xr-x 1 root root 23928348 Oct  7 17:10 python3.11
-rwxr-xr-x 1 root root     3053 Oct  7 17:45 python3.11-config


$ ls -ld /usr/bin/py*
-rwxr-xr-x 1 root root    7806 Apr  5  2021 /usr/bin/py3clean
-rwxr-xr-x 1 root root   13184 Apr  5  2021 /usr/bin/py3compile
lrwxrwxrwx 1 root root      31 Apr  5  2021 /usr/bin/py3versions -> ../share/python3/py3versions.py
lrwxrwxrwx 1 root root       8 Apr  5  2021 /usr/bin/pydoc3 -> pydoc3.9
-rwxr-xr-x 1 root root      78 Mar 12  2021 /usr/bin/pydoc3.9
lrwxrwxrwx 1 root root      12 Apr  5  2021 /usr/bin/pygettext3 -> pygettext3.9
-rwxr-xr-x 1 root root   21537 Feb 19  2021 /usr/bin/pygettext3.9
lrwxrwxrwx 1 root root       7 Mar  2  2021 /usr/bin/python -> python3
lrwxrwxrwx 1 root root       9 Apr  5  2021 /usr/bin/python3 -> python3.9
-rwxr-xr-x 1 root root 4703672 Mar 12  2021 /usr/bin/python3.9

Resources :