Important

This is part of a Draft of the Python Contributor’s Guide. Text in square brackets are notes about content to fill in. Currently, the devguide and this new Contributor’s Guide co-exist in the repo. We are using Sphinx include directives to demonstrate the re-organization. The final Contributor’s Guide will replace the devguide with content in only one place. We welcome help with this!

The [Plan for the Contributor’s Guide] page has more details about the current state of this draft and how you can help. See more info about the Contributor Guide in the discussion forum: Refactoring the DevGuide.

Install Dependencies

[This is the Install dependencies section from the devguide.]

This section explains how to install libraries which are needed to compile some of CPython’s modules (for example, zlib).

For Unix-based systems, we try to use system libraries whenever available. This means optional components will only build if the relevant system headers are available. The best way to obtain the appropriate headers will vary by distribution, but the appropriate commands for some popular distributions are below.

On Fedora, RHEL, CentOS and other dnf-based systems:

$ sudo dnf install git pkg-config
$ sudo dnf install dnf-plugins-core  # install this to use 'dnf builddep'
$ sudo dnf builddep python3

Some optional development dependencies are not included in the above. To install some additional dependencies for optional build and test components:

$ sudo dnf install \
      gcc gcc-c++ gdb lzma glibc-devel libstdc++-devel openssl-devel \
      readline-devel zlib-devel libffi-devel bzip2-devel xz-devel \
      sqlite sqlite-devel sqlite-libs libuuid-devel gdbm-libs perf \
      expat expat-devel mpdecimal python3-pip

On Debian, Ubuntu, and other apt-based systems, try to get the dependencies for the Python you’re working on by using the apt command.

First, make sure you have enabled the source packages in the sources list. You can do this by adding the location of the source packages, including URL, distribution name and component name, to /etc/apt/sources.list. Take Ubuntu 22.04 LTS (Jammy Jellyfish) for example:

$ deb-src http://archive.ubuntu.com/ubuntu/ jammy main

Alternatively, uncomment lines with deb-src using an editor, for example:

$ sudo nano /etc/apt/sources.list

For other distributions, like Debian, change the URL and names to correspond with the specific distribution.

Then you should update the packages index:

$ sudo apt-get update

Now you can install the build dependencies via apt:

$ sudo apt-get build-dep python3
$ sudo apt-get install pkg-config

If you want to build all optional modules, install the following packages and their dependencies:

$ sudo apt-get install build-essential gdb lcov pkg-config \
      libbz2-dev libffi-dev libgdbm-dev libgdbm-compat-dev liblzma-dev \
      libncurses5-dev libreadline6-dev libsqlite3-dev libssl-dev \
      lzma lzma-dev tk-dev uuid-dev zlib1g-dev libmpdec-dev

Note that Debian 12 and Ubuntu 24.04 do not have the libmpdec-dev package. You can safely remove it from the install list above and the Python build will use a bundled version.

For macOS systems (versions 10.9+), the Developer Tools can be downloaded and installed automatically; you do not need to download the complete Xcode application.

If necessary, run the following:

$ xcode-select --install

This will also ensure that the system header files are installed into /usr/include.

Also note that macOS does not include several libraries used by the Python standard library, including libzma, so expect to see some extension module build failures unless you install local copies of them. As of OS X 10.11, Apple no longer provides header files for the deprecated system version of OpenSSL which means that you will not be able to build the _ssl extension. One solution is to install these libraries from a third-party package manager, like Homebrew or MacPorts, and then add the appropriate paths for the header and library files to your configure command.

For Homebrew, install dependencies using brew:

$ brew install pkg-config openssl@3 xz gdbm tcl-tk mpdecimal

For Python 3.13 and newer:

$ GDBM_CFLAGS="-I$(brew --prefix gdbm)/include" \
   GDBM_LIBS="-L$(brew --prefix gdbm)/lib -lgdbm" \
   ./configure --with-pydebug \
               --with-system-libmpdec \
               --with-openssl="$(brew --prefix openssl@3)"

For Python 3.11 and 3.12:

$ GDBM_CFLAGS="-I$(brew --prefix gdbm)/include" \
   GDBM_LIBS="-L$(brew --prefix gdbm)/lib -lgdbm" \
   ./configure --with-pydebug \
               --with-openssl="$(brew --prefix openssl@3)"

For Python 3.9 and 3.10:

$ CPPFLAGS="-I$(brew --prefix gdbm)/include -I$(brew --prefix xz)/include" \
   LDFLAGS="-L$(brew --prefix gdbm)/lib -L$(brew --prefix xz)/lib" \
   ./configure --with-pydebug \
               --with-openssl="$(brew --prefix openssl@3)" \
               --with-tcltk-libs="$(pkg-config --libs tcl tk)" \
               --with-tcltk-includes="$(pkg-config --cflags tcl tk)" \
               --with-dbmliborder=gdbm:ndbm

(--with-dbmliborder is a workaround for a Homebrew-specific change to gdbm; see #89452 for details.)

For MacPorts, install dependencies using port:

$ sudo port install pkgconfig openssl xz gdbm tcl tk +quartz mpdecimal

For Python 3.13 and newer:

$ GDBM_CFLAGS="-I$(dirname $(dirname $(which port)))/include" \
   GDBM_LIBS="-L$(dirname $(dirname $(which port)))/lib -lgdbm" \
   ./configure --with-pydebug \
               --with-system-libmpdec

For Python 3.11 and 3.12:

$ GDBM_CFLAGS="-I$(dirname $(dirname $(which port)))/include" \
   GDBM_LIBS="-L$(dirname $(dirname $(which port)))/lib -lgdbm" \
   ./configure --with-pydebug

And finally, run make:

$ make -s -j8

There will sometimes be optional modules added for a new release which won’t yet be identified in the OS-level build dependencies. In those cases, just ask for assistance in the Core Development category on Discourse.

Explaining how to build optional dependencies on a Unix-based system without root access is beyond the scope of this guide.

For more details on various options and considerations for building, refer to the macOS README.

Note

While you need a C compiler to build CPython, you don’t need any knowledge of the C language to contribute! Vast areas of CPython are written completely in Python: as of this writing, CPython contains slightly more Python code than C.

On Windows, extensions are already included and built automatically.

The BeeWare project maintains scripts for building Android dependencies, and distributes pre-compiled binaries for each of them. These binaries are automatically downloaded and used by the CPython build script at Android/android.py.

As with CPython itself, the dependencies for CPython must be compiled for each of the hardware architectures that iOS supports. Consult the documentation for XZ, bzip2, OpenSSL and libffi for details on how to configure the project for cross-platform iOS builds.

Alternatively, the BeeWare Project maintains a project for building iOS dependencies, and distributes pre-compiled binaries for each of the dependencies. If you use this project to build the dependencies yourself, the subfolders of the install folder can be used to configure CPython. If you use the pre-compiled binaries, you should unpack each tarball into a separate folder, and use that folder as the configuration target.