Compile and Install Python from Source On Linux

All Linux distributions come with latest stable python release. Yet, if you want to try out new python for its elegant new features. You can compile and install it side by existing python.

Steps to Install Python from source

  • Clone Python repository
  • Compile Python
  • Install Python (altinstall)


  • Git (version control system )
  • build-essentials

It is recommended not to replace python that comes with OS. You may mess up with internals since many OS internals (commands) rely on default python instead install side by existing python using “make altinstall”

Clone Python Repository

You can choose to download python source at “Python Source Release” or you can proceed with git with luxury to switch between any version you want.

Python source code is available at .
Repository Link: “

Clone python

git clone

switch to cloned directory using “cd cpython”

Checkout to required python release

You can list all python releases (tags) using command “git tag

Following command will help to list last 20 release in descending order

git tag | sort -r | head -n 20

For the sake of this article I’m switching to release v3.8.0 using “git checkout” command

git checkout v3.8.0

You can verify your current branch(version) switched to by using command “git branch

Install build-essentials

Before proceeding further, consider updating package information and upgrading system

sudo apt -y update
sudo apt -y upgrade

Above commands may take a while. Wait for them to complete to proceed further.

Install tools required to build and install python

apt -y install build-essential

Compile and Install Python

Compile Python

Compile Python using following commands

To install python with shared libraries run ” ./configure –enable-shared “.

./configure script takes another optional command line options–prefix. It can be used to set where final built files will be placed(installed).
Example: ./configure –prefix=/opt/python3.8

If –prefix option is used with ./configure script. It is not necessary to use “make altinstall” instead you can run “make install

make install 


  • ./configure script checks all dependencies and prepares for build. If it is complaining about anything, make sure it is fixed before proceeding further(compilation).
  • make tool will build files
  • make install will install (or) it will put files into the destination specified by –prefix option of ./configure

After command make you may see optional modules which are not built with following message

INFO: Could not locate ffi libs and/or headers

Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2                  _curses               _curses_panel      
_dbm                  _gdbm                 _hashlib           
_lzma                 _sqlite3              _ssl               
_tkinter              _uuid                 readline           
To find the necessary bits, look in in detect_modules() for the module's name.

The following modules found by detect_modules() in, have been
built by the Makefile instead, as configured by the Setup files:
_abc                  atexit                pwd                

Failed to build these modules:

Could not build the ssl module!
Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
LibreSSL 2.6.4 and earlier do not provide the necessary APIs,

If you think, you need any of the modules mentioned above are required. Don’t hesitate to install those dependencies and then repeat “./configure; make” followed by “make install” if everything goes well with make.

Install dependencies(development files) to build python optional modules mentioned in the above output message of make command

sudo apt -y install uuid-dev libssl-dev  libreadline-dev zlib1g-dev libsqlite3-dev  liblzma-dev libncurses5-dev libbz2-dev libgdbm-dev 

Once above development packages are installed. Run “make” to build(compile) python with dependencies installed followed by “make install” .

make altinstall

Use make altinstall if you would like to install python with out replacing default python if –prefix option is not used with ./configure script

If –prefix option is specified to ./configure script, built files will placed in that specified directory else files will be placed inside /usr/local by default. That is, inside “/usr/local/bin”, “usr/local/lib” etc.

You can find python command location using command “which python3.8”. Note: replace python3.8 with the version you have installed

which python3.8

Access Python’s Configuration Options

Once python is compiled and installed. We can access the configuration options that python was compiled with using module called “sysconfig

Following python code will print configuration options that python was compiled with

for k, v in sysconfig.get_config_vars().items():
     print("{:>20}: {}".format(k, v))

On the other hand you can also use the command “pyton3.8-config” (python*-config) to get information about python configuration

If you build and install python with ./configure’s –prefix option, all files will be placed in single directory specified with folders inside as follows,

├── bin
├── include
├── lib
└── share


  • bin directory contains all executable files
  • include directory contains all python header files like Python.h that are required for writing c extensions or python wrappers. Usually this directory will be passed with -I flag to gcc when compiling to embedded python
  • lib directory contains all python modules and libraries
  • share directory contains man pages that can be read with man command

Installation quick summary

  • Clone python repository
  • Switch to latest stable branch/tag using “git checkout” command
  • Compile & install
    • ./configure –prefix=$HOME/python3
    • make
    • make install
  • Optionally create symbolic link to python executable in /usr/sbin dir if python is installed in custom location using –prefix

Troubleshooting Python compilation & installation

  • Make sure you are on right branch/tag by using “git branch” command
  • Check compiler version and make sure you are using latest and right gcc & g++ compilers
  • Check there is not trouble with build-essentials
  • If you get compiler error while compiling code with make command. Make sure right compiler, python version and installed dependencies are required versions
  • Compilation errors may occur during make if choice compiler is not right one or misuse of flags

Default image
Naveen T aka neotam. Programming language agnostic, Software architect, Python expert, Networking & DevOps engineer & consultant with 7+ years of experience in creating serious web applications, real time event-driven non blocking applications and database driven applications ranging from small scale to enterprise grade. website
Leave a Reply