Almost every Linux distribution comes with a version of Python included in the default system packages. But on occasion, due to some reasons, you might not find Python installed on your Ubuntu system.

Let's take a closer look at how you can install Python on Ubuntu, with a brief guide on updating the Python package as well.

How to Check if Python Is Installed on Ubuntu

Python is a powerful, high-level scripting language used by many developers around the globe. It is ideal for a variety of real-world applications including web development, web scraping, and penetration testing. You can even build a Telegram bot using Python.

To check if Python is installed on your system, open up a terminal by pressing Ctrl + Alt + T. Type in "python3" and press Enter.

If you see the following output, then you have Python installed on your Ubuntu machine:

        Python 3.12.0b3 (main, June 29 2023, 17:44:14) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

This output provides information on the version of Python installed on your system along with the current date and time.

On the other hand, if you see an error that states "bash: python3: command not found," then sadly you don't have Python installed.

You can also check the Python version by typing the following command in your terminal:

        python3 --version
    

The output will give you details on which version of Python is currently installed on your computer.

How to Install Python 3.12 on Ubuntu

Installing Python on Ubuntu is easy. You can get the latest version of Python from multiple sources. Here are some of the recommended ways:

1. Install Python Using APT

APT, or Advanced Package Tool is the default package manager on Ubuntu and other Debian-based distros. You can download the Python package from the official Ubuntu repository. Here's how to do it:

  1. Open up your terminal by pressing Ctrl + Alt + T.
  2. Update your system's repository list by entering the following command:
            sudo apt update
        
  3. Download the latest version of Python with:
            sudo apt install python3
        
  4. APT will automatically find the package and install it on your computer.

2. Use Deadsnakes PPA to Install Python 3.12 on Ubuntu

If for some reason, you are unable to download the Python package from the official Ubuntu repositories, you can try adding the Deadsnakes PPA to your system repository list. PPAs or Personal Package Archives are repositories that are specially designed for Ubuntu users.

By default, you can't add PPAs to your system. The "software-properties-common" package provides you with an efficient way to manage and add PPAs on Ubuntu.

  1. Install the above-mentioned package on your system by typing the following command:
            sudo apt install software-properties-common
        
  2. Add the official Deadsnakes PPA to your system's repository list with:
            sudo add-apt-repository ppa:deadsnakes/ppa
        
  3. Update your system's package list:
            sudo apt update
        
  4. Download the latest version of Python from the added PPA:
            sudo apt install python3.12
        

Since the Deadsnakes PPA has almost every version of Python in its database, you can install older versions of Python as well. Just replace the package name with the version of Python you want to install on your computer.

        sudo apt install python3.3
sudo apt install python3.8
sudo apt install python3.10

3. Install Python 3 on Ubuntu From Source

You can also download and build the latest version of Python from the official Python website. Although compiling the source code might seem a bit daunting at first, it'll become easier once you know the process.

  1. Update your system's local repository list by running:
            sudo apt update
        
  2. Install supporting dependencies on your system with APT:
            sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
        
  3. Make a new directory to store the Python source files:
            mkdir ./python && cd ./python
        
  4. Download the Python source code from the official FTP server:
            wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0b3.tgz
        
  5. Extract the TGZ file that you just downloaded with:
            tar -xvf Python-3.12.0b3.tgz
        
  6. You need to perform tests and optimizations before installing Python. This is important as it increases the execution speed of your code by at least 10 percent:
            cd Python-3.12.0b3
    ./configure --enable-optimizations
  7. Build the package using the MakeFile present in the directory:
            sudo make install
        

After you've implemented these steps, check if Python was installed properly on your computer by typing python3 --version in the terminal.

Note that Python modules are managed through PIP. PIP is a package management system used to download and add libraries from the Python Package Index. Installing Python PIP on your system is important if you want to use additional modules in your next Python project.

Updating Python to the Latest Version

First of all, make sure that you have an outdated version of Python installed on your system. You can do this by entering python3 --version in your terminal. Note down the version details.

You can find out what's the latest version available by searching the internet. A quick Google search on "python latest version number" would suffice. If the two numbers don't match, then you are probably running an outdated version.

Upgrading to Python's latest version is easy with Ubuntu's Advanced Package Tool. If you have installed Python on your system using APT or the Deadsnakes PPA, enter the following command to download the latest Python version:

        sudo apt install python3
    

You can also use the --only-upgrade flag with the command:

        sudo apt --only-upgrade install python3
    

For those who have compiled Python from the source on their own, you can head over to the Python FTP and grab a copy of the latest version. You will have to follow the steps all over again to build the package, though.

Running Python on Ubuntu Is Easy

Python comes preinstalled on almost every Linux system and is available on official distribution repositories as well. If you still don't have Python installed on your computer, you can easily download it using Ubuntu's package manager.

The Python language is used in a variety of different sectors, and its applications are enough to demonstrate how powerful it is. Programming languages have now become extremely important because of the growing demand for developers across industries.