Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- set -euo pipefail
- # ============================================================
- # Python 3.12.13 installer for Debian 13
- #
- # Everything except the final system installation stays inside
- # the directory where this script is executed.
- #
- # System-wide installation:
- # /usr/local/bin/python3.12
- #
- # Debian's system Python (/usr/bin/python3) is NOT modified.
- # ============================================================
- PYTHON_VERSION="3.12.13"
- PREFIX="/usr/local"
- SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
- WORK_DIR="${SCRIPT_DIR}/python-${PYTHON_VERSION}-build"
- TARBALL="Python-${PYTHON_VERSION}.tar.xz"
- SOURCE_DIR="${WORK_DIR}/Python-${PYTHON_VERSION}"
- URL="https://www.python.org/ftp/python/${PYTHON_VERSION}/${TARBALL}"
- echo
- echo "============================================================"
- echo " Python ${PYTHON_VERSION} installer"
- echo "============================================================"
- echo
- echo "Working directory:"
- echo " ${WORK_DIR}"
- echo
- echo "System installation:"
- echo " ${PREFIX}/bin/python3.12"
- echo
- echo "Debian system Python will NOT be modified."
- echo
- # ------------------------------------------------------------
- # Check OS
- # ------------------------------------------------------------
- if [[ ! -f /etc/debian_version ]]; then
- echo "ERROR: This script is intended for Debian."
- exit 1
- fi
- # ------------------------------------------------------------
- # Check sudo
- # ------------------------------------------------------------
- if ! command -v sudo >/dev/null 2>&1; then
- echo "ERROR: sudo is required."
- exit 1
- fi
- # ------------------------------------------------------------
- # Check architecture
- # ------------------------------------------------------------
- ARCH="$(uname -m)"
- if [[ "$ARCH" != "x86_64" ]]; then
- echo "ERROR: This script expects x86_64."
- echo "Detected: ${ARCH}"
- exit 1
- fi
- # ------------------------------------------------------------
- # Check if exact version is already installed
- # ------------------------------------------------------------
- if [[ -x "${PREFIX}/bin/python3.12" ]]; then
- INSTALLED_VERSION="$("${PREFIX}/bin/python3.12" --version 2>&1 || true)"
- if [[ "$INSTALLED_VERSION" == "Python ${PYTHON_VERSION}" ]]; then
- echo "Python ${PYTHON_VERSION} is already installed:"
- echo " ${PREFIX}/bin/python3.12"
- exit 0
- fi
- fi
- # ------------------------------------------------------------
- # Create local working directory
- # ------------------------------------------------------------
- mkdir -p "$WORK_DIR"
- # ------------------------------------------------------------
- # Install build dependencies
- #
- # These are system-wide package changes and require sudo.
- # ------------------------------------------------------------
- echo "[1/7] Installing build dependencies..."
- sudo apt-get update
- sudo apt-get install -y \
- build-essential \
- wget \
- ca-certificates \
- libssl-dev \
- zlib1g-dev \
- libbz2-dev \
- libreadline-dev \
- libsqlite3-dev \
- libncursesw5-dev \
- xz-utils \
- tk-dev \
- libxml2-dev \
- libxmlsec1-dev \
- libffi-dev \
- liblzma-dev
- # ------------------------------------------------------------
- # Download Python source
- # ------------------------------------------------------------
- echo
- echo "[2/7] Downloading Python ${PYTHON_VERSION}..."
- cd "$WORK_DIR"
- if [[ ! -f "$TARBALL" ]]; then
- wget --https-only -O "$TARBALL" "$URL"
- else
- echo "Source archive already exists:"
- echo " ${WORK_DIR}/${TARBALL}"
- fi
- # ------------------------------------------------------------
- # Extract source
- # ------------------------------------------------------------
- echo
- echo "[3/7] Extracting source..."
- if [[ ! -d "$SOURCE_DIR" ]]; then
- tar -xf "$TARBALL"
- fi
- cd "$SOURCE_DIR"
- # ------------------------------------------------------------
- # Configure
- # ------------------------------------------------------------
- echo
- echo "[4/7] Configuring Python ${PYTHON_VERSION}..."
- ./configure \
- --prefix="$PREFIX" \
- --with-ensurepip=install
- # ------------------------------------------------------------
- # Compile
- # ------------------------------------------------------------
- echo
- echo "[5/7] Compiling Python..."
- make -j"$(nproc)"
- # ------------------------------------------------------------
- # Install system-wide
- #
- # altinstall deliberately avoids replacing python3.
- # ------------------------------------------------------------
- echo
- echo "[6/7] Installing Python system-wide..."
- sudo make altinstall
- # ------------------------------------------------------------
- # Verify
- # ------------------------------------------------------------
- echo
- echo "[7/7] Verifying installation..."
- PYTHON_BIN="${PREFIX}/bin/python3.12"
- if [[ ! -x "$PYTHON_BIN" ]]; then
- echo
- echo "ERROR: Installation completed but ${PYTHON_BIN} was not found."
- exit 1
- fi
- INSTALLED_VERSION="$("$PYTHON_BIN" --version 2>&1)"
- if [[ "$INSTALLED_VERSION" != "Python ${PYTHON_VERSION}" ]]; then
- echo
- echo "ERROR: Wrong Python version installed."
- echo "Expected: Python ${PYTHON_VERSION}"
- echo "Found: ${INSTALLED_VERSION}"
- exit 1
- fi
- echo
- echo "============================================================"
- echo " SUCCESS"
- echo "============================================================"
- echo
- echo "Python:"
- echo " ${INSTALLED_VERSION}"
- echo
- echo "Executable:"
- echo " ${PYTHON_BIN}"
- echo
- echo "System-wide command:"
- echo " python3.12"
- echo
- echo "Debian system Python:"
- python3 --version
- echo
- echo "Build files remain in:"
- echo " ${WORK_DIR}"
- echo
- echo "============================================================"
Advertisement
Add Comment
Please, Sign In to add comment