Devbyte tutorials cover

How to Install Python 3 on Ubuntu 22.04: A Quick and Easy Guide

Python is a powerful and versatile programming language used by developers worldwide. Ubuntu 22.04, a popular Linux distribution, comes with Python 3 pre-installed. Checking the installed Python version is as simple as typing “python3 –version” in the terminal.

For those needing a specific or newer version of Python, installing it on Ubuntu 22.04 is straightforward. The process involves adding a repository, updating the package list, and using the apt package manager. This allows users to have multiple Python versions on their system, catering to different project requirements.

Keeping Python up-to-date ensures access to the latest features and security patches. Ubuntu’s package management system makes it easy to maintain and upgrade Python installations. Whether for web development, data analysis, or machine learning, having the right Python version is crucial for a smooth coding experience on Ubuntu 22.04.

Preparing the System

Before installing Python 3 on Ubuntu 22.04, you need to set up your system. This involves updating package lists, installing necessary dependencies, and adding the Python PPA.

Updating Package Lists

To start, open a terminal window. Run the following command to update the package lists:

sudo apt update

This ensures you have the latest information about available packages. It’s a good practice to run this command before installing any new software.

Next, upgrade existing packages to their latest versions:

sudo apt upgrade -y

The “-y” flag automatically answers yes to any prompts during the upgrade process.

Installing Dependencies

Some dependencies are needed to add new repositories and build Python. Install them with this command:

sudo apt install software-properties-common build-essential -y

The software-properties-common package allows you to add PPAs easily. Build-essential includes compilers and libraries necessary for building Python from source.

Adding the Python PPA

To get the latest Python version, add the deadsnakes PPA. This repository provides up-to-date Python releases.

First, add the PPA:

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

The “-y” flag automatically confirms the addition of the repository.

After adding the PPA, update the package lists again:

sudo apt update

This refreshes the package information to include the new PPA.

Installation of Python 3

Ubuntu 22.04 comes with Python 3 pre-installed. Users can choose to install from the repository or compile from source for specific needs. After installation, some important tasks help set up the environment.

Installing from the Repository

The easiest way to install Python 3 on Ubuntu 22.04 is through the package manager. Open a terminal and type:

sudo apt update
sudo apt install python3

This installs the latest Python version available in the Ubuntu repositories. To check the installed version, run:

python3 --version

Users can also install additional Python packages using pip:

sudo apt install python3-pip

Compiling from Source

For more control over the installation, compiling Python from source is an option. First, install required dependencies:

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev libbz2-dev

Download the Python source code from the official website. Extract the archive and navigate to the directory. Run these commands:

./configure --enable-optimizations
make -j $(nproc)
sudo make altinstall

The altinstall option prevents overwriting the default Python binary.

Post-Installation Tasks

After installation, set up a virtual environment for projects:

python3 -m venv myproject
source myproject/bin/activate

This creates an isolated space for Python packages. Install required modules using pip:

pip install package_name

To exit the virtual environment, type deactivate. It’s also helpful to add Python to the system PATH if needed. This allows easy access to Python from any directory.

Managing Python Environments

Python environments help keep projects organized and avoid conflicts between packages. They allow you to work on different projects with different dependencies.

Using the venv Module

The venv module creates isolated Python environments. To set up a new environment, open a terminal and run:

python3 -m venv myenv

This creates a new folder called “myenv” with a fresh Python installation. To activate it:

source myenv/bin/activate

The prompt will change to show the active environment. Now any packages installed will only affect this environment.

To exit the environment, simply type:

deactivate

Handling Python Packages with pip

Pip is Python’s package installer. It makes adding new functionality to projects easy. To install a package:

pip install package_name

To see all installed packages:

pip list

For specific versions:

pip install package_name==1.2.3

To update Python packages:

pip install --upgrade package_name

Pip also allows uninstalling packages:

pip uninstall package_name

Using pip with virtual environments keeps the main Python installation clean and avoids conflicts between projects.

Share this article: