To install python from source (on my current 21.04) ubuntu distro execute next commands:

Current version: 3.10.4

Note: when install fails on xxxx, install specific setuptools and restart the install (will probalbly only happen on upgrading):

python<current> -m pip install setuptools@git+
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev libncurses-dev libgdbm-dev libz-dev tk-dev libsqlite3-dev libreadline-dev liblzma-dev libffi-dev libgdbm-compat-dev


tar -xf Python-3.10.<x>.tgz

cd Python-3.10.<x>
./configure --enable-optimizations

make -j <nr of cores>
sudo make altinstall