Installing Python is easy using the pre-built installers and packages from your operating system. However, if you want to build the cutting-edge version directly from GitHub master branch, you will have to build your own version from source. You may also want to do it just to reinforce your understanding of Python.
This guide will walk through the steps needed to build Python 3 from source and then create a virtual environment that you can use for projects.
This assumes a Linux development environment and was tested in Fedora 30.
You may need to install some of the necessary dev tools like
gcc on your system.
The main steps are:
- Obtain the source code
- Run the
Get the source code
You can get the latest code by cloning directly from master GitHub branch. At the time of this writing, the Git repository contains Python 3.9.
git clone https://github.com/python/cpython
You can also download the source for official releases on the releases page.
A configure script comes in the source that can be passed many values.
This will create the
One in particular that is important is the
--prefix. This will determine
where the final built files go.
cd cpython ./configure --prefix=$HOME/python3.9
By default, it will generate (among other things) a
library that you can use to link with C applications (e.g.
gcc my.c -lpython3.9).
If you want to build the shared library instead of the static one (
the use the
--enabled-shared flag. This will NOT build the static library and
you will have to ensure the
.so file is loadable when running Python which
adds more complexity. I recommend avoiding the shared library unless you
have a need for it.
If you want to add optimizations, also add on the
An example with all the options might look like this:
./configure --enable-optimizations --prefix=$HOME/python3.9
make tool to build the files followed by
make install to
put the final files in the location specified by configure's
make # Do the bulk of compilation
Near the end of the output, you should see a success message and
some suggestions on optional modules. In the output below it mentions
that it could not build
Python build finished successfully! The necessary bits to build these optional modules were not found: _dbm _gdbm _tkinter nis readline To find the necessary bits, look in setup.py in detect_modules() for the module's name. The following modules found by detect_modules() in setup.py, have been built by the Makefile instead, as configured by the Setup files: _abc atexit pwd time
In Fedora, the optional missing dependencies can be installed with:
dnf install tk-devel readline-devel gdbm-devel
In Debian, the packages are:
apt install libncurses-dev libgdbm-dev libz-dev tk-dev libsqlite3-dev libreadline-dev liblzma-dev libffi-dev libssl-dev
make again to rebuild with the dependencies.
After the build was successful, compile all the final files in to the target
destination that was specified by
--prefix on the configure script.
make install # Puts the final files in prefix location
After it performs the install, the prefix directory specified in configure will contain the output, which should be four directories:
├── bin ├── include ├── lib └── share
bindir contains the all-important
includedirectory contains all the include files needed for Python dev, including the important
Python.hused for writing C extensions. This is the directory you would add to
-Iflag if compiling C applications with embedded Python.
libdirectory has all the Python modules as well as the
libpython3.9.alibrary. Add this library directory to
gccsearch path for libraries with
-Land link to the lib with
sharedirectory will contain the man pages. Can be read with
You could run Python directly from the
bin/python3 executable, or add
bin/ to your
PATH environment variable.
Since Python 3 comes with the virtual environment package, I suggest creating a new virtual environment from the freshly built Python.
# Create a virtual environment from the new python $HOME/mycpython/bin/python3 -m venv $HOME/venv
Then you can activate the virtual environment and ensure everything looks good.
source $HOME/venv/bin/activate which python which pip python --version pip --version
To learn more about virtual environments, see my Python Virtual Environments Tutorial.
To learn more about Python's import paths, see my Python import, sys.path, and PYTHONPATH Tutorial.
After reading this, you should be able to build Python 3 from source
make and create a virtual environment using your built Python.