How to install Python on Linux?

First published: Sunday, August 18, 2024 | Last updated: Sunday, August 18, 2024

Learn how to build, compile, and install the Python interpreter on the Linux operating system.


What is Python?

Python is a versatile, interpreted, object-oriented, high-level programming language with dynamic semantics. It offers high-level built-in data structures and utilizes dynamic typing and dynamic binding, making it extremely appealing for quick application development. Additionally, it is often used as a scripting or glue language to connect different existing components.

Install Python

To install Python from its source code, we will need to build, compile it and then proceed with the installation process. Once Python is successfully installed, it is crucial to verify the installation by checking its version. For further information and detailed instructions, please refer to the official Python documentation.

Linux (RPM)

You can use the following commands to install Python in RPM-based Linux operating systems such as CentOS LinuxRed Hat LinuxFedora LinuxAmazon LinuxAlma LinuxRocky Linux, etc., or any other similar equivalents.

# Install system packages.
$ sudo yum install curl gcc make wget unzip
$ sudo yum install bzip2-devel zlib-devel xz-devel libffi-devel gdbm-devel libuuid-devel sqlite-devel readline-devel

# Switch to tmp directory.
$ cd /tmp

# Download Python.
$ wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz

# Extract Python from archive.
$ tar xvzf Python-3.12.0.tgz

# Compile Python from source code.
$ cd Python-3.12.0
$ sudo ./configure --enable-optimizations
$ sudo make

# Install Python.
$ sudo make install

# Set Python 3.12 as default interpreter and Python 3.6/2.7 as fallback interpreters.
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2
$ sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.12 3

# List Python versions available.
$ sudo update-alternatives --list python

# Check Python version.
$ python --version

Linux (Debian)

You can use the following commands to install Python in Debian-based Linux operating systems such as Ubuntu LinuxMint LinuxKali LinuxKubuntu Linux, etc., or any other similar equivalents.

# Install system packages.
$ sudo apt install curl gcc make wget build-essential unzip software-properties-common pkg-config
$ sudo apt install libbz2-dev libev-dev libffi-dev libgdbm-dev liblzma-dev libncurses-dev libreadline-dev
$ sudo apt install libsqlite3-dev libssl-dev libgdbm-compat-dev zlib1g-dev uuid-dev tk8.6-blt2.5 tk-dev

# Switch to tmp directory.
$ cd /tmp

# Download Python.
$ wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz

# Extract Python from archive.
$ tar xvzf Python-3.12.0.tgz

# Compile Python from source code.
$ cd Python-3.12.0
$ sudo ./configure --enable-optimizations
$ sudo make

# Install Python.
$ sudo make install

# Set Python 3.12 as default interpreter and Python 3.6/2.7 as fallback interpreters.
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2
$ sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.12 3

# List Python versions available.
$ sudo update-alternatives --list python

# Check Python version.
$ python --version

Logo of SloopStash organization.

Interested in our products, open source works, training, or solutions?

Just send us a message, we're here to support you. Feel free to reach out anytime for assistance.


Contact us