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:
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:
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:
After installing the “python-is-python3” package, the “python” command will show the Python3 (as per the interpreter “/usr/bin/python3”):
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:
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:
Then, configure the network with the DHCP server via the command:
Step 2: Fix the Missing Dependencies
Now, use the command to locate and install the missing dependencies:
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:
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:
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:
Now, check the 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:
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”:
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:
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:
$ ./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:
Step 5: Install Python
Once the files are compiled, you can use the following command to install Python from the binaries:
After the successful execution, run the following command to verify the installation:
The output shows that the Python 3.11.1 version has been installed.
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.