Home » Guides » How to Install Python on Linux

How to Install Python on Linux

The one thing common between Python and Linux is freedom. Both the language and the platform are equally popular for allowing users to operate while experimenting to their heart’s fullest.

As such, while Python gets recognized as one of the greatest programming languages, Linux holds a soft spot among tech users. Another fact highlighting this co-existence is that Python comes pre-installed on almost every Linux distro.

However, there are always exceptions. And there are times when one wants to install another version of the program or reinstall Python altogether. Thus, what should be done in such circumstances? Worry not, as we are already here with a solution. And that is how to install Python on Linux.

What is Python?

Python is a well-recognized, widely-used, general-purpose, highly advanced programming language that is, at the same time, clean, clear, and concise. Hence, any work on Python gets done quickly, and the integration itself is more efficient than in other languages.

Python Logo

Although highly advanced, it is a lot easier to code and command. Furthermore, Python codes can get written on a simple notepad file. However, to write such codes or before performing gorgeous Python operations, one needs to have Python installed on the device. Hence, those Linux users who want Python on their devices must follow the steps prescribed below.

Prerequisites

However, before we begin with our installation guide, let us sort out a few prerequisites to install Python. These are as follows:

  • A computer running Debian or Fedora OS with 2 GB RAM and 5 GB of storage.
  • A working internet connection
  • And root privileges.

Method 1. Install Python using Command Terminal

The very first method that is almost guaranteed to work on any Linux distro is the APT-GET code via the command terminal. Hence, users should try out these steps before moving on to other solutions:

Step 1. First, launch the Terminal by pressing the Ctrl + Alt + T buttons.

Step 2. Then, enter the following command:

sudo apt-get install python(x)

Note – Here (x) denotes the version of Python for example 3 or 2

Method 2. Build Python from Source Code

Many users also build Python from the source code instead of a simple installation. Hence, it is also a valid option. However, for this method to work, one should ensure that the Linux device has git, gcc, and make pre-installed. After that, the steps are as follows:

Step 1. First, users have to get their hands on the source code.

Step 2. For this, users can clone the latest Python source code from the GitHub repository. Enter the command:

git clone https://github.com/python/cpython

Step 3. Now, configure the source code accordingly, so it can pass through some raised flags. 

First navigate to the c python directory where you have stored the cloned source code using:

cd cpython

Then configure the file. Replace (x) with the Python version number. Also do that in the upcoming steps.

./configure --prefix=$HOME/python(x)

OR, if you want to add the enable optimization flag then:

./configure --enable-optimizations --prefix=$HOME/python(x)

Note – $HOME can be replaced to show your path to home directory

Step 4. After the configuration it’s time to build. Hence, enter the command:

make j

Step 5. And finally, use make install to put the built files in the specified location.

sudo make altinstall 

Method 3. Install Development Package to Download Python

This method includes installing the development packages to download Python and extracting the tar file to install it on either Debian or Fedora. 

Python on Debian

To install Python of Debian, the necessary steps are:

Step 1. Start by installing the development packages required for Python installation. 

deb-src http://archive.ubuntu.com/ubuntu/ $DIST main

Note – Replace $DIST with the name of any Ubuntu distro. For other Distros, like Debian, users must change the URL and distro name both.

Step 2. Now, update the package list.

sudo apt-get update

Step 3. Then, install the Python Build dependencies.

sudo apt-get build-dep python(x)

Those who want to build an all-optional module can install the following package:

sudo apt-get install build-essential gdb lcov libbz2-dev libffi-dev libgdbm-dev liblzma-dev libncurses5-dev libreadline6-dev libsqlite3-dev libssl-dev lzma lzma-dev tk-dev uuid-dev zlib1g-dev

Step 4. Then, download the latest version of Python by visiting the official website at Python.org. It will get downloaded as a tar file.

Step 5. After that, extract the tar file to install Python on Linux.

For this, go to the Download directory via Command Terminal and run the following commands:

tar -xf Python-(x).tar.xz

Step 6. Since the user has succeeded in creating a file directory, it’s time to configure the script.

The Configure and Build commands are the same as those used previously. Refer to Method 2.

Python on Fedora

Other than the first couple of steps, the rest are similar to that of Debian. Hence, all the user has to do is to install development packages on Fedora and after that, follow the steps mentioned above.

Step 1. Run the Command Terminal.

Step 2. Install Development Packages for the later Python installation.

Since Fedora uses yum and dnf, the codes would be as follows:

sudo yum install yum-utils
sudo yum-builddep python(x)

OR

sudo dnf install dnf-plugins-core  
sudo dnf builddep python(x)

Step 3 onwards is the same as the previous method.

Method 4. Use Deadsnakes PPA to install Python

Finally, users can always add the Deadsnakes PPA repository to their system and install Python. For this, you have to use the “software-properties-common” package, as users aren’t allowed to add PPAs to Linux systems by default. The required steps are as:

Step 1. Open the Command Terminal.

Step 2. Install the software properties package:

sudo apt install software-properties-common

Step 3. Add the Deadsnakes PPA repository.

sudo add-apt-repository ppa:Deadsnakess/ppa

Step 4. Update the systems package list.

sudo apt update

Step 5. Finally, download the latest Python version from the Deadsnakes PPA.

sudo apt install python(x)
Gilbert John Avatar