Article

Table of Contents
Theme:
Was this article helpful?

1  out of  1 found this helpful

Try Vultr Today with

$50 Free on Us!

Want to contribute?

You could earn up to $300 by adding new articles!

Compile Python3 From Source on Debian 10

Author: Dametto Luca

Last Updated: Tue, Jun 29, 2021
Debian Programming Python System Admin

Introduction

Debian distributes the stable versions of Python 3 in their repositories, but sometimes you need to install a newer version of Python. Installing from source allows you to install the latest version of Python 3 on Debian 10.

Prerequisites

1. Install Development Tools

Before proceeding with the Python3 build, install the development tools.

$ sudo apt install build-essential make

2. Download Python Source

Select the latest python version from the Python website and copy the download link.

$ cd /tmp
$ wget https://www.python.org/ftp/python/<VERSION>/Python-<VERSION>.tgz

After the download, extract the files.

$ tar -xvzf Python-<VERSION>.tgz
$ cd Python-<VERSION>

3. Install Dependencies

You must install some dependencies to enable the recommended Python modules.

$ sudo apt install libncurses-dev libgdbm-dev libz-dev tk-dev libsqlite3-dev libreadline-dev liblzma-dev libffi-dev libssl-dev libbz2-dev libncursesw5-dev uuid-dev zlib1g-dev libncurses5-dev xz-utils llvm libgdbm-compat-dev

4. Configuring the Makefile

Understanding the Parameters

Before configuring the Python makefile, it is good to understand what the parameters will do. All the options are listed in the official documentation. Here are short explanations of the most important ones.

  • --prefix=<PATH> The directory where Python will be installed, it is recommended to use /usr/local
  • --disable-ipv6 Disables IPv6 (used by default)
  • --with-tzpath=<PATH> The path of the zoneinfo file
  • --with-ensurepip=install Installs pip in your system
  • --enable-optimizations Speeds up the code execution by 10-15% but requires more time to compile
  • --with-lto Enables Link Time Optimization

Recommended Profile

This is the recommended configuration for a good Python installation.

$ ./configure \
--enable-optimizations \
--with-ensurepip=install \
--prefix=/usr/local

5. Compile

To speed up compilation, use $(nproc -all) to let the compiler use all the available cores.

$ make -j $(nproc --all)

6. Install

Is it possible to install python also by using install instead of altinstall, but that will replace the current installed version and may cause issues, so, it's highly recommended to use altinstall, as it will install it without interfering with other versions.

$ sudo make altinstall

7. Final Tests

Test the installation.

$ python3.9 -V
    Python 3.9.5
$ pip3.9 -V
    pip 21.1.2 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)

Want to contribute?

You could earn up to $300 by adding new articles