How to Compile CPython on Debian-Based Linux

Python
CPython
C
compilation
make
Debian
Linux
Author

Galen Seilis

Published

July 25, 2024

This is a short blog post to remind myself how to compile CPython from its source.

You need to get the source files for Python as you need to give the C compiler (and other tools) the needed instructions for producing machine code. The CPython source is available on Github. Using git, you can download with

$ git clone https://github.com/python/cpython.git

You should install build-essential which provides tooling for building Debian packages. This can be done with apt:

$ sudo apt install build-essential

Next install these assorted packages:

$ sudo apt install libssl-dev zlib1g-dev libncurses5-dev libncursesw5-dev libreadline-dev libsqlite3-dev libgdbm-dev libdb5.3-dev libbz2-dev liblzma-dev libffi-dev

Here is a brief description of each package:

Install Package Description
libssl-dev Secure Sockets Layer toolkit - development files This package is part of the OpenSSL project’s implementation of the SSL and TLS crypographic protocols for secure communication over the internet.
zlib1g-dev Compression library - development zlib is a library implementing the deflate compression method found in gzip and PKZIP.
libncurses5-dev Transitional pacakge for libncurses-dev Package prociding libncurses5-dev.
libncursesw5-dev Transitional package for libncurses-dev Package providing libncursesw5-dev.
libreadline-dev GNU readline and history libraries, development files The GNU readline library aids in the consistency of user interface across discrete programs that need to provide a command line interace
libsqlite3-dev libsqlite3-dev SQLite is a C library that implements an SQL database engine.
libgdbm-dev GNU dbm database routines (development files) GNU dbm (‘gdbm’) is a library of database functions that use extensible hashing and works similarly to the standard UNIX ‘dbm’ functions.
libdb5.3-dev Berkeley v5.3 Database Libraries [development] This is the development package which contains headers and static libraries for the Berkely v5.3 database library
libbz2-dev High-quality block-sorting file compressor library - development Static libraries and include files for the bzip2 compressor library
liblzma-dev XZ-format compression library - development files XZ is the successor to the Lempel-Ziv/Markov-chain Algorithm compression format, which provides memory-hungry but powerful compressoin (often better than bzip2) and fast, easy decompression.
libffi-dev Foreign Fucntion Interface library (development files) This package contains the headers and static library files necessary for building programs which use libffi

Now run the configuration tool that the Python dev’s have kindly prepared. It will prepare a makefile for everything you need to build CPython.

$ ./configure --with-pydebug

The --with-pydebug tells configure to use a debugging hook.

Finally, you can just run make.

$ make

That’s pretty much it. If you want to silence the large standard output, you can add an -s. By default make will compile the first target specified in the make file, which for this project is actually the entirety of CPython itself. You can specify special build targets related to building, testing, installation, and other topics.