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

Python is a high-level, interactive programming language created by Guido van Rossum and used to write automation and other types of scripts. It's popular among developers for its simplicity and readability, making it accessible to beginners, while being powerful and versatile enough for seasoned developers. Python is used across various fields, including data science, machine learning, artificial intelligence (AI), and web development. Its user-friendly syntax and strong community support contribute to its popularity and effectiveness as a general-purpose programming language.

This guide provides instructions on how to install Python on Ubuntu 22.04 Jammy Jellyfish distribution. You will learn how to set up the Deadsnakes PPA repository on Ubuntu and create a local Python virtual environment.

Prerequisites

Before you begin how to install Python 3 on Ubuntu, ensure you have the following:

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

How to Install Python on Ubuntu 22.04?

There are multiple ways to install Python on Ubuntu:

  1. Install Python using Apt Repository
  2. Install Python3 on Ubuntu by adding the deadsnakes PPA repository
  3. Install Python from the official Python website via source code

Method 01: Install Python using Apt Repository

This method uses the APT package manager and the default Ubuntu repositories to install Python. It's the simplest method, but it might not always provide the most recent version of Python. For the latest versions, you can check the official Python website or use a third-party repository. 

Perform the following steps to install Python on Ubuntu 22.04:

Step 1: Update Apt Repositories

First, update the package lists to ensure you're getting the latest version available. Run the following command:

$ sudo apt update

Step 2: Install Python3 on Ubuntu

Once the package lists are updated, install Python by running:

$ sudo apt install python3

Step 3: Verify the Installation

Check that Python was installed correctly by verifying the version:

$ python3 --version

The above command will display the installed Python version on the terminal.

Method 02: Install Python3 on Ubuntu via Deadsnakes PPA

Software packages that are not available in the default Ubuntu apt repository. You can install these packages using third-party repositories. A Personal Package Archive (PPA) is a third-party repository for Ubuntu that provides access to newer software versions not available in the default repositories.

The Deadsnakes PPA offers Python versions 3.7, 3.9, and 3.11 for Ubuntu 22.04. Additionally, Python 3.10 is available through the official Ubuntu repositories and is preinstalled by default.

To install Python3 on Ubuntu using the deadsnakes PPA repository, follow the below steps:

Step 1: Update Package Lists

Open a terminal and run the following command to update your package lists:

$ sudo apt update

Step 2: Install Required Software

To manage PPAs more effectively, you'll need the software-properties-common package. Install it with:

$ sudo apt install software-properties-common

Step 3: Add the Deadsnakes PPA

The Deadsnakes PPA offers newer versions of Python than those included in the default Ubuntu repositories. Add it by running:

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

After adding the PPA, update the package lists again:

$ sudo apt update

Step 4: Install Python 3 on Ubuntu

The Deadsnakes PPA includes multiple Python versions. To install a specific version, use the appropriate package name. For instance, to install Python 3.12, run:

$sudo apt install python3.12

Confirm the installation by pressing 'y' when prompted and wait for the process to complete. Check that Python was installed correctly by running:

$ python3 --version

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

This method involves downloading and compiling Python from the official source code. It’s a more complex process but allows you to install the latest version of Python. Follow the below steps:

Step 1: Install Required Packages

First, start by updating your local package lists:

$ sudo apt update

Compiling Python from a source requires additional software. Install the necessary packages with:

$ 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

Change to the /tmp directory, which is commonly used for temporary files:

$ cd /tmp

Visit the official Python website and find the version you want. 

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. For example:

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

After executing the above command, the provided version is Python3.12.1 will download in your system’s home directory.

Step 3: Extract the Downloaded File

Extract the tarball with:

$ tar -xf Python-3.12.1.tgz

Replace Python-3.12.1.tgz with the file name you downloaded if different.

Step 4: Prepare Python for Installation

Navigate to the extracted directory:

$ cd Python-3.12.1

Prepare the build system with:

./configure --enable-optimizations

The --enable-optimizations flag improves performance by 10-20%. This process may take some time.

Step 5: Build and Install Python

Compile and install Python with:

$ sudo make install

If you want to keep the existing Python installation and install this version as an alternative, use:

$ sudo make altinstall

Allow the installation process to complete. Check that Python was installed correctly by running:

$ python3 --version

How to Set Up a Python3 Virtual Environment? 

A virtual environment provides an isolated space for running Python projects, ensuring that each project operates independently of its dependencies. This isolation helps manage different project requirements more effectively.

To set up a virtual environment, follow these steps:

1. Install the python3-venv Module

First, install the python3-venv module, which is needed to create virtual environments:

$ sudo apt install -y python3-venv

2. Create a New Directory for the Environment

Choose a directory where you want to set up your virtual environment. Create a new directory and navigate into it:

$ mkdir my_project_env && cd my_project_env

3. Create a Virtual Environment

Create the virtual environment in this directory. Here, we will name the environment my_env:

$ python3 -m venv my_env

4. Check the contents of the virtual environment by listing its directory:

$ ls my_env

5. Activate the Virtual Environment 

To activate the virtual environment, use the following command:

$ source my_env/bin/activate

Once activated, your command prompt will be prefixed with the environment’s name, indicating that you are working within the virtual environment. For instance, you will see (my_env) at the beginning of the prompt.

While activated, any Python applications or libraries you use will be confined to this environment, allowing you to manage dependencies separately from other projects.

Conclusion

In this guide, we demonstrated different methods for how to install Python on Ubuntu 22.04. Additionally, we learned how to create a virtual Python environment to run your Python programs separately from other projects, ensuring isolation and better management of dependencies.

Are you searching for top-notch hosting services? BlueVPS.com offers a range of services designed to enhance the performance and availability of your cloud infrastructure. Their solutions include simple, scalable, and customizable virtual private servers (VPS) that provide a dedicated environment tailored to your needs. 

With BlueVPS, you benefit from unlimited traffic, ensuring that your applications can handle high volumes of data and users without constraints. Their services are crafted to deliver reliable performance and flexibility, making them a robust choice for managing your cloud resources effectively.

Blog