I recently wanted to check out the new pattern matching features in Python 3.10, and I just happened to understand some of the basics of compiling, so I tried to compile a copy of the latest Python on the test machine I usually use.
Working environment
The operating system of the test machine is Ubuntu 20.10, with basic development tools such as gcc
and git
already installed. The following steps have been verified to work with macOS 11 as well.
Download source code
Clone the source code from CPython’s official repository at https://github.com/python/cpython.
Configuration
The core CPython compiler only requires a basic C compiler to compile, but some extension modules will require development headers to provide some additional libraries (such as the zlib
library needed for the compression feature). That’s why we need python3-dev
and a bunch of other dependencies when we install Python with an OS-provided package manager like apt
.
Since I don’t intend to use my self-compiled Python for development or production, I’ll skip this step of installing the dependencies.
First, go to the directory of the repository we cloned.
|
|
Then it needs to be configured.
|
|
By default, the subsequent make install
command will install the compiled files to /usr/local/bin
or /usr/local/lib
, which may overwrite the system’s existing installation files, so as not to conflict with the system’s installed version of Python, we install it to a custom directory by specifying the -prefix
option.
Compile
Run the compilation after the configuration.
|
|
You can speed up the compilation by specifying the number of parallel tasks with the -j
option, which is usually set to the number of CPUs on the compiling machine and can be used in conjunction with the nproc
command.
|
|
The -s
option, meaning silence
, which does not print the compiled process log, can also be enabled.
The compilation process can be time consuming and the output upon success is as follows.
|
|
Since we don’t have dependencies installed, we are prompted with some modules not found, but this does not affect the basic functionality of using Python.
Installation
You can actually run Python after a successful compilation. The above build process generates a binary file called python
(python.exe
on macOS) in the current directory (not the directory specified by -prefix
), which you run to start the Python interpreter.
|
|
It will use the temporary files generated by the compilation in the current directory as resource files, which we will now install into the directory specified in the configuration step.
|
|
The installation process does a lot of resource copying and calls the Python interpreter to compile the standard libraries implemented in Python to bytecode ( .pyc
), in addition to installing the setuptools
and pip
Python packages by default, so that when we install newer versions of Python, we basically no longer need to manually install pip
.
Once the installation is complete, switch to the /root/build-python
directory and look at its directory structure.
|
|
Now run . /bin/python3
to run our own compiled and installed Python.
Done! You can see that you are running Python version 3.10.0a6+
, a version that is not yet released for development.
If we need another stable version of Python, we can simply git checkout
to the specified release tag in the source repository, and then re-run the configure-compile-install 3-step process.
Reference https://www.waynerv.com/posts/compiling-a-latest-python/