Как собрать python из исходников
Перейти к содержимому

Как собрать python из исходников

  • автор:

How to Compile & Install Python 3.11/3.10 from Source in Ubuntu

This simple tutorial shows how to compile and install Python 3.11, Python 3.10, or other certain Python version in Ubuntu.

For Ubuntu 18.04, Ubuntu 20.04 and Ubuntu 22.04 LTS, there’s well trusted “deadsnakes” team PPA that maintains all the Python packages. For non-LTS releases, you may build the programming language package manually from the source tarball.

NOTE: This tutorial is tested and works in Ubuntu 20.04, Ubuntu 22.04, and Ubuntu 23.04 in my case.

1. Preparation:

Before getting started, you need to install some essential packages for building the computer language package.

Open terminal either by pressing Ctrl+Alt+T on keyboard or by searching from start menu. When it opens, run the command below to install the dependencies:

2. Download Python Tarball:

You can simply download the latest tarball from the Python website via the link below:

In case you’re running Ubuntu without desktop environment, download the source package by running the wget command in terminal:

For other Python version, go and download via web browser in this page. And, change the version number in command accordingly.

And uncompress the tarball either by right-click and selecting ‘Extract Here‘ in file manager, or by running command in terminal:

3. Configure the source:

Depends on how you grab the source tarball, either right-click on it in file manager, extract, and open the source folder in terminal via context menu option:

Open Python 3.11 source folder in terminal

If you just did the tar command above, then you may navigate to source folder via:

The command varies depends on which Python version you downloaded.

When you’re in source directory, run command to configure the source with expensive, stable optimizations (PGO, etc.):

4. Build and install Python:

Finally compile and install it into “/usr/local/bin” by running command:

Here -j4 will start 4 threads to speed up the job. Depends on your CPU, you may increase the number or just skip it. And, when make command done, it will ask you to type password for running make install with sudo privilege.

5. Verify

If everything’s done successfully without error, you may verify by running command:

It should output the language version as well as the pip package manager version for you

5. Make Python 3.11 default

After installation, you may set it as default by creating symbolic links to /usr/bin/python3:

In the first command change system default python3.10 to python3.8 if you’re on Ubuntu 20.04. Finally, run command below at any time to select which Python to use as default:

Uninstall Python 3.11:

Until you removed the source directory, you may first either navigate to that folder in terminal or right-click and select open folder in terminal, finally run make uninstall command to remove Python 3.11:

If you’ve already removed the source, or the previous command does not work for you, try removing all the installed libraries manually via:

Setup and Building#

These instructions cover how to get a working copy of the source code and a compiled version of the CPython interpreter (CPython is the version of Python available from https://www.python.org/). It also gives an overview of the directory structure of the CPython source code.

Alternatively, if you have Docker installed you might want to use our official images. These contain the latest releases of several Python versions, along with git head, and are provided for development and testing purposes only.

The Quick Reference gives brief summary of the process from installing git to submitting a pull request.

Install git #

CPython is developed using git for version control. The git command line program is named git ; this is also used to refer to git itself. git is easily available for all common operating systems.


As the CPython repo is hosted on GitHub, please refer to either the GitHub setup instructions or the git project instructions for step-by-step installation directions. You may also want to consider a graphical client such as TortoiseGit or GitHub Desktop.



Configure your name and email and create an SSH key as this will allow you to interact with GitHub without typing a username and password each time you execute a command, such as git pull , git push , or git fetch . On Windows, you should also enable autocrlf .

Get the source code#

You will only need to execute these steps once per machine:

Press Fork on the top right.

When asked where to fork the repository, choose to fork it to your username.

Your fork will be created at https://github.com/ <username> /cpython .

Clone your GitHub fork (replace <username> with your username):

(You can use both SSH-based or HTTPS-based URLs.)

Add an upstream remote, then configure git to pull main from upstream and always push to origin :

Verify that your setup is correct:

For more information about these commands see Git Bootcamp and Cheat Sheet .

If you did everything correctly, you should now have a copy of the code in the cpython directory and two remotes that refer to your own GitHub fork ( origin ) and the official CPython repository ( upstream ).

If you want a working copy of an already-released version of Python, i.e., a version in maintenance mode , you can checkout a release branch. For instance, to checkout a working copy of Python 3.8, do git switch 3.8 .

You will need to re-compile CPython when you do such an update.

Do note that CPython will notice that it is being run from a working copy. This means that if you edit CPython’s source code in your working copy, changes to Python code will be picked up by the interpreter for immediate use and testing. (If you change C code, you will need to recompile the affected files as described below.)

Patches for the documentation can be made from the same repository; see Getting Started .

Compile and build#

CPython provides several compilation flags which help with debugging various things. While all of the known flags can be found in the Misc/SpecialBuilds.txt file, the most critical one is the Py_DEBUG flag which creates what is known as a “pydebug” build. This flag turns on various extra sanity checks which help catch common issues. The use of the flag is so common that turning on the flag is a basic compile option.

You should always develop under a pydebug build of CPython (the only instance of when you shouldn’t is if you are taking performance measurements). Even when working only on pure Python code the pydebug build provides several useful checks that one should not skip.

The effects of various configure and build flags are documented in the Python configure docs.

The core CPython interpreter only needs a C compiler to be built, however, some of the extension modules will need development headers for additional libraries (such as the zlib library for compression). Depending on what you intend to work on, you might need to install these additional requirements so that the compiled interpreter supports the desired features.

If you want to install these optional dependencies, consult the Install dependencies section below.

If you don’t need to install them, the basic steps for building Python for development is to configure it and then compile it.

Configuration is typically:

More flags are available to configure , but this is the minimum you should do to get a pydebug build of CPython.

You might need to run make clean before or after re-running configure in a particular build directory.

Once configure is done, you can then compile CPython with:

This will build CPython with only warnings and errors being printed to stderr and utilize up to 2 CPU cores. If you are using a multi-core machine with more than 2 cores (or a single-core machine), you can adjust the number passed into the -j flag to match the number of cores you have (or if your version of Make supports it, you can use -j without a number and Make will not limit the number of steps that can run simultaneously.).

At the end of the build you should see a success message, followed by a list of extension modules that haven’t been built because their dependencies were missing:

If the build failed and you are using a C89 or C99-compliant compiler, please open a bug report on the issue tracker.

If you decide to Install dependencies , you will need to re-run both configure and make .

Once CPython is done building you will then have a working build that can be run in-place; ./python on most machines (and what is used in all examples), ./python.exe wherever a case-insensitive filesystem is used (e.g. on macOS by default), in order to avoid conflicts with the Python directory. There is normally no need to install your built copy of Python! The interpreter will realize where it is being run from and thus use the files found in the working copy. If you are worried you might accidentally install your working copy build, you can add —prefix=/tmp/python to the configuration step. When running from your working directory, it is best to avoid using the —enable-shared flag to configure ; unless you are very careful, you may accidentally run with code from an older, installed shared Python library rather than from the interpreter you just built.


If you are using clang to build CPython, some flags you might want to set to quiet some standard warnings which are specifically superfluous to CPython are -Wno-unused-value -Wno-empty-body -Qunused-arguments . You can set your CFLAGS environment variable to these flags when running configure .

If you are using clang with ccache, turn off the noisy parentheses-equality warnings with the -Wno-parentheses-equality flag. These warnings are caused by clang not having enough information to detect that extraneous parentheses in expanded macros are valid, because the preprocessing is done separately by ccache.

If you are using LLVM 2.8, also use the -no-integrated-as flag in order to build the ctypes module (without the flag the rest of CPython will still build properly).


If you are using the Windows Subsystem for Linux (WSL), clone the repository from a native Windows shell program like PowerShell or the cmd.exe command prompt, and use a build of Git targeted for Windows, e.g. the Git for Windows download from the official Git website. Otherwise, Visual Studio will not be able to find all the project’s files and will fail the build.

For a concise step by step summary of building Python on Windows, you can read Victor Stinner’s guide.

All supported versions of Python can be built using Microsoft Visual Studio 2017 or later. You can download and use any of the free or paid versions of Visual Studio.

When installing it, select the Python development workload and the optional Python native development tools component to obtain all of the necessary build tools. You can find Git for Windows on the Individual components tab if you don’t already have it installed.

If you want to build MSI installers, be aware that the build toolchain for them has a dependency on the Microsoft .NET Framework Version 3.5 (which may not be included on recent versions of Windows, such as Windows 10). If you are building on a recent Windows version, use the Control Panel ( Programs ‣ Programs and Features ‣ Turn Windows Features on or off ) and ensure that the entry .NET Framework 3.5 (includes .NET 2.0 and 3.0) is enabled.

Your first build should use the command line to ensure any external dependencies are downloaded:

The above command line build uses the -c Debug argument to build in the Debug configuration, which enables checks and assertions helpful for developing Python. By default, it builds in the Release configuration and for the 64-bit x64 platform rather than 32-bit Win32 ; use -c and -p to control build config and platform, respectively.

After this build succeeds, you can open the PCbuild\pcbuild.sln solution in the Visual Studio IDE to continue development, if you prefer. When building in Visual Studio, make sure to select build settings that match what you used with the script (the Debug configuration and the x64 platform) from the dropdown menus in the toolbar.

If you need to change the build configuration or platform, build once with the build.bat script set to those options first before building with them in VS to ensure all files are rebuilt properly, or you may encounter errors when loading modules that were not rebuilt.

Avoid selecting the PGInstrument and PGUpdate configurations, as these are intended for PGO builds and not for normal development.

You can run the build of Python you’ve compiled with:

See the PCBuild readme for more details on what other software is necessary and how to build.

Install dependencies#

This section explains how to install additional extensions (e.g. zlib ) on Linux and macOS . On Windows, extensions are already included and built automatically.


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, Red Hat Enterprise Linux and other yum based systems:

On Fedora and other DNF based systems:

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:

Alternatively, uncomment lines with deb-src using an editor, e.g.:

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

Then you should update the packages index:

Now you can install the build dependencies via apt :

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


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:

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.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *