How to Reinstall|Repair Python on Linux Ubuntu 22.04|20.04
Posted April 4, 2024
This guide teaches you how to Reinstall or Repair corrupted Python installations on Linux Ubuntu 22.04|20.04. Ubuntu comes with Python installed. As a matter of fact, You do not want to mess up with the default installed Python. It will mess up your system. Ubuntu uses Python.
Out of the Box, some Ubuntu elements won’t work without the default installed dependency of Python3. Let’s say you have messed up this setup and your Ubuntu system is messed up already. You need to reinstall Python ASAP.
Related: How to Update and Upgrade Python3 Version on Ubuntu 22.04|20.04
There is no safe way to remove Python from Ubuntu. The better solution is to Reinstall and you must get it right. Otherwise, your Ubuntu will remain corrupted, Now, Let’s dive in and Reinstall|Repair Python on Linux Ubuntu 22.04|20.04.
Reinstall Python on Ubuntu: Reasons to not Uninstall Python
Let’s get into the helpful and cautionary tales of Reinstalling Python on Ubuntu. Removing Python will also uninstall packages dependent on Python this way:
- Your Ubuntu system gets vulnerable and insecure.
- The main impact is, that your GUI gets disrupted. You may get GUI stuck on a black screen.
- This gets even messed up if you don’t have access to SSH.
- Some Python modules are used by the operating system; Removing them creates instability.
- Packages such as APT use Python to get executed.
Examining Python Reinstall commands on Ubuntu
On closer look, you need to first check the command used to remove Python. By default, Python is saved on /usr/bin/python*
on Ubuntu. This means once Python gets installed these paths will get deleted.
Now, consider the following command used to remove Python from an Ubuntu system (check commands history):
27 python3 --version
28 ls /usr/bin/python*
19 python3 --version
30 ls /usr/bin/python*
31 sudo apt remove python3
32 ls /usr/bin/python*
33 python3 --version
34 sudo apt purge python3
35 sudo rm -r /usr/bin/python3
36 sudo rm -r /usr/bin/python3.10
37 python3 --version
38 history
Let’s check the steps to get Python reinstalled
Reinstalling|Repair Python on Linux Ubuntu 22.04|20.04
First, force Python to remove Python3 even though you have uninstalled it
sudo dpkg --remove --force-remove-reinstreq --force-depends python3
sudo update-alternatives --remove-all python3
The first case to reinstall Python3 is:
- If you have not deleted the Python3 files on your paths:
/usr/bin/python3
/usr/bin/python3.10
This means, you still have a Python3 installation candidate.
Now, you need to run the Python3 reinstallation command as follows:
sudo apt-get install --reinstall python3
sudo apt-get install --fix-broken
sudo apt-get update && sudo apt-get upgrade
This command should repair and reinstall your Python from the available installation candidates:
This way, you will reinstall Python and fix Corrupted Python. Confirm is Python3 is now ready:
python3 --version
which python3
Repair and Reinstall Deleted Python Listed Folders
When running the sudo apt-get install --reinstall python3
you might get the following error:
This happens if you deleted all folders listed to contain Python.
Now, you need to manually install the Python version compatible with your Ubuntu version. For example, on Ubuntu 22.04, it is Python 3.10 and Ubuntu 20.04 is version 3.8. Run your command as such:
sudo apt-get install python3.10
However, you will get dpkg error as follows:
In such a case, first repair dpkg and install Python with apt download as follows:
sudo apt-get download python3 python3.10 python3-minimal python3.10-minimal libpython3.10-minimal
sudo dpkg -i *python3*.deb
Remember python3.10 is for Ubuntu 22.04. Change it accordingly.
You might get FileNotFoundError error (Not a must)
Make sure you have your Python3 stdlib:
sudo apt-get download libpython3.10-stdlib
sudo dpkg -i libpython3.10-stdlib*
Ignore Permission denied if you got any
You must now manually install dpkg:
sudo dpkg --configure -a
To confirm Python works, Check the paths and version:
which python
## Try this command if the above failed
python3 --version
You should also have all deleted Python paths:
ubuntu@:~$ ls /usr/bin/python*
/usr/bin/python3 /usr/bin/python3.10
Reinstalling Python With Repair Modifications
If the above steps don’t work for you, you might consider creating modifications to your specific Ubuntu version. In this case, you will:
- Repair and reinstall Python
- Ensure Python works after the Ubuntu upgrade, for example, from 20.04 => 22.04.
- Correct corrupted Ubuntu broken Python installation.
- Reinstall Python after apt purging, etc.
If this is your case, check these steps. First, you need to grab the Ubuntu version you are using and its related default Python version. I have created the following table for you:
Ubuntu Version | Default Python Version |
---|---|
Ubuntu 16.04 | Python 3.5 |
Ubuntu 18.04 | Python 3.6 |
Ubuntu 20.04 | Python 3.8 |
Ubuntu 22.04 | Python 3.10 |
In My case, I will use Ubuntu 22.04 with Python 3.10 as follows:
- Download the required packages for Python 3.10 and Ubuntu 22.04 and have all the necessary packages:
cd /tmp
sudo apt-get download libpython3.10-minimal
sudo apt-get download libpython3.10-stdlib
sudo apt-get download python3-minimal
sudo apt-get download python3.10-minimal
sudo apt-get download python3.10
⚠️ Remember to change the Ubuntu and Python versions based on your system.
- Remove existing Python 3.10 installations on your system to avoid conflicts with related libraries and executables:
# Remove Python libraries
sudo rm -rf /usr/local/lib/python3.10*
# delete Python executables
sudo rm -rf /usr/local/bin/python3.10*
# Remove Python3 symlink
sudo update-alternatives --remove python3 /usr/local/bin/python3.10
# delete cached python3 path
sudo hash -r
- Extract downloaded Python 3.10 packages:
cd /tmp
# Extract libpython3.10-minimal package
sudo dpkg-deb -x libpython3.10-minimal_3.10.x-xubuntu0~22.04_amd64.deb missing
# libpython3.10-stdlib package
sudo dpkg-deb -x libpython3.10-stdlib_3.10.x-xubuntu0~22.04_amd64.deb missing
# python3.10-minimal package
sudo dpkg-deb -x python3.10-minimal_3.10.x-xubuntu0~22.04_amd64.deb missing
# Extract python3.10 package
sudo dpkg-deb -x python3.10_3.10.x-xubuntu0~22.04_amd64.deb missing
# python3-minimal package
sudo dpkg-deb -x python3-minimal_3.10.x-xubuntu0~22.04_amd64.deb missing
- Copy extracted files to system directories to make sure Python 3.10 files are installed correctly on your system
cd /tmp/missing
# Copy extracted files to system directories
sudo cp -rpfv /tmp/missing/* /
- Verify the Python version and if it is correctly installed:
python3
# You should see Python 3.10 running.
which python3
# Check Python version paths
- Go ahead and clean up the temporary directory used for extraction:
# Remove temporary files
sudo rm -rf /tmp/missing
- Reinstall Any Packages Dependent on Python 3.10 compatibility:
dpkg -s -a python3.10 | grep reinstreq
# Any listing will be reinstalled along with python3
sudo apt-get install --reinstall python3
- Install software-properties-common to add essential utilities:
sudo apt install software-properties-common
Once sudo apt install software-properties-common
the whole process confirms you have reinstalled and repaired Python.
Reinstalling Freshly Compiled Python Version
Download a new Python version and reinstall it on Ubuntu as follows:
- Download Python from the source based on your version.
wget https://www.python.org/ftp/python/3.10.12/Python-3.10.12.tgz
- Now unzip this file source:
sudo tar -xvf Python-3.10.12.tgz
- CD to this directory:
cd Python-3.10.12
- Finally, install your new Python3 source code:
## first create makefiles
sudo ./configure --enable-optimizations
## Install Python3 using this command
sudo make altinstall
## Confirm Python path
which python3
Conclusion
You have learned how to Reinstall and Repair Python on Linux Ubuntu 22.04|20.04. I hope you can now safely use Ubuntu while ensuring Python is running as expected.
These solutions were tested on Ubuntu 22.04 and 20.04. If you encounter a challenge leave a comment below.