Install Python from Source

Saturday, Dec 23, 2023

Trying to update from Python3.9 to 3.11 using apt get or apt install proved to be a pain in the ass because of gpg keyring, apt add-key deprecated.

Turns out installing from source is much easier though it takes a considerably longer time to compile and install.

I followed the steps from [1] which I have copied here

sudo apt-get update
sudo apt-get install gdebi-core

sudo apt-get install \
    curl \
    gcc \
    libbz2-dev \
    libev-dev \
    libffi-dev \
    libgdbm-dev \
    liblzma-dev \
    libncurses-dev \
    libreadline-dev \
    libsqlite3-dev \
    libssl-dev \
    make \
    tk-dev \
    wget \
    zlib1g-dev

export PYTHON_VERSION=3.11.7
export PYTHON_MAJOR=3

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

./configure \
    --prefix=/opt/python/${PYTHON_VERSION} \
    --enable-shared \
    --enable-optimizations \
    --enable-ipv6 \
    LDFLAGS=-Wl,-rpath=/opt/python/${PYTHON_VERSION}/lib,--disable-new-dtags
make
sudo make install

Find out where the current python binary is at with

  which python3

Navigate to the output, delete the symbolic links to the old version. Create new symbolic links to the newly installed version. New version was installed in /opt/python/3.11.7

[1] : Install Python Source