How to Install Python on Ubuntu 24.04?

Ubuntu 24.04 is a favorite for many programmers because it works well with Python. You can install or update Python in different ways on Ubuntu. Python is a powerful programming language utilized in many areas, like building websites, analyzing data, and more. It’s good for science, data work, AI, and automating tasks. 

In addition, Python works smoothly with Ubuntu, which gives a reliable and safe setup for making software, so lots of experts and beginners like to use it.

This guide will explore all possible methods to install Python on Ubuntu 24.04, ensuring you can get started with Python development.

How to Install Python on Ubuntu 24.04?

Installing Python on Ubuntu 24.04 achieves this in different methods, each with its steps and considerations. 

Checking for Pre-installed Python

Before installing Python, it’s important to check if it’s pre-installed on the Ubuntu system. You can do this by opening the terminal and typing the below command:

python3
Install Python on Ubuntu 24.04 a

If you see a version number, Python is installed; if not, you’ll need to install it.

Method 1: Installing Python Using the Default Package Manager (APT)

The simplest way to install Python is through the default package manager of Ubuntu. This method is straightforward but may not provide the latest version of Python.

To install Python using APT on Ubuntu, follow these steps:

Step 1: Update Package List 

Open the terminal and update the package list to ensure you have the latest/most stable version of the repository: 

sudo apt update
Install Python on Ubuntu 24.04 b

Step 2: Install Python 

Then, install Python by utilizing the apt command by mentioning the “python3” package name:

sudo apt install python3
Install Python on Ubuntu 24.04 c

Step 3: Verify Python Installation

After installation, users can check the Python version by executing the “python3” command with the “–version” utility:

python3 --version
Install Python on Ubuntu 24.04 d

Method 2: Installing Python Using Source Code

For those who require the most recent version of Python, installing from the source code is a good choice. This process involves a few more steps but ensures you have the most up-to-date version:

Step 1: Prepare Your System

Start by refreshing the package list to make sure all existing packages are up to date with the command:

sudo apt update

Step 2: Install Dependencies

Before downloading the Python source code, install the necessary dependencies to compile Python:

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev
Install Python on Ubuntu 24.04 e

Step 3: Download the Python Source Code

Navigate to the Python website and download the latest/most recent source code tarball. Alternatively, you can use “wget” to download it directly in the terminal:

cd /tmp
wget https://www.python.org/ftp/python/3.12.2/Python-3.12.2.tgz
Install Python on Ubuntu 24.04 f

Step 4: Extract the Source Code

Once the download is done, users can extract the tarball using the “tar” command:

tar -xf Python-3.12.3.tgz
Install Python on Ubuntu 24.04 g

Note: Replace <3.12.3> with the actual version number of the downloaded Python source.

Step 5: Compile the Source Code

Change to the folder having the extracted source file and execute the configure script. The below flag optimizes the Python binary by executing several tests:

cd Python-3.12.2./configure --enable-optimizations
Install Python on Ubuntu 24.04 h

Step 6: Install Python

Finally, install Python by compiling the source code via the “make” command:

sudo make install
Install Python on Ubuntu 24.04 i

Step 7: Verify the Python Installation

Finally, users can check the Python version to authenticate the installation:

python3.12 --version
Install Python on Ubuntu 24.04 j

Again, replace “<3.12>” with the version number you installed.

This process can be particularly useful for developers who need the latest features and fixes or those who require a specific configuration for their Python environment.

Method 3: Installing Python Using Deadsnakes PPA

Another method is possible to install a specific version of Python via the Deadsnakes PPA. This repository provides the most recent releases of Python not given in Ubuntu repositories:

Step 1: Check Existing Python Installation

Open your terminal and type “python3”. If the terminal returns a version number, Python is installed; if not, you’ll see an error message indicating Python is not present.

Step 2: Add the Deadsnakes PPA 

Deadsnakes PPA offers the most recent/ latest versions of Python. To add the Deadsnakes PPA to the system, run the below commands:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
Install Python on Ubuntu 24.04 k

Step 3: Install Python

Finally, install the specific/desired version of Python. For instance, install Python 3.13 via the apt command:

sudo apt install python3.13
Install Python on Ubuntu 24.04 l

After the installation, verify it by checking the Python version with 

python3.13 --version
Install Python on Ubuntu 24.04 m

If you already have Python installed and wish to upgrade to a newer version, you can do so by following the instructions provided by the Deadsnakes PPA or by compiling from the source code, as mentioned above.

How To Run Python Script In Ubuntu 24.04?

Running Python scripts efficiently on Ubuntu can significantly streamline your development workflow. 

Step 1: Ensure Python is Installed

Ubuntu 24.04 has pre-installed Python on the operating system. To verify it, type “version” utility:

python3 --version
Install Python on Ubuntu 24.04 n

Step 2: Write Python Script

Now, utilize the text editor for writing the Python script. Save it with a “.py” extension. To do so, create a  Python script “ilf_script.py” in the Nano editor as below:

sudo nano ilf_script.py
Install Python on Ubuntu 24.04 o

Step 3: Run the Python Script

You can run your script by typing in the terminal:

python3 ilf_script.py
Install Python on Ubuntu 24.04 p

If your script is not executable and includes the shebang line (‘#!/usr/bin/env python3’), you can run it directly by typing “./script_name.py”. Also, give execution permissions to your script with:

chmod +x ilf_script.py
./ilf_script.py
Install Python on Ubuntu 24.04 q

This is how you can write as well as run Python script on Ubuntu 24.04.

How to Uninstall/Remove Python from Ubuntu 24.04?

there may be scenarios where you need to uninstall Python from your system. Whether it’s to reinstall a different version or to resolve conflicts, removing Python should be done with caution to avoid system instability.

Let’s  uninstall Python from Ubuntu 24.04:

Uninstall Python Using APT

APT is the standard for uninstalling on Ubuntu. To remove Python, execute the following commands:

sudo apt remove python<version>
sudo apt autoremove         # Remove any Unused Dependencies
Install Python on Ubuntu 24.04 r

Purge Configuration Files

After the uninstallation of Python, several files may remain. To remove these, use the command:

sudo apt purge python<version>

Uninstall Python Using Source Code

If there’s no uninstall option, users need to manually remove the files. For this, use the “rm” to remove the directory as well as all in it. For instance, remove Python installed in “/tmp/python3.13”: 

sudo rm -rf /tmp/python3.13
Install Python on Ubuntu 24.04 s

Be very careful with this command, as it’s irreversible.

Uninstall Python Using the PPA Repository

If you’ve installed Python using a PPA, you’ll need to follow specific steps to remove it safely from your system.

sudo add-apt-repository --remove ppa:deadsnakes/ppa
Install Python on Ubuntu 24.04 t

After removing the PPA, you can uninstall Python with the “apt” command:

sudo apt remove python<version>

Note: Always ensure that you’re removing the correct version and that it won’t disrupt your system’s operations.

FAQ

Some of the most asked questions regarding installing Python on Ubuntu, here are some of these answers:

Set Up a Python Virtual Environment on Ubuntu 24.04

First, ensure that Python is pre-installed on the Ubuntu system. Next, install “pip” using “sudo apt install python3-pip -y”, followed by the installation of the “venv” package with “sudo apt install python3-venv -y”. Finally, create a virtual environment in the desired folder via “python3 -m venv mytest_env” and activate it with the “source mytest_env/bin/activate” command.

Switch Python on Ubuntu

The “update-alternatives” command is an efficient tool for switching/managing several Python versions. It allows users to pick the Python version that they wish to use by default.

Set the Python-specific version as the Default

For setting Python as the default, use the command “sudo update-alternatives –set python /usr/bin/python<version>”. 

Upgrade a Python Latest Version

For the latest version, one may add the Deadsnakes PPA to the system’s software sources, update the package list, and install the desired Python version by following Method 3

Conclusion

To install Python on Ubuntu 24.04, use the default package manager APT. First, update the package list with “sudo apt update”. Then, install Python by using the “sudo apt install python3”. After the installation, verify its version with “python3 –version”. If users need a particular version of Python that is not available in the default repository, consider using the Deadsnakes PPA or compiling Python from the source code.

Ubuntu provides the flexibility to work with Python in a way that suits your needs, whether you prefer the simplicity of APT, the control of compiling from source, or the specificity of a PPA.