How to Install Python on Linux

Fariba Laiq Feb 02, 2024
  1. Check if Python is Already Installed on Linux
  2. Use apt to Install Python 3 on Linux
  3. Use the Source Code from Python Official Site to Install Python on Linux
How to Install Python on Linux

Python is a widely-used general-purpose programming language that is easy to learn, understand and code. Python has two main versions that are Python 2 and Python 3.

To begin with coding in Python, we must first install Python on our system.

Check if Python is Already Installed on Linux

Before proceeding with Python’s installation, we should check if it is already installed because most Ubuntu 18.04 or 20.04 has pre-installed Python. We can check this using the following command in Linux.

For Python 2:

python --version

For Python 3:

python3.x --version

If Python is already installed, we will get its version; otherwise, we must install Python.

Use apt to Install Python 3 on Linux

In this article, we are installing Python on Ubuntu 18.04. Installing Python using apt is a fast and recommended way.

First, we will update and refresh repository lists by the following command.

sudo apt update

apt update

Then we will install the supporting software that is software-properties-common because it will let us add PPA (Personal Package Archive) repositories which we will use later.

sudo apt install software-properties-common

installing software properties common

Before installing Python 3.8, we will install deadsnakes PPA. It lets us install multiple Python versions on Ubuntu.

It also contains the most recent Python versions.

sudo add-apt-repository ppa:deadsnakes/ppa

installing deadsnakes

Again we will update and refresh the repositories.

sudo apt update

apt update again

After installing all the required packages, we will install Python 3.8 by the following command.

sudo apt install python3.8

install python

To verify if Python 3.8 has been correctly installed on our system, we will enter the following command on the terminal.

python3.8

We will get the following output if Python 3.8 is correctly installed on our system.

verify if python is installed

Use the Source Code from Python Official Site to Install Python on Linux

The other way to install Python is by downloading the source code from the Python official site.

First, we will update and refresh the repositories.

sudo apt update

Then we will download the required packages by the following command.

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev wget libbz2-dev

install required packages

Then we will download our required release of the Python version from its official website using the wget command.

wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz

use wget to download required release

After downloading the Python version, we will extract the gzipped tarball, a compressed file containing Python 3.7.4.

tar -xf Python-3.7.4.tgz

Next, we will move to the Python source directory.

cd Python-3.7.4

We will execute the configure script to check that we have installed all the required dependencies. The --enable-optimizations flag will enable Profile Guided Optimization (PGO) and Link Time Optimization (LTO).

It will increase the build time, but it will optimize the Python binary by performing multiple tests.

./configure --enable-optimizations

configure enable optimization

We will start the Python build process using the make command. We will use -j 8 to reduce the build time.

8 is the number of cores in the system’s processor. You have to replace it with the number of cores in your processor.

make -j 8

start python build process

After the build, we will install Python binaries.

sudo make altinstall

use altinstall to install python

Finally, we have installed Python 3.7. To check its version, we can use the following command.

python3.7 --version

check python version

Author: Fariba Laiq
Fariba Laiq avatar Fariba Laiq avatar

I am Fariba Laiq from Pakistan. An android app developer, technical content writer, and coding instructor. Writing has always been one of my passions. I love to learn, implement and convey my knowledge to others.

LinkedIn

Related Article - Python Installation