How to Install Python on Ubuntu 22.04? (A Complete Step-by-Step Guide)

Posted on

How to Install Python on Ubuntu 22.04? (A Complete Step-by-Step Guide)

Python, created by Guido van Rossum, is a versatile and interactive high-level programming language. Its simple syntax and readability make it popular for automation and other scripts. It is favored by both beginners and experienced developers due to its power and ease of use in fields like data science, machine learning, AI, and web development. Python’s strong community support further enhances its effectiveness as a general-purpose language.

This comprehensive guide provides step-by-step instructions for installing Python on Ubuntu 22.04 Jammy Jellyfish. You’ll learn how to set up the Deadsnakes PPA repository and create a local Python virtual environment.

Prerequisites

Before you begin, ensure you have the following:

  • A running Ubuntu 22.04 server.
  • SSH access to the server, either as root or a user with sudo privileges.

How to Install Python on Ubuntu 22.04?

There are several ways to install Python on Ubuntu:

  1. Using the Apt Repository
  2. Adding the Deadsnakes PPA repository
  3. Building from source code downloaded from the official Python website

Method 01: Install Python using Apt Repository

This is the simplest method, using the APT package manager and Ubuntu’s default repositories. It may not always provide the newest version of Python. Check the official Python website or use a third-party repository for the latest versions.

Follow these steps to install Python on Ubuntu 22.04:

Step 1: Update Apt Repositories

First, update the package lists to get the latest available versions:

$ sudo apt update

Step 2: Install Python3 on Ubuntu

Install Python with the following command:

$ sudo apt install python3

Step 3: Verify the Installation

Verify the installation by checking the Python version:

$ python3 --version

This command will display the installed Python version.

Method 02: Install Python3 on Ubuntu via Deadsnakes PPA

For software not in the default repositories, use third-party repositories. A Personal Package Archive (PPA) offers newer software versions than the defaults.

The Deadsnakes PPA provides Python versions 3.7, 3.9, and 3.11 for Ubuntu 22.04. Python 3.10 is also available and pre-installed by default from Ubuntu’s official repositories.

Follow these steps to install Python3 using the Deadsnakes PPA:

Step 1: Update Package Lists

Update your package lists:

$ sudo apt update

Step 2: Install Required Software

Install the software-properties-common package to manage PPAs effectively:

$ sudo apt install software-properties-common

Step 3: Add the Deadsnakes PPA

Add the Deadsnakes PPA to access newer Python versions:

$ sudo add-apt-repository ppa:deadsnakes/ppa

Update the package lists again:

$ sudo apt update

Step 4: Install Python 3 on Ubuntu

Install a specific Python version using its package name. For example, to install Python 3.12:

$sudo apt install python3.12

Confirm the installation when prompted and wait for it to complete. Verify the installation by checking the version:

$ python3 --version

Method 03: Install Python from the Official Python Website via Source Code

This more complex method involves downloading and compiling Python from source, allowing you to install the latest version. Follow these steps:

Step 1: Install Required Packages

Update your local package lists:

$ sudo apt update

Install the necessary packages for compiling Python:

$ sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget

Step 2: Download Python Source Code

Navigate to the /tmp directory for temporary files:

$ cd /tmp

Go to the official Python website and find the version you want to download.

how to install python on ubuntu 22.04? (a complete step-by-step guide)

source: https://www.python.org/downloads/source/

Download the source code using wget:

$ wget https://www.python.org/ftp/python/3.12.1/Python-3.12.1.tgz

This example downloads Python3.12.1 to your system’s home directory.

Step 3: Extract the Downloaded File

Extract the tarball:

$ tar -xf Python-3.12.1.tgz

Adjust the filename if necessary.

Step 4: Prepare Python for Installation

Navigate to the extracted directory:

$ cd Python-3.12.1

Prepare the build system:

./configure --enable-optimizations

The –enable-optimizations flag can improve performance by 10-20%. This step may take some time.

Step 5: Build and Install Python

Compile and install Python:

$ sudo make install

To install this version alongside an existing Python installation, use:

$ sudo make altinstall

Allow the installation to complete. Verify the installation by checking the version:

$ python3 --version

How to Set Up a Python3 Virtual Environment? 

A virtual environment creates an isolated space for Python projects, ensuring dependency isolation and better project management.

Share this: