Introduction

Python is a versatile and powerful programming language that has gained immense popularity among developers worldwide. Its simplicity, readability, and extensive library support make it an ideal choice for various applications, from web development to data analysis and machine learning.

This guide explains how to install Python on Ubuntu 24.04.

Prerequisites

Before you start:

Install Python from the Deadsnakes PPA

The Deadsnakes PPA (Personal Package Archive) is a popular third-party repository that offers a wide range of Python versions for Ubuntu. DeadSnakes PPA offers more recent versions of Python, including both stable and pre-release versions.

DeadSnakes PPA offers the following benefits:

  • Newer Python versions.
  • Multiple Python versions.
  • Compatibility with modern tools or libraries.
  • Pre-release or beta versions of Python.

Follow the steps below to add the Deadsnakes PPA repository to your system and install Python.

  1. Add the Deadsnakes PPA to your system.

    CONSOLE
    $ sudo add-apt-repository ppa:deadsnakes/ppa  
    
  2. Update your system's package information index.

    CONSOLE
    $ sudo apt update 
    
  3. Install python3 the default Python version.

    CONSOLE
    $ sudo apt install python3
    
  4. Ensure you have installed python3.

    CONSOLE
    $ python3 --version
    

    Output:

    CONSOLE
    Python 3.12.3
    

Install Specific Python 3.x Version

DeadSnakes PPA allows you to install and manage multiple versions of Python side-by-side. This is particularly useful especially if you want to test your Python code across different Python versions. To install a specific version of Python3, for instance Python 3.8 or Python 3.9, follow the steps below.

  1. Install python3.8. Replace python3.8 with your specific version.

    CONSOLE
    $ sudo apt install python3.8
    
  2. Verify that you've installed the correct Python version.

    CONSOLE
    $ python3.8 --version
    

    To install multiple Python versions repeat the above command with the desired versions.

Install pip

pip is the most popular package manager for Python. It is a command-line tool that allows you to install and manage Python packages from the Python Package Index (PyPI) and other package repositories. You should always install pip after installing Python to match your Python version to pip.

  1. Install pip for python3.

    CONSOLE
    $ sudo apt install python3-pip
    
  2. Replace 3.8 if you installed a specific Python 3.x version.

    CONSOLE
    $ sudo apt install python3.8-pip
    

Create a Hello, World! Application

A Hello, World! application is a simple program that outputs the text Hello, World! to the screen. It is often the first program that you should write and run as a beginner to verify that Python works. Now that you've installed Python create and run a simple Hello, World! application.

  1. Create a new hello-world.py file using the nano text editor.

    CONSOLE
    $ nano hello-world.py
    
  2. Add the following content to hello-world.py file.

    Python
    print("Hello, World!")
    
  3. Press Ctrl + X, then Y to save and close the file.

  4. Run the file using Python.

    CONSOLE
    $ python3 hello-world.py
    

    If you installed a specific Python 3.x version, replace python3 with python3.x. For instance python3.8.

    CONSOLE
    $ python3.8 hello-world.py
    

    Output:

    CONSOLE
    Hello, World!
    

    The above output confirms that Python is working as expected.

Conclusion

In this guide, you've learned how to install Python on Ubuntu 24.04 using the Deadsnakes PPA, including installing specific Python 3.x versions, setting up pip and running a simple Hello, World! application. After following these steps, you have now set up a Python environment on your Ubuntu system. You can now start developing Python applications.