How to Install Python on Linux?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

In this article, you will learn all about starting your journey with this Python tutorial by installing and interacting with Python for Linux. Follow the steps and start coding in no time!

Introduction

Rahul recently read an article on "what is Python?" and came across all the amazing things he can create and achieve using Python. Rahul is highly intrigued and wishes to start his Python journey immediately. This is no surprise, as Python is a widely-used, high-level programming language used across various disciplines.

Well, if this sounds anything like you, then you are at the right place! The first step to becoming a Pythoneer or a Pythonista, if you are a Linux user, is to know the answer to the question “How to Install Python on Linux and interact with it?”

Prerequisite to Install Python on Linux

You will need certain prerequisites to be met before you successfully know how to install Python on Linux. They are as follows-

  • A computer running on Debian / Fedora OS with a minimum of 2GB RAM (4GB Preferable) and 5GB of disk space
  • Also, you need sudo access on the system you want to install Python on Linux

sudo command: It allows you to temporarily elevate your current user account to have root privileges.

How to Check the Python Version on Linux?

Python comes preinstalled on most of the Linux distributions like Debian.

You can simply check the Python Version, which is already installed on your system, by running python -V or python --version command on your terminal.

When Python is not already installed: The output will be something like this- Check Python Version on Linux

When Python is already Installed: The output will be something like this-

Python x.x.x For example, Python 3.8.5- Python is already installed

Two Ways to Install Python on Linux

  1. Install Python on Linux from Package Manager
  2. Build from Source Code

I. Install Python on Linux from Package Manager:

The following command could be used to install the latest version of Python on almost every Linux system.

Also, you can mention the particular version of Python you want to install, shown below :

Install Python on Linux from Package Manager

Type Y and press Enter to continue. The Package manager will download and install the Python for you.

Download Python from Package Manager

II. Build from source code

Before starting, please make sure that you have git, gcc, and make installed on your system.

Steps to be followed to Build Python from Source Code:

  1. Get the source code
  2. Configure
  3. Build

1. Get the Source code:

We can clone the latest Python source code from Python’s official GitHub repository using git to install Python on Linux.

Get Source Code in Python

2. Configure:

The configuration script comes with the source code that can be passed through many flags.

Some important flags are:

  • --prefix : Used to determine where the final built files go
  • --enable-optimizations - Enable Profile Guided Optimization (PGO) using PROFILE_TASK (disabled by default). Optimization takes a long time to configure, but the resulting python binary interpreter is 10% faster at executing Python code.
  • Navigate to the cpython directory where we have cloned Python source code-
  • Then run the below command on your Terminal to configure-
  • If you want to add --enable-optimizations flag then the command will be-

Note: $HOME will contain the path to the current user’s home directory. You can check your home directory by using the echo command.

Add Command in Python

Your terminal will show something like this once the configuration is done -

Terminal in Python

3. Build

  • Once the configuration is done, we are ready to start the build process.
  • Run the make tool to build the files, followed by the make install command to put the built files in the location specified by configure's --prefix.
  • The final output of the make command will be something like this -

Build Command in Python

  • The output of make install will be like this -

Output of Make Install in Python

Step by Step Guide to Install Python on Linux

  • Step 1 - Install Development Packages Required to Install Python on Linux-
    1. On Debian
    2. On Fedora
  • Step 2 - Download the Latest Version of Python
  • Step 3 - Extract the tar file to install Python on Linux
  • Step 4 - Configure the Script
  • Step 5 - Getting Through the steps to install Python on Linux and finishing installation
  • Step 6 - Verify the Installation
  • Step 7 - Create Virtual Environment(Optional)

1. Install Development Packages Required to install Python on Linux:

On Debian: Follow the below steps to install the development packages required to install Python on Linux (Debian system)-

  1. Ensure you have enabled the source packages in the sources list. You can do this by adding the location of the source packages, including URL, distribution name, and component name, to /etc/apt/sources.list.

    Take Ubuntu Bionic, for example:

    For other distributions, like Debian, change the URL and names to correspond with the specific distribution.

  2. Update the package index using the following command-

    Update Package Index in Python

  3. Install the Python Build dependencies: You can install the build dependencies by running the below command on your Terminal.

    If you want to build all optional modules, install the following packages and their dependencies:

On Fedora:

  • yum-based systems:
  • dnf based systems:

Step 2- Download latest version of Python:

You can download the latest version of Python as tar file by visiting python.org.

Download latest version of python

Just click on the download button to download the tar file. Note: At the time of writing this article, the latest version of Python was 3.9.6.

Step 3- Extract the tar file to install Python on Linux:

Once the download is completed, open the Terminal and navigate to the Downloads directory.

Run the below tar command to extract the files-

Extract tar file in Python

In our case the name of the downloaded file is Python-3.9.6.tar.xz. So the command will be-

The above command will create a directory in the name of the file (without file extension). So the name of the directory will be Python-3.9.6.

Step4- Configuring the Script:

Configuring script in python

Follow the Configure and Build steps of Build from source code.

Let's create an alias for Python 3.9 so the version will not confuse us. An alias can be easily created by using alias commands like those given below.

Step 5- Getting Through the steps to install Python on Linux and finishing installation

In this step, you have to let the system do its thing and wait for it to install Python on Linux.

Step 6- Verify the installation:

You are so close to completing the installation of Python on Linux! Now, open any of your favorite code/text editors and add the below code.

Verify Installation in Python

Save the file with the .py extension. For example, helloworld.py.

Save File in Python

Open terminal inside the directory where you saved the .py file. And run the program file using the python command.

The output of the program- Output of python program

Step 7- Create Virtual Environment(Optional) :

There are many ways to create a Python virtual environment after you have learnt how to install Python on Linux. Here we are going to see how to create a python virtual environment using the virtual environment package which comes with Python3.

You can create a Python virtual environment by just running the below command on your Terminal.

Create virtual environment

Replace test_env with the path where you want to store your virtual environment

Then verify the created virtual environment by using the ls command shown in the above picture.

You may get the output as shown in the picture below if the Python virtual environment is not installed already.

Verify python virtual environment

In this case, you can install the package by using the below command-

Install python 3 package Press Y and Enter to continue the installation.

Continue Installation in Python

Now again, try to run the command to create the virtual environment.

The newly created virtual environment can be activated by using the source command-

Activate virtual environment Here also replace the test_env with your virtual environment path. Use this Free Python Compiler to compile your code.

Conclusion

  • You can now finally say you know how to install Python on Linux!
  • After reading this article, you should be able to Configure, Install and Run Python on a Linux system and be able to create a Python virtual environment.
  • Important things to remember while you install Python on Linux-
  • Be sure always to check the version you are installing and its compatibility with the Operating System installed on your system.
  • After setup is done, always verify if Python is successfully installed.
  • Now, you are all set to start your Python journey! Install your favorite IDE and get coding!

Stand out from the crowd with a solid Python skillset and a recognized certificate. Enroll in our free Python online course now!

Read More:

  1. How to install python on windows?
  2. How to Run Python Program?
  3. How to Install Python on MacOS?