How to Install Python on Ubuntu?

This guide provides a straightforward, step-by-step walkthrough for installing the Python programming language on an Ubuntu Linux system. You will learn how to update your system packages, install Python using the Advanced Package Tool (APT), verify the installation, and set up a programming environment. Whether you are using the default version included with your Ubuntu release or need a specific newer version, these instructions will ensure Python is correctly configured and ready for your development projects.

Step 1: Update Your System Packages

Before installing any new software, it is best practice to update your local package index. This ensures you are installing the latest available versions and patches from the Ubuntu repositories.

Open your terminal and execute the following command:

sudo apt update && sudo apt upgrade -y

Step 2: Check for Pre-installed Python

Ubuntu usually comes with Python 3 pre-installed. You can check if it is already available on your system, and see which version you have, by running:

python3 --version

If Python is installed, the terminal will return the version number (for example, Python 3.12.x). If it is not installed, or if you need to reinstall it, proceed to the next step.

Step 3: Install Python via APT

The easiest way to install Python on Ubuntu is through the default APT package manager. This installs a stable version tested specifically for your distribution.

Run the following command to install Python 3:

sudo apt install python3 -y

Step 4: Install Pip and Essential Development Tools

To manage software packages and libraries in Python, you will need pip, Python’s package installer. It is also highly recommended to install the build-essential package and other common dependencies for Python development.

Install them by running:

sudo apt install python3-pip python3-dev build-essential -y

You can verify that pip was installed successfully by checking its version:

pip3 --version

Step 5: Verify the Full Installation

To ensure everything is working correctly, you can open the interactive Python shell. Type the following command into your terminal:

python3

This will open the Python prompt (indicated by >>>). You can test it by running a simple print statement:

print("Python is working!")

To exit the interactive environment and return to your regular terminal, type exit() and press Enter, or press Ctrl + D.