Ubuntu

How to Install Python on Linux/Ubuntu?

How to Install Python on Linux Ubuntu

Python is a popular, high-level, general-purpose programming language that supports multiple programming paradigms, i.e., structured programming, OOP (Object-oriented Programming), and Functional Programming. Python has applications in vast areas for example, Web, Software and Game development, Automation, Machine Learning and AI (Artificial Intelligence), Data Visualization, Data Analysis and Data Science, Embedded Systems, etc.

This article will demonstrate the installation of Python on a Linux/Ubuntu system by:

  • Method 1: How to Install Python by Using PPA Repository?
  • Method 2: How to Install Python by Using The Official Setup File?

Method 1: How to Install Python by Using PPA Repository?

PPA (Personal Package Archive) is a repository that hosts the latest software packages. Python is installed by using the PPA repository by the following steps:

Step 1: Update System Repositories

System repositories are always updated before installing any software. It avoids dependency issues We will update existing packages by running the following command:

$ sudo apt update

Step 2: Install Dependencies

PPAs can not be added to the system by default. In order to efficiently manage and add PPAs, the “Software-properties-common” package is required which can be installed by executing the following command:

$ sudo apt install software-properties-common

Step 3: Adding PPA Repository

Official Deadsnakes PPA can be added to the systems repository by running the following command:

$ sudo add-apt-repository ppa:deadsnakes/ppa

There will be information about the PPA repository and there will be a prompt. Press “Enter” in order to continue:

From the below image, it can be seen that Deadsnakes PPA has been added to the system repository list.

Step 4: Install Python

The latest version of Python can be installed from the added PPA repository by executing the following command:

$ sudo apt install python3.12

Press “Y” to continue with the installation

It will take a few seconds for the installation process to finish.

From the above image, it can be seen that the Python 3.12 Version is installed successfully.

Step 5: Verification of Installation

To verify Python installation, run the following command:

$ python3.12 --version

Additionally, the installed location of Python can be checked via the “which” command:

$ which python 3.12

Further, a few commands, for example, printing on screen and addition of two numbers can be checked on Python by exciting the following script:

$ python3.12

>>> print(“Hello world”)

>>> print(5+2)

Method 2: How to Install Python by Using The Official Source Code?

In this section, Python will be installed by using the official source code as shown in the following steps:

Step 1: Install Dependencies

The supporting built dependencies can be installed with “apt” by the following command:

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

Press “Y” at the prompt to continue with the installation.

The dependencies are installed. Now we will continue with the installation process.

Step 2: Download Python 3.12 Setup

The latest version of Python can be downloaded from ​​Python’s Official FTP Server by wget tool:

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

From the above image, it can be seen that the compressed archive “Python-3.12.0rc1.tgz” is downloaded in the “Home” Directory. We can view the compressed file by executing the following “ls” command:

$ ls

Step 3: Extract Python 3.12 Compressed Archive

Next, the compressed archive will be extracted via the tar command:

$ tar -xf Python-3.12.0rc1.tgz

From the below image, it can be seen that the compressed archive Python-3.12.0rc1.tgz has successfully extracted to the Python-3.12.0rc1 directory.

$ ls

Step 4: Run Configuration Script

Navigate inside the “Python-3.12.0rc1” directory by using the “cd” command and run the configuration script:

$ cd python-3.12.0rc1
$ ./configure --enable-optimizations

The configuration script will run for a few minutes.

Step 5: Build Python

The following command is used to build Python:

$ sudo make altinstall

Upon doing so, the Installation process takes a few minutes to complete.

Note: “Altinstall” is used instead of the install command. This is because not to overwrite the default Python 3.

Step 6: Verification of Installation

To verify Python installation, run the following command:

Bonus Tip: Uninstall Python

Python 3.12 can be removed by executing the following command:

$ sudo apt autoremove python3.12

Conclusion

Python can be installed on Linux/Ubuntu using two methods: “PPA” and “wget”. To install Python using PPA, first, execute the “sudo add-apt-repository ppa:deadsnakes/ppa” command, and then run the “sudo apt install python3.12” command. Alternatively, users can run the “wget” command to download the latest Python version. Once the compressed archive is downloaded, extract the downloaded compressed archive and finally build the package.

Similar Posts