Guest User

install pyton 3.12

a guest
Sep 6th, 2026
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.77 KB | Software | 0 0
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3.  
  4. # ============================================================
  5. # Python 3.12.13 installer for Debian 13
  6. #
  7. # Everything except the final system installation stays inside
  8. # the directory where this script is executed.
  9. #
  10. # System-wide installation:
  11. #   /usr/local/bin/python3.12
  12. #
  13. # Debian's system Python (/usr/bin/python3) is NOT modified.
  14. # ============================================================
  15.  
  16. PYTHON_VERSION="3.12.13"
  17. PREFIX="/usr/local"
  18.  
  19. SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
  20. WORK_DIR="${SCRIPT_DIR}/python-${PYTHON_VERSION}-build"
  21.  
  22. TARBALL="Python-${PYTHON_VERSION}.tar.xz"
  23. SOURCE_DIR="${WORK_DIR}/Python-${PYTHON_VERSION}"
  24.  
  25. URL="https://www.python.org/ftp/python/${PYTHON_VERSION}/${TARBALL}"
  26.  
  27. echo
  28. echo "============================================================"
  29. echo " Python ${PYTHON_VERSION} installer"
  30. echo "============================================================"
  31. echo
  32. echo "Working directory:"
  33. echo "  ${WORK_DIR}"
  34. echo
  35. echo "System installation:"
  36. echo "  ${PREFIX}/bin/python3.12"
  37. echo
  38. echo "Debian system Python will NOT be modified."
  39. echo
  40.  
  41. # ------------------------------------------------------------
  42. # Check OS
  43. # ------------------------------------------------------------
  44.  
  45. if [[ ! -f /etc/debian_version ]]; then
  46.    echo "ERROR: This script is intended for Debian."
  47.    exit 1
  48. fi
  49.  
  50. # ------------------------------------------------------------
  51. # Check sudo
  52. # ------------------------------------------------------------
  53.  
  54. if ! command -v sudo >/dev/null 2>&1; then
  55.    echo "ERROR: sudo is required."
  56.    exit 1
  57. fi
  58.  
  59. # ------------------------------------------------------------
  60. # Check architecture
  61. # ------------------------------------------------------------
  62.  
  63. ARCH="$(uname -m)"
  64.  
  65. if [[ "$ARCH" != "x86_64" ]]; then
  66.    echo "ERROR: This script expects x86_64."
  67.    echo "Detected: ${ARCH}"
  68.    exit 1
  69. fi
  70.  
  71. # ------------------------------------------------------------
  72. # Check if exact version is already installed
  73. # ------------------------------------------------------------
  74.  
  75. if [[ -x "${PREFIX}/bin/python3.12" ]]; then
  76.    INSTALLED_VERSION="$("${PREFIX}/bin/python3.12" --version 2>&1 || true)"
  77.  
  78.    if [[ "$INSTALLED_VERSION" == "Python ${PYTHON_VERSION}" ]]; then
  79.        echo "Python ${PYTHON_VERSION} is already installed:"
  80.        echo "  ${PREFIX}/bin/python3.12"
  81.        exit 0
  82.    fi
  83. fi
  84.  
  85. # ------------------------------------------------------------
  86. # Create local working directory
  87. # ------------------------------------------------------------
  88.  
  89. mkdir -p "$WORK_DIR"
  90.  
  91. # ------------------------------------------------------------
  92. # Install build dependencies
  93. #
  94. # These are system-wide package changes and require sudo.
  95. # ------------------------------------------------------------
  96.  
  97. echo "[1/7] Installing build dependencies..."
  98.  
  99. sudo apt-get update
  100.  
  101. sudo apt-get install -y \
  102.    build-essential \
  103.    wget \
  104.    ca-certificates \
  105.    libssl-dev \
  106.    zlib1g-dev \
  107.    libbz2-dev \
  108.    libreadline-dev \
  109.    libsqlite3-dev \
  110.    libncursesw5-dev \
  111.    xz-utils \
  112.    tk-dev \
  113.    libxml2-dev \
  114.    libxmlsec1-dev \
  115.    libffi-dev \
  116.    liblzma-dev
  117.  
  118. # ------------------------------------------------------------
  119. # Download Python source
  120. # ------------------------------------------------------------
  121.  
  122. echo
  123. echo "[2/7] Downloading Python ${PYTHON_VERSION}..."
  124.  
  125. cd "$WORK_DIR"
  126.  
  127. if [[ ! -f "$TARBALL" ]]; then
  128.    wget --https-only -O "$TARBALL" "$URL"
  129. else
  130.    echo "Source archive already exists:"
  131.    echo "  ${WORK_DIR}/${TARBALL}"
  132. fi
  133.  
  134. # ------------------------------------------------------------
  135. # Extract source
  136. # ------------------------------------------------------------
  137.  
  138. echo
  139. echo "[3/7] Extracting source..."
  140.  
  141. if [[ ! -d "$SOURCE_DIR" ]]; then
  142.    tar -xf "$TARBALL"
  143. fi
  144.  
  145. cd "$SOURCE_DIR"
  146.  
  147. # ------------------------------------------------------------
  148. # Configure
  149. # ------------------------------------------------------------
  150.  
  151. echo
  152. echo "[4/7] Configuring Python ${PYTHON_VERSION}..."
  153.  
  154. ./configure \
  155.    --prefix="$PREFIX" \
  156.    --with-ensurepip=install
  157.  
  158. # ------------------------------------------------------------
  159. # Compile
  160. # ------------------------------------------------------------
  161.  
  162. echo
  163. echo "[5/7] Compiling Python..."
  164.  
  165. make -j"$(nproc)"
  166.  
  167. # ------------------------------------------------------------
  168. # Install system-wide
  169. #
  170. # altinstall deliberately avoids replacing python3.
  171. # ------------------------------------------------------------
  172.  
  173. echo
  174. echo "[6/7] Installing Python system-wide..."
  175.  
  176. sudo make altinstall
  177.  
  178. # ------------------------------------------------------------
  179. # Verify
  180. # ------------------------------------------------------------
  181.  
  182. echo
  183. echo "[7/7] Verifying installation..."
  184.  
  185. PYTHON_BIN="${PREFIX}/bin/python3.12"
  186.  
  187. if [[ ! -x "$PYTHON_BIN" ]]; then
  188.    echo
  189.    echo "ERROR: Installation completed but ${PYTHON_BIN} was not found."
  190.    exit 1
  191. fi
  192.  
  193. INSTALLED_VERSION="$("$PYTHON_BIN" --version 2>&1)"
  194.  
  195. if [[ "$INSTALLED_VERSION" != "Python ${PYTHON_VERSION}" ]]; then
  196.    echo
  197.    echo "ERROR: Wrong Python version installed."
  198.    echo "Expected: Python ${PYTHON_VERSION}"
  199.    echo "Found:    ${INSTALLED_VERSION}"
  200.    exit 1
  201. fi
  202.  
  203. echo
  204. echo "============================================================"
  205. echo " SUCCESS"
  206. echo "============================================================"
  207. echo
  208. echo "Python:"
  209. echo "  ${INSTALLED_VERSION}"
  210. echo
  211. echo "Executable:"
  212. echo "  ${PYTHON_BIN}"
  213. echo
  214. echo "System-wide command:"
  215. echo "  python3.12"
  216. echo
  217. echo "Debian system Python:"
  218. python3 --version
  219. echo
  220. echo "Build files remain in:"
  221. echo "  ${WORK_DIR}"
  222. echo
  223. echo "============================================================"
Advertisement
Add Comment
Please, Sign In to add comment