How to Install Python on Ubuntu 22.04

  • Francis Ndungu

Introduction

Python is a widely used programming language celebrated for its versatility and powerful features. It is particularly popular among developers for its ease of use and extensive libraries that support everything from web development to data analysis and machine learning.

This guide details the steps to install Python on Ubuntu 22.04.

Prerequisites

Before you begin:

  • Deploy a VPS server running Ubuntu 22.04.
  • Set up a non-root sudo user.

Install Python from the Deadsnakes PPA

The Deadsnakes PPA (Personal Package Archive) is a trusted repository that provides multiple versions of Python for Ubuntu. It includes the latest releases, stable versions, and even pre-release versions.

Benefits of the Deadsnakes PPA:

  • Availability of the latest Python versions.
  • Support for multiple Python versions.
  • Compatibility with modern tools and libraries.
  • Access to pre-release or beta versions of Python.

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

  1. Add the Deadsnakes PPA to your system.

    CONSOLE
    $ sudo add-apt-repository ppa:deadsnakes/ppa  
    
  2. Refresh your system's package list.

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

    CONSOLE
    $ sudo apt install python3
    
  4. Verify the new Python version.

    CONSOLE
    $ python3 --version
    

    Output:

    CONSOLE
    Python 3.10.4
    

Install a Specific Python 3.x Version

The Deadsnakes PPA lets you install and manage multiple Python versions concurrently, which is beneficial for testing your code across different versions. Here's how to install a specific Python 3.x version, such as Python 3.8 or Python 3.9.

  1. Install a specific Python version. Replace 3.8 with your desired version.

    CONSOLE
    $ sudo apt install python3.8
    
  2. Confirm the new Python version.

    CONSOLE
    $ python3.8 --version
    

    To install multiple Python versions, repeat the steps with the desired versions.

Installing pip

pip is the go-to package manager for Python, allowing you to install and manage Python packages from the Python Package Index (PyPI) and other repositories. Always install pip after installing Python to ensure compatibility with your Python version.

  1. Install pip for the default Python version.

    CONSOLE
    $ sudo apt install python3-pip
    

    If you installed a specific Python 3.x version, replace 3.8 with your version.

    CONSOLE
    $ sudo apt install python3.8-pip
    

Create a Hello, World! Application

A Hello, World! application is a simple program that prints Hello, World! to the screen. It's often the first program you write to confirm that Python works correctly.

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

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

    Python
    print("Hello, World!")
    
  3. Save and close the file by pressing Ctrl + X, then Y.

  4. Execute the file using Python.

    CONSOLE
    $ python3 hello-world.py
    

    If you installed a specific Python 3.x version, replace python3 with python3.x, such as python3.8.

    CONSOLE
    $ python3.8 hello-world.py
    

    Output:

    CONSOLE
    Hello, World!
    

    This output verifies that Python works as expected.

Conclusion

This guide shows you how to install Python on Ubuntu 22.04 using the Deadsnakes PPA, including instructions for installing specific Python 3.x versions, setting up pip, and running a basic Hello, World! application. After following these steps, you now have a fully functional Python development environment on your Ubuntu system.

  • Databases
  • Webservers
  • PHP
  • API
  • Python
  • VPS Guides
  • Network
  • AI
  • Node.js