How to Install Python on Ubuntu?

How to Install Python on Ubuntu?

Python is one of the most widely used programming languages, known for its simplicity and flexibility.

Python 3 is the most recent version, offering improvements in performance, security updates, and new features that make it more compatible with modern libraries and tools. 

If you want to install Python on Ubuntu, whether it’s Ubuntu 20.04 or 22.04, this manual will show you how to do it efficiently.

Prerequisites for Installation

Prerequisites for Installation

Before you proceed with installing Python 3, ensure that your system meets the following conditions:

  • Your system should be running Ubuntu 20.04 or Ubuntu 22.04.
  • You must have a user account with root privileges.
  • You should have access to the terminal (Ctrl+Alt+T).

Checking if Python is Already Installed on Ubuntu

Checking if Python is Already Installed on Ubuntu

Before installing Python on Ubuntu, it’s a good idea to check if Python 3 is installed on your machine. Open the terminal and type the following command:

bash

Copy code

python3

  • If Python 3 is installed, you’ll see its version information and additional details like copyright and license.
  • If you get an error message like “bash: python3: command not found,” it means Python is not installed.

Methods to Install Python on Ubuntu

There are several ways you can install Python 3 on Ubuntu. Below are the three main methods to consider:

1. Install Python Using APT

This method is the most straightforward, using the APT package manager to get Python from the default Ubuntu repositories.

However, remember that this might not always provide the latest Python version.

Steps to install Python via APT:

  • Update Package Repositories
    To get the latest available packages, run the following command:
    bash
    Copy code
    sudo apt update
  • Install Python 3
    Once the repositories are updated, run this command to install Python on Ubuntu:
    bash
    Copy code
    sudo apt install python3
  • Verify the Installation
    After the installation completes, check the installed Python version by running:
    bash
    Copy code
    python3 –version

2. Installing Python from Source Code

If you prefer to install the latest Python version, you can download the source code from the official Python website and compile it manually.

This process is more complex but ensures you get the newest version.

Steps to install Python from source:

  • Update Local Repositories
    First, update the package repositories:
    bash
    Copy code
    sudo apt update
  • Install Required Dependencies
    To compile Python from source, you’ll need to install some additional software:
    bash
    Copy code
    sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
  • Download the Python Source Code
    Change to the /tmp directory where you will download the source code:
    bash
    Copy code
    cd /tmp

Visit the official Python website and choose the version you want. Copy the download link and use wget to download it. For example:
bash
Copy code
wget https://www.python.org/ftp/python/3.12.1/Python-3.12.1.tgz

  • Extract the Files
    After downloading, extract the compressed file:
    bash
    Copy code
    tar -xf Python-3.12.1.tgz
  • Configure and Optimize Python
    Enter the extracted directory and configure the build process:
    bash
    Copy code
    cd Python-3.12.1

./configure –enable-optimizations

This step optimizes Python for better performance, speeding up code execution.

  • Install Python
    Now, compile and install Python with:
    bash
    Copy code
    sudo make install

If you want to install a secondary version alongside an existing one, use:
bash
Copy code
sudo make altinstall

  • Check Python Version
    To confirm the installation was successful, run:
    bash
    Copy code
    python3 –version

3. Installing Python from a PPA

If you want to install a more recent version of Python, you can use the Deadsnakes PPA. This method adds a third-party repository to get newer versions of Python.

Steps to install Python from PPA:

  • Update Repositories
    Start by updating the local repository list:
    bash
    Copy code
    sudo apt update
  • Install Software Properties
    Install the software-properties-common package to manage additional repositories:
    bash
    Copy code
    sudo apt install software-properties-common
  • Add the Deadsnakes PPA
    Add the Deadsnakes repository to your system:
    bash
    Copy code
    sudo add-apt-repository ppa:deadsnakes/ppa

After adding the PPA, update your repositories:
bash
Copy code
sudo apt update

  • Install Python 3
    Now you can install Python 3.12 (or any other version available in the PPA):
    bash
    Copy code
    sudo apt install python3.12
  • Verify the Installation
    Check the Python version with:
    bash
    Copy code
    python3 –version

Conclusion

Following one of these methods, you should now have Python 3 installed on your Ubuntu system.

Whether you use APT, compile from source, or leverage a PPA, you can easily install Python on Ubuntu.

After installing, don’t forget to verify the installation to ensure everything went smoothly.

If you haven’t installed PIP yet, consider doing so, as it is essential for managing Python packages.

The post How to Install Python on Ubuntu? appeared first on About Chromebooks.