A list of available Python versions can be found on python.org.

export PYTHON_VERSION=3.11.5
export PYTHON_MAJOR=3

prepare to build

curl -O https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz
tar -xvzf Python-${PYTHON_VERSION}.tgz
cd Python-${PYTHON_VERSION}

build

./configure --enable-optimizations --with-lto=full --prefix=/opt/python/${PYTHON_VERSION} --libdir=/opt/python/${PYTHON_VERSION}/lib
make
sudo make install

make changes to /etc/profile.d/python.sh

# add python startup script for interactive sessions
export PYTHONSTARTUP=/etc/pythonstart
PATH=/opt/python/3.11.5/bin/:$PATH

set alternatives

update-alternatives --install /usr/bin/python3 python3 /opt/python/3.11.5/bin/python3.11 1
update-alternatives --config python3