How to Install Python in Ubuntu 24.04


Python is one of the mainly used programming languages with clear syntax. Using Python, you can write simple scripts and can create complex programs that do different operations. In Ubuntu Python comes preinstalled, but if you have deleted or want to reinstall Python on your system follow this article.

Table of Contents:

1. Check Python Installation

Before proceeding to Python installation in Ubuntu 24.04, the first step is to check if Python is already there or not.

To check the Python installation, run this command:

python3

If you see the output displaying the Python version, it means Python is already installed on your system. If the system throws an error like the python3: command not found, it means your Python is not installed on your system.

2. Installing Python on Ubuntu 24.04

For Python installation, you can use three different methods. You can either use the default package manager or download and install Python from its source code. Sometimes the Python installed using the package manager is not the updated one, so we have to rely on third-party PPA repositories to get the updated version of Python. One such PPA repository is the Deadsnakes repository.

Let’s cover these three methods for Python installation in detail.

2.1. Installing Python Using Default Package Manager (APT)

Installing Python using the default APT package manager is the easiest way. But it might not always give you the latest version of Python. For the most up-to-date version, consider checking the official site or downloading it using third-party PPA repositories.

Follow the given steps to install Python on Ubuntu 24.04 using the default repository.

Step 1: Open the console and update the package list of your system. To do this, run the below command. This will fetch information about available packages and make sure that all the packages are up-to-date:

sudo apt update

Step 2: After updating the system packages, simply run this to get Python3 on your system:

sudo apt install python3

Step 3: Once the Python is installed, verify its installation using this command:

python3 --version

2.2. Installing Python Using Source Code

To get the newest Python version, you can directly download it from the official website by compiling its source code in Ubuntu 24.04. This might look difficult at starting, but the tradeoff will give you the newer Python version.

Step 1: Before downloading the source code file, first update your system:

sudo apt update

Step 2: To compile the package from its source code, you will need to install some supporting dependencies on your Ubuntu 24.04:

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

Step 3: Now use cd to navigate to the temporary directory. This directory is used as a pit stop for installing the packages from the source code. You can easily clear the leftover files from this directory once Python is installed on your system:

cd /tmp

Note: You can also create a new directory for your Python using the mkdir command.

Step 4: Now open the Python official source code download page and select the version you want to download:

Now scroll down to the files menu and copy the link address of the Gzipped source tarball:

Step 5: You can either directly download the tar file or use the wget command to get the tarball source code file using directly through the terminal:

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

Step 6: After downloading the source code file, extract it using the tar command:

tar -xf Python-3.12.2.tgz

Note: If you are downloading any other version of Python, replace its version number in the above command.

Step 7: Before compiling, configure the build environment with the following command:

cd Python-3.12.2

./configure --enable-optimizations

The –enable-optimizations flag will optimize the Python binary. It does this after multiple test runs.

Step 8: Compile the Python source code using the following command:

sudo make install

Step 9: Confirm that Python 3.12 is installed by checking its version:

python3 --version

You should see an output indicating Python 3.12.x, confirming the successful installation.

2.3. Installing Python Using Deadsnakes PPA

Another way to install the latest Python is using PPA. It is a third-party repository known as the Personal Package Archive. It provides a convenient method to access and install the recent version of programs that are by default not available in Ubuntu’s default repository.

Step 1: Open the terminal and update system packages first:

sudo apt update

Step 2: Next install the software properties common package. It lets you manage and install new repositories in your system. It provides an abstraction layer for managing the repositories (or software sources) that your system uses.

To install it, run this command:

sudo apt install software-properties-common

Step 3: Now add the deadsnakes ppa to Ubuntu 24.04:

sudo add-apt-repository ppa:deadsnakes/ppa

Step 4: One last time, update the system packages:

sudo apt update

Step 5: Finally install the latest version of Python using this command:

sudo apt install python3.12

Note: Deadsnakes repository has many Python versions available, including the older one. You just have to specify the needed version in the above command.

Step 6: To verify the Python installation, run this command:

python3 --version

3. Upgrade Installed Python Version

If you have already installed Python but only want to upgrade it to the latest version, then first you have to check your current version. It must be an older one.

After that, if you have previously installed Python using the APT or dead snake repository, run this command to get the latest version of Python:

sudo apt install python3

On the other hand, if you have installed Python using the source code, then open the Python download page and get the link to the latest Python Gzipped source tarball file. After that, install it using the above-explained steps.

Conclusion

Python is one of the most used simple programming languages. It is easy to write and understand. To install Python in Ubuntu 24.04 use the apt install or directly download its source code from the official site. However, if you need the latest versions of Python, you can install it using a third-party PPA repository.

Print Friendly, PDF & Email
Categories