Skip to content

Instantly share code, notes, and snippets.

@yuri1969
Last active March 22, 2022 10:02
Show Gist options
  • Save yuri1969/a4f2a2f1b6fdd51f547022f4fd48b54e to your computer and use it in GitHub Desktop.
Save yuri1969/a4f2a2f1b6fdd51f547022f4fd48b54e to your computer and use it in GitHub Desktop.
Install a non-system Python on GNU Linux
#!/bin/bash
if [[ $# -lt 1 ]]; then
echo "Usage: ${0##*/} <version>"
echo "Example: ${0##*/} 3.6.15"
exit 1
fi
VERSION="$1"
CURR_DIR="$(dirname -- "${BASH_SOURCE[0]}")"
INSTALL_DIR="${CURR_DIR}/dist/${VERSION}"
TMP_DIR="${CURR_DIR}/tmp"
SRC_DIR="Python-${VERSION}"
ARCHIVE="${SRC_DIR}.tgz"
URL="https://www.python.org/ftp/python/${VERSION}/${ARCHIVE}"
mkdir -p "$INSTALL_DIR" || { echo "Can't create install dir ${INSTALL_DIR}"; exit 1; }
mkdir -p "$TMP_DIR" || { echo "Can't create install dir ${INSTALL_DIR}"; exit 1; }
wget -O "${TMP_DIR}/${ARCHIVE}" "$URL" || { echo "Can't download from ${URL}"; exit 1; }
tar -xvf "${TMP_DIR}/${ARCHIVE}" -C "${TMP_DIR}" || { echo "Can't unpack the downloaded file"; exit 1; }
cd "${TMP_DIR}/${SRC_DIR}" || { echo "Can't go into the Python dist dir"; exit 1; }
# --enable-optimizations enables PGO, we don't need this epic speed...
# defined prefixes & altinstal prevent owerwriting the system python
./configure "--prefix=${INSTALL_DIR}" "--exec_prefix=${INSTALL_DIR}/bin" && make && make altinstall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment