| by Arround The Web | No comments

How to Install Python on Ubuntu 22.04?

Python is a versatile programming language widely used for various purposes, i.e., scientific computing, data analysis, and web development. It is often considered one of the best programming languages for beginners, but also widely used by experienced developers due to its vast array of libraries and frameworks. Python is a primary scripting language for Ubuntu and many other Linux distributions, which makes it a catalyst for various operations on Ubuntu.

This article will discuss:

    • How to Check if Python is Installed?
    • How to Check if Python3 is Installed?
    • How to Install python-is-python3?
    • How to Uninstall Python From Ubuntu 22.04?
    • How to Reinstall Python on Ubuntu 22.04 if it was Uninstalled?
    • How to Install a Different Version of Python From Source?

How to Check if Python is Installed?

In Ubuntu, Python, and Python3 refer to different versions of the Python programming language. The code written in Python3 may not work with Python2 and vice versa. Therefore, it is necessary to check the version of installed Python before using it:

$ python

 

The output displays that “python” is not installed in the system.

How to Check if Python3 is Installed?

Python3 comes by default with Ubuntu 22.04. To check the installed version of “python3”, run the following command:

$ python3

 

The output returns that “Python 3.10.6” is currently installed in the system.

How to Install python-is-python3?

The “python-is-python3” package redirects Python to Python3 by creating a symbolic link of Python3 for Python. This utility is available on the default repository of Ubuntu and can be installed using the command:

$ sudo apt install python-is-python3

 

After installing the “python-is-python3” package, the “python” command will show the Python3 (as per the interpreter “/usr/bin/python3”):

$ python

 

The output displays the current version of Python as “Python 3.10.6”.

How to Uninstall Python From Ubuntu 22.04?

Many applications and system packages rely on the default version of Python. If you remove the default Python, the packages and system utilities associated with Python will stop functioning, and the system will crash.

Let us show you the important utilities that will be removed with the default Python version:

$ sudo apt remove python3

 

 

Look at the above screenshots. Various system utilities, such as Ubuntu Desktop, Ubuntu-minimal, gdm3 (GNOME Display Manager), and other GNOME-based packages, will be removed with the command.

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

As discussed earlier, the default version of Python on Ubuntu 22.04 is harmful to uninstall. However, if you have removed it accidentally then it can be fixed by following the steps mentioned in this section.

Once Python is uninstalled, you will get the following interface, which is tty1:


Now, all the fixing will be carried out using this tty1 support. If you are unable to get the tty support, you can use the shortcuts “CTRL+ALT+F1 or CTRL+ALT+F3” to invoke it.

Now, let’s move towards the steps.

Step 1: Fix the Internet Issue

Before proceeding, enable internet access on your Ubuntu system using the command which will allow the client to connect to the DHCP server:

$ sudo dhclient -r

 

Then, configure the network with the DHCP server via the command:

$ sudo dhclient

 

Step 2: Fix the Missing Dependencies

Now, use the command to locate and install the missing dependencies:

$ sudo apt-get install -f

 

 

Step 3: Install “ubuntu-Desktop”

After that, run the following command to install the Ubuntu-Desktop package to have the GUI and the CLI support on your Ubuntu 22.04:

$ sudo apt install ubuntu-desktop

 

As soon the installation is completed, the login screen will appear automatically:


Step 4: Verify the Default Python

Log in to the system and run the following command to verify the availability of the default Python3:

$ python3 --version

 

The output confirms that with the installation of Ubuntu-Desktop, the Python is restored.

It is suggested to reinstall the package “python-is-python3” to map the Python3 on Python:

$ sudo apt install python-is-python3

 

Now, check the Python version:

$ python --version

 

Python has now the same version as Python3.

Note: The uninstallation of default Python is not recommended as there are a lot of utilities that are dependent on default python.

How to Install a Different Version of Python From Source?

The source packages of Python can be obtained from its official website. These source files include the latest stable version and Python prerelease. This section will describe the steps to installing a specific Python version from source files.

Prerequisite Step: Install Dependencies

The following dependencies are recommended to be installed before proceeding with the Python’s installation:

$ 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

First, download the source files of the specific version that you want 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

 

Note: Navigate to the link to choose the Python version.

Step 2: Extract the Python Source Files

Navigate to the directory (in our case, it is the current directory) where the source files are downloaded and extract it using the “-xvf” flag of the “tar” command:

$ tar -xvf Python-3.11.1.tgz

 

 

Step 3: Configure Python

Switch to the directory of the extracted files and then run the configure script with the “–enable-optimizations” option for the best performance of the Python environment:

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

 

Step 4: Compile the Source Files

Now, compile the source files using the “make” utility to build an executable (binaries that are recognized by the machine) for the Python package:

$ make

 

 

Step 5: Install Python

Once the files are compiled, you can use the following command to install Python from the binaries:

$ sudo make altinstall

 

 

After the successful execution, run the following command to verify the installation:

$ python3.11 --version

 

The output shows that the Python 3.11.1 version has been installed.

Conclusion

To install Python on Ubuntu 22.04, you can use an apt package manager and compile/build the source files. By using APT, execute the “sudo apt install python<version>” command. While the source files are obtained from the official website and are processed by compiling them. This post has presented the methods to install Python on Ubuntu 22.04 and demonstrated the method to recover Ubuntu 22.04 if the default Python is removed accidentally.

Share Button

Source: linuxhint.com

Leave a Reply