Install Python from source

Sometimes you need a Python version not available by your distribution. This guide shows how to build a complete Python environment from source.

Distribution: Ubuntu 20.04

Install dependencies

Install the required packages to build Python (requires root):

apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget

Build Python

The rest of the build process can be done as a regular user.

tar -xzf Python-3.7.9.tgz
cd Python-3.7.9
./configure --prefix=/path/to/install --enable-optimizations
make install

Set up a venv and use it

cd /path/to/install
bin/python3 --version
bin/python3 -m pip --version
bin/python3 -m venv env
env/bin/pip install <module>
>>> import <module>

Alternatively install Python using pyenv

Install dependencies:

apt install --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

Install Python using pyenv:

git clone pyenv
cd pyenv
src/configure && make -C src
export PYENV_ROOT="/path/to/pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
pyenv install 3.7.9
versions/3.7.9/bin/python3 --version