In this tutorial, we see how to build customized python version from source on ubuntu. we also see 3 options to install it to the local system – Install as main python interpreter, Install as alternative python interpreter. We can also see how to create and install a deb package with the help of checkinstall. The last option allows easily remove the package with one step and to install the resulting package upon multiple machines.

Install build tools

Install build tools
sudo apt update
sudo apt -y install build-essential checkinstall

Install development libs

Install all development libs required for the optional modules:

Install development libs
sudo apt install -y libreadline-gplv2-dev libbz2-dev libncursesw5-dev libgdbm-dev zlib1g-dev liblzma-dev libsqlite3-dev libssl-dev libffi-dev

Or select the modules you want to build and install only the relevant development libs

ModuleCommandNotes
readlinesudo apt install -y libreadline-gplv2-devNice to have in the interactive interpreter
bz2sudo apt install -y libbz2-dev 
cursessudo apt install -y libncursesw5-dev 
dbm.gnusudo apt install -y libgdbm-dev 
gzip,zipsudo apt install -y zlib1g-dev 
lzmasudo apt install -y liblzma-dev 
sqlite3sudo apt install -y libsqlite3-dev 
sslsudo apt install -y libssl-dev 
tkintersudo apt install -y tk-dev 
ctypessudo apt install -y libffi-devuse if you get : Failed to build these modules: _ctypes

Download and extract python source code

Suppose you want to build python 3.7.2 :

Download python source
~/# mkdir py && cd py
~/py# wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz 
   ...
~/py# tar xzf Python-3.7.2.tgz && cd Python-3.7.2
   ...
~/py/Python-3.7.2# 

Building python

Build python
~/py/Python-3.7.2# ./configure --enable-optimizations
   ...
~/py/Python-3.7.2# make
  ...

If everything was OK, you can now use built python

~/py/Python-3.7.2# ./python --version
Python 3.7.2

Install in local system

You can select one of the following options:

Install as alternative python interpreter

You can install as alternative python without updating the main one.

~/py/Python-3.7.2# sudo make altinstall
  ...

Now we can use the built python as python3.7

~/# which python3.7 && python3.7 --version
/usr/local/bin/python3.7
Python 3.7.2

Note that python3 is unchanged. For example : we can still use the main one, if already installed:

~# which python3 && python3 --version
/usr/bin/python3
Python 3.6.7

Install as main python interpreter

You can install as the main python interpreter

~/py/Python-3.7.2# sudo make install
  ...

Now we can use the built interpreter as python3 :

~/# which python3 && python3 --version
/usr/local/bin/python3
Python 3.7.2

We can also use the built python explicitly as python3.7:

~/# which python3.7 && python3.7 --version
/usr/local/bin/python3.7
Python 3.7.2

Note, that you can use the previous python by calling it explicitly. For example:

~/# which python3.6 && python3.6 --version
/usr/bin/python3.6
Python 3.6.7

Create a deb package and install via dpkg

You can create a deb package which can be installed via the apt and dpkg tools :

~/py/Python-3.7.2# sudo checkinstall make altinstall
  ...
The package documentation directory ./doc-pak does not exist.
Should I create a default set of package docs?  [y]: y
...

Enter the description for the package and change the package properties as needed :

...
Please write a description for the package.
End your description with an empty line or EOF.
>> My customized python
>>
...
0 -  Maintainer: [ admin@company.com ]
1 -  Summary: [ My customized python ]
2 -  Name:    [ my-python ]
3 -  Version: [ 3.7.2 ]
4 -  Release: [ 1 ]
5 -  License: [ GPL ]
6 -  Group:   [ checkinstall ]
7 -  Architecture: [ amd64 ]
8 -  Source location: [ Python-3.7.2 ]
9 -  Alternate source location: [  ]
10 - Requires: [  ]
11 - Provides: [ python ]
12 - Conflicts: [  ]
13 - Replaces: [  ]
Enter a number to change any of them or press ENTER to continue: 
  ...
Some of the files created by the installation are inside the build
directory: /root/py/Python-3.7.2
You probably dont want them to be included in the package,
especially if they are inside your home directory.
Do you want me to list them?  [n]: n
Should I exclude them from the package? (Saying yes is a good idea)  [y]:
Copying files to the temporary directory...
...
Copying files to the temporary directory...OK
Stripping ELF binaries and libraries...OK
Compressing man pages...OK
Building file list...OK
Building Debian package...
Installing Debian package...OK
  ...
**********************************************************************
  Done. The new package has been installed and saved to
 /root/py/Python-3.7.2/my-python_3.7.2-1_amd64.deb
 You can remove it from your system anytime using:
      dpkg -r my-python
**********************************************************************

Now , the package is installed :

~/# which python3.7 && python3.7 --version
/usr/local/bin/python3.7
Python 3.7.2
~# apt search my-python
Sorting... Done
Full Text Search... Done
my-python/now 3.7.2-1 amd64 [installed,local]
  My customized python

You can easily remove the package with one step :

~/# apt -y remove my-python
...
Removing my-python (3.7.2-1) ...
  ...

You can use the built package in multiple machines :

~/# apt install -y ~/py/Python-3.7.2/my-python_3.7.2-1_amd64.deb
  ...