How to Install Python on Ubuntu 22.04

Introduction

Python is a flexible programming language that is frequently used for a wide range of purposes, including data analysis, web development, and scientific computing. It is frequently regarded as one of the greatest programming languages for beginners, but it is also commonly used by experienced developers due to its extensive library and framework support. Python is a key scripting language for Ubuntu and many other Linux distributions, making it a driving force behind numerous operations on Ubuntu.

How to Check if Python is Installed?

Python and Python3 are different versions of the Python programming language on Ubuntu. The code written in Python3 is not compatible with Python2 and vice versa. Hence, before using Python, make sure to check the installed version:

python

The output indicates that the machine does not have python installed.

How to Check if Python3 is Installed?

Python3 is preinstalled in Ubuntu 22.04 by default. Run the following command to verify the python3 version that is installed:

python3

How to Install python-is-python3?

The python-is-python3 package creates a symbolic link to Python3 in order to point Python to Python3. The following command can be used to install this utility from Ubuntu's default repository:

sudo apt install python-is-python3

The python command will display Python3 (as per the interpreter /usr/bin/python3) after the python-is-python3 package has been installed:

python

The output indicates that Python is now running at version “Python 3.10.6”.

How to Uninstall Python From Ubuntu 22.04?

The default version of Python is used by many applications and system packages. If you delete the default Python, the packages and system utilities connected with Python will stop working, and the system will crash.

The following are some of the significant utilities that will be removed with the default Python version:

sudo apt remove python3

Take a look at the screenshots above. The program will delete a number of system utilities, including gdm3 (GNOME Display Manager), Ubuntu Desktop, Ubuntu-minimal, and other GNOME-based packages.

How to Reinstall Python on Ubuntu 22.04 if it was Uninstalled?

As was already said, it is harmful to delete the default Python installation in Ubuntu 22.04. But, if you unintentionally removed it, you can restore it by using the instructions in this section.

After uninstalling Python, you will see the tty1 interface as seen below:

Now, this tty1 support will be used for all the fixing. Use the shortcuts CTRL+ALT+F1 or CTRL+ALT+F3 to call up the tty support if you are unable to get it.

Let us now proceed to the steps.

Step 1 – Fix the Internet Issue

Before proceeding, enable internet connectivity on your Ubuntu system by running the following command, which allows the client to connect to the DHCP server:

sudo dhclient -r

Finally, use the following command to configure the network using the DHCP server:

sudo dhclient

Step 2 – Fix the Missing Dependencies

Use the following command to find and install the missing dependencies right away:

sudo apt-get install -f

Step 3: Install “ubuntu-Desktop”

After that, run the command below to install the Ubuntu-Desktop package so that your Ubuntu 22.04 will support both the GUI and the CLI:

sudo apt install ubuntu-desktop

Step 4: Verify the Default Python

To check if the default Python3 is available, log in to the machine and issue the command below:

python3 --version

The output demonstrates that Python is restored after installing Ubuntu-Desktop.

To map Python3 on Python, it is advised to reinstall the package python-is-python3:

sudo apt install python-is-python3

Verify the Python version now:

python --version

The current version of Python is the same as Python3.

👉
Because so many utilities depend on default Python, it is not advisable to uninstall it.

How to Install a Different Python Version From Source?

Python source packages are available for download from the Python website. Python prerelease is included in these source files, as well as the most recent stable version. In this section, we will go over how to install a specific Python version from source files.

Prerequisite Step: Install Dependencies

Before continuing with the installation of Python, it is advised that the following dependencies be installed:

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

Step 1: Download the Source Files

To begin, download the source files for the version you wish to install, as we did here for Python3.11.1:

wget https://www.python.org/ftp/python/3.11.1/Python-3.11.1.tgz
👉
Click the link to select the Python version.

Step 2: Extract the Python Source Files

To extract the source files, use the -xvf flag of the tar command and navigate to the directory where they were downloaded (in our instance, that is the current directory):

tar -xvf Python-3.11.1.tgz

Step 3: Configure Python

Move to the extracted files directory and then execute the configure script with the -enable-optimizations option for the optimum Python environment performance:

cd Python-3.11.1
./configure --enable-optimizations

Step 4: Compile the Source Files

To create an executable (binaries that the computer can recognize) for the Python package, compile the source files using the "make" utility:

make

Step 5: Install Python

The following command can be used to install Python from the binaries once the files have been compiled:

sudo make altinstall

Run the next command to confirm the installation after a successful execution:

python3.11 --version

The output indicates that Python 3.11.1 has been installed.

FAQs to Install Python on Ubuntu 22.04

What version of Python is available for Ubuntu 22.04?

Ubuntu 22.04 comes with Python 3.10 pre-installed, but you can also install Python 2.7 or other versions of Python using the package manager.

How do I check if Python is installed on Ubuntu 22.04?

You can check if Python is installed on Ubuntu 22.04 by running the command python3 -V for Python 3.10 or python -V for Python 2.7 in the terminal. If Python is installed, it will display the version number.

Can I install multiple versions of Python on Ubuntu 22.04?

Yes, you can install multiple versions of Python on Ubuntu 22.04, but you will need to manage them using virtual environments.

What is a virtual environment in Python and how do I create one on Ubuntu 22.04?

A virtual environment in Python is a self-contained environment that allows you to install and manage different versions of Python and their dependencies. To create a virtual environment on Ubuntu 22.04, you can use the "venv" module, which is included with Python 3.10.

Can I use Python for web development on Ubuntu 22.04?

Yes, you can use Python for web development on Ubuntu 22.04. Popular web frameworks for Python include Django, Flask, and Pyramid.

Conclusion

You can use the apt package manager and compile/build the source files to install Python on Ubuntu 22.04. Run the sudo apt install python<version>> command using APT. While the source files are downloaded from the official website and compiled. In this tutorial, we have covered how to install Python on Ubuntu 22.04 and how to restore Ubuntu 22.04 if the default Python gets accidentally removed.

If you have any suggestions or queries, kindly leave them in the comments section.