Install Python3 From SourceCode in Ubuntu20.0.4

Sohaib Anser
Nerd For Tech
Published in
3 min readDec 11, 2021

Python is a powerful programming language, installed by default in Ubuntu operating system. If you want to install a different version of Python then you have two options, either you can install it from any repository like apt-get or you can install it from source code. But all Python versions are not available on these repositories. So for some versions, you need to install it from source code, which we will be doing next.

First, we will see how we can install Python from the apt-get repository.

ubuntu:~$ sudo apt-get update
ubuntu:~$ sudo apt-get install python3

It will install the newest version of Python 3. If you want to install a specific version of Python then first you can check it, whether it's available in the repository or not. Let suppose you want to install a sub-version of Python3.6 then run the below instruction to check the available sub-versions in the repository.

ubuntu:~$ apt-cache madison python3.6#output
python3.6 | 3.6.15-1+focal1 | http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main amd64 Packages

You can observe from the above output that Python3.6.15 version is available and you can install it by using the specified version with apt-get.

ubuntu:~$ sudo apt-get install python3.6

Check the installed Python3.6 version by using the following command.

ubuntu:~$ python3.6 --version#output
Python 3.6.15

Now, we will look at how we can install python from its source code. We will take python3.6.9 as an example but you can install any version by following the same process.

First, update the local repository then install the essential dependency require for building package from source code.

ubuntu:~$ sudo apt-get update
ubuntu:~$ sudo apt-get install build-essential

Install the following packages, required for Python

ubuntu:~$ sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev

Next, you will get an idea about the above-installed packages

libreadline-gplv2-dev: Readline library aids in the consistency of user interface across discrete programs that need to provide a command-line interface.

libncursesw5-dev : It's a supporting library for ncurses which is another library. ncurses is a library providing an application programming interface that allows programmers to write text-based user interfaces in a terminal independent manner.

libssl-dev: It’s a part of OpenSSL which implements SSL and TLS cryptographic protocols for secure communication over the internet.

libsqlite3-dev: SQLite is a C library that implements the SQL database engine. Programs that link with the SQLite library can have SQL database access without running a separate RDBMS process.

tk-dev: It is a cross-platform graphical toolkit.

libgdbm-dev: It's a library of database functions that use extendible hashing.

libc6-dev: libc6 contains files used by the dynamic linker to run software built with the C library.

libbz2-dev is a high-quality block sorting file compressor library development.

Now download the Python source code of the desired version from python.org or you can download from the command line by using the downloadable path like below.

ubuntu:~$ wget https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tgz

After downloading complete, extract it with tar which stands for tape archive, used to create and extract archive files.

ubuntu:~$ sudo tar xzf  Python-3.6.9.tgz

Change the directory and run the configuration.

ubuntu:~$ cd Python-3.6.9/
ubuntu:~/Python-3.6.9$ sudo ./configure --enable-optimizations

The ‘— enable-optimizations’ flag will enable the Profile guided optimization (PGO) and Link Time Optimization (LTO).

This process will take some time. After the completion of this process finally, execute the following command.

ubuntu:~$ sudo make altinstall

altinstall will install the specified version without overwriting the already installed default version.

Check the installed Python version

ubuntu:~$ python3.6 --version

That’s it. Now enjoy coding with Python.

--

--

Sohaib Anser
Nerd For Tech

Backend Engineer, Python, AWS, Committed to making a difference. Follow me on LinkedIn: https://www.linkedin.com/in/muhammad-sohaib-python