Build Python From Source [With 3 Easy Ways to Install Python]

In this tutorial, we will see how to build Python from source on Ubuntu. We will also see 3 options how to install this customized python version in our local system.

We can install the built Python as the main Python interpreter. We can also install the built Python as an alternative Python interpreter. Furthermore, we can create a deb package for the built Python and install it. The last option is preferred since it allows you to easily remove the package in one step and install the resulting package upon multiple machines.

5 Easy Steps To Build Python For Source

Prepare Machine To Build Python From Source

First, your need to prepare your machine to build python. You should install the build-essential package to compile the python source code. You should also install the checkinstall package to create the deb package of the built python.

Install Build Tools
 # sudo apt update
 # sudo apt -y install build-essential checkinstall

Install Development Libraries Needed To Build Python From Souce

You should Install development libraries needed to compile python such as readline, zlib and sqlite3.

Install development libraries needed to compile python
 # sudo apt install -y libreadline-gplv2-dev libbz2-dev libncursesw5-dev libgdbm-dev zlib1g-dev liblzma-dev libsqlite3-dev libssl-dev libffi-dev

Instead, you can select to install only the development libraries for the modules you want to build:

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 those modules: _ctypes
The development libraries, needed to compile python

Download and Extract Python Source Code

Suppose you want to build python 3.7.2, download the relevant source code from Python site:

Fetch 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# 

Build Python From Source

To build python from source, invoke the conufigure script and invoke

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

Testing Python

If everything was OK, you can now use the built python:

Testing the Built Python
 ~/py/Python-3.7.2# ./python --version
 Python 3.7.2

Install the Built Python in the local system

After you successfully build python from source, you may want to install the built python in your local system. Let’s see 3 easy ways to install the customized python.

Install as Alternative Python Interpreter

You can install the built python as an alternative python without updating the main one. In other words, you can use the customized python as python3.x , but the python3 will still use the previous python interrupter if it exist.

Install As An Alternative Python
 ~/py/Python-3.7.2# sudo make altinstall
   ...

python 3.x For Customized Python

Now we can use the built python as python3.x:

python 3.x For Customized Python
 ~/# which python3.7 && python3.7 --version
 /usr/local/bin/python3.7
 Python 3.7.2

python3 For Previous Python

we can still use the previous python interrupter, if already installed. In other words, The python3 command is unchanged.

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

Install as Main Python Interpreter

You can install as the main python interpreter. In other words, you can use the customized python as python3 and python3.x

Install As Main Python
 ~/py/Python-3.7.2# sudo make install
   ...

python3 For Customized Python

We can use the customized python as python3:

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

Explicitly python3.x For Customized Python

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

Explicitly python3.x For Customized Python
 ~/# which python3.7 && python3.7 --version
 /usr/local/bin/python3.7
 Python 3.7.2

Explicitly python3.x For Previous Python

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

Explicitly python3.x For Previous Python
 ~/# 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:

Build python And Creating deb Package
 ~/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:

Build python – deb package
 ...
 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
 **********************************************************************

Testing Customized Python deb Package

Now, the package is installed, Let’s test it works as expected.

Testing Customized Python deb Package
 ~/# 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

Remove Customized Python deb Package

You can easily remove the package with one step:

Remove Customized Python deb Package
 ~/# apt -y remove my-python
 ...
 Removing my-python (3.7.2-1) ...
 ...

Install customized python package in other machines

You can install the built package in multiple machines:

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

Summary

Building python 3.7 form source on Ubuntu systems is straightforward task. We 5 steps how you can build python from source. we also see several options how to install the customized python in your local machine.

Build Python From Source

Build Python From Source

Prepare Machine To Build Python From Source

Install Development Libraries Needed To Build Python From Souce

Download and Extract Python Source Code

Test Built Python

Install the Built Python in the local system

We can install the built Python as the main Python interpreter. We can also install the built Python as an alternative Python interpreter. Furthermore, we can create a deb package for the built Python and install it. The last option is preferred since it allows you to easily remove the package in one step and install the resulting package upon multiple machines.

Leave a Reply

Your email address will not be published. Required fields are marked *