Compiling Python 3.12 from Source Tarball on Linux

Joe • October 2, 2023

learning python writing

Compiling Python 3.12 from Source Tarball on Linux

Preliminary Dev tools installation

Debian/Ubuntu

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

Red Hat or Fedora:

sudo dnf update
sudo dnf groupinstall "Development Tools" "Development Libraries"

Download "Gzipped source tarball" the from Python.org, and it extract it locally.

wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz
tar -xvf Python-3.12.0.tgz
cd Python-3.12.0/
./configure --enable-optimizations

Check how many CPUs you have to enable faster compilation:

root@github-cicd:~# nproc
8
root@github-cicd:~#

In this example we have 8 CPUs on the github-cicd server. We want to use all our cores to compile so it runs faster. If you run into issues you can try using 1 or 2 fewer CPUS than reported by nproc)

Compile python via make

make -j 6 #use 6 CPUs

This may take several minutes and you should see output similar to the following:

Checked 111 modules (31 built-in, 74 shared, 1 n/a on linux-x86_64, 1 disabled, 4 missing, 0 failed on import)
make[2]: Leaving directory '/root/Python-3.12.0'
touch profile-gen-stamp
make[1]: Leaving directory '/root/Python-3.12.0'
# Next, run the profile task to generate the profile information.
./python -m test --pgo --timeout=1200 || true
0:00:00 load avg: 7.20 Run tests sequentially (timeout: 20 min)
0:00:00 load avg: 7.20 [ 1/44] test_array
0:00:03 load avg: 7.20 [ 2/44] test_base64
0:00:04 load avg: 7.18 [ 3/44] test_binascii
0:00:04 load avg: 7.18 [ 4/44] test_binop
0:00:04 load avg: 7.18 [ 5/44] test_bisect
0:00:05 load avg: 7.18 [ 6/44] test_bytes
0:00:23 load avg: 6.47 [ 7/44] test_bz2
0:00:24 load avg: 6.35 [ 8/44] test_cmath
0:00:25 load avg: 6.35 [ 9/44] test_codecs
0:00:30 load avg: 6.08 [10/44] test_collections
0:00:33 load avg: 6.08 [11/44] test_complex
0:00:34 load avg: 5.75 [12/44] test_dataclasses
0:00:36 load avg: 5.75 [13/44] test_datetime
0:00:47 load avg: 5.10 [14/44] test_decimal
0:01:04 load avg: 4.54 [15/44] test_difflib
0:01:08 load avg: 4.26 [16/44] test_embed
0:01:30 load avg: 3.14 [17/44] test_float
0:01:31 load avg: 3.14 [18/44] test_fstring
0:01:34 load avg: 2.97 [19/44] test_functools
0:01:36 load avg: 2.97 [20/44] test_generators
0:01:37 load avg: 2.97 [21/44] test_hashlib
0:01:39 load avg: 2.81 [22/44] test_heapq
0:01:42 load avg: 2.81 [23/44] test_int
0:01:44 load avg: 2.75 [24/44] test_itertools
0:02:01 load avg: 3.25 [25/44] test_json
0:02:09 load avg: 3.44 [26/44] test_long
0:02:20 load avg: 3.53 [27/44] test_lzma
0:02:21 load avg: 3.53 [28/44] test_math
0:02:33 load avg: 3.82 [29/44] test_memoryview
0:02:35 load avg: 3.84 [30/44] test_operator
0:02:36 load avg: 3.84 [31/44] test_ordered_dict
0:02:41 load avg: 3.69 [32/44] test_patma
0:02:42 load avg: 3.69 [33/44] test_pickle
0:03:05 load avg: 2.85 [34/44] test_pprint
0:03:06 load avg: 2.85 [35/44] test_re
0:03:10 load avg: 2.70 [36/44] test_set

Once the make command completes, install Python 3.12 on your system:

sudo make altinstall

The make altinstall command will finish and we can then verify our location and version:

...
Looking in links: /tmp/tmppikf_vfp
Processing /tmp/tmppikf_vfp/pip-23.2.1-py3-none-any.whl
Installing collected packages: pip
Successfully installed pip-23.2.1
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
root@hub:~/Python-3.12.0# which python 3.12
/usr/bin/python
root@hub:~/Python-3.12.0# which python3.12
/usr/local/bin/python3.12
root@hub:~/Python-3.12.0# python3.12 --version
Python 3.12.0
root@hub:~/Python-3.12.0#