- How to remove Python 2 from Ubuntu 20.04?
- 2 Answers 2
- Uninstall Python in Ubuntu 22.04
- Prerequisites
- Python Major Releases
- Method 1: Removing Python Using APT
- Step 2: Uninstalling Python
- Optional: Remove All Python Packages
- Method 2: Removing Python from the Source
- Method 3: Removing PyPy
- Bonus: Removing PIP
- Conclusion
- About the author
- Sidratul Muntaha
- zhensongren / uninstall_python3.MD
- Ye kisne de diye baccho ke haath mein laptop
- Footer
How to remove Python 2 from Ubuntu 20.04?
I’ve upgraded recently from Ubuntu 18.04 to 20.04. But I can see that Python2 is still the default instead of python 3.8.2.
$ python -V Python 2.7.18rc1 $ python3 -V Python 3.8.2
Is it possible to remove python 2 and replace it with Python 3.8? EDIT: I have always used the following aliases in my ubuntu 18.04:
alias python='python3' alias pip='pip3'
After upgrading, I removed those aliases because I thought python2 should no longer be there but it is still there. Thanks
I can’t help sorry, but my system automatically removed python2 , and python now reports 3.8.3. Had you made changes to your python defaults at any time? (my upgrade path was not LTS to LTS though)
@guiverc Yes, I was always using an alias to python3 and pip3 from my bashrc file. But I forgot about that before upgrading, but even though ubuntu should be smart enough to rely on absolute paths instead of my bashrc file.
FYI: Upgrading testing involves no user-changed configs/aliases being setup or used, so for best results you should always change your own modifications back to default before your release-upgrade (esp. where related to system used tooling like python!). It is impossible to cater, or test for every possible user-changeable config, however the upgrade path from 18.04 LTS to 20.04 LTS isn’t strictly open yet because testing & fixes aren’t fully completed (the path opens only after the release of 20.04.1) You could submit a bug report, which may prevent other users from encountering it..
2 Answers 2
Before proceeding make sure that you do not really have packages which depend on Python 2.
Then you need to install special package which set aliases for you:
sudo apt-get install python-is-python3
And optionally remove all trails of Python 2 packages manually by
sudo apt-get autoremove --purge
That works fine. Just as a note, I am trying to install now a package but it will remove python-is-python3 and install python-is-python2 again.
@Navaro then you cannot remove Python 2 and its aliases. The package you are installing «new» has a dependency on Python 2 existing, therefore it needs Python 2 to work. You are stuck without options in this case.
@ThomasWard: Yes, I am stuck because some packages I use frequently still depend on Python2: VirtualBox, paraviewopenfoam, etc.
As user ‘N0rbert’ answered, you should install ‘python-is-python3’ to set aliases. And then check the packages that depend on python2 before removing them.
As with my experience with ubuntu 20.04 LTS, only python3 was installed, and I installed python2 by mistake. So, it was relatively easy to uninstall python2.
To uninstall, enter following commands in terminal:
sudo apt remove python2 --simulate sudo apt remove python2 sudo apt autoremove --purge
Break-down of above commands:
sudo apt remove python2 —simulate : perform a simulation of events that would occur but do not actually change the system.
This would print details of what might happen if you remove python2 from the system. If you are satisfied with the result(outcome) then you may do actual remove by: sudo apt remove python2 and then sudo apt autoremove —purge to remove the configuration files and the unused packages.
Uninstall Python in Ubuntu 22.04
Python is one of the most popular programming languages. It’s an interpreted general-purpose programming language with an emphasis on simplicity. Because of its versatility, Python is used for various purposes: web development, data analysis, artificial intelligence, and much more.
In this guide, we will have a look at uninstalling Python in Ubuntu 22.04.
Prerequisites
To follow this guide, you need the following components:
- A properly-configured Linux system. Learn more about setting up an Ubuntu virtual machine on VirtualBox.
- Access to a non-root user with sudo privilege. Check out the article on using sudoers to manage the sudo privilege.
Python Major Releases
As of now, the two major versions of Python are:
Python 2 received its last update (v2.7.18) on April 20, 2020. It’s been mostly phased out in favor of Python 3. This move, however, caused a major uproar in the community. Python 2 was so popular that the EOL had to be pushed multiple times into the future.
Although deprecated, you may still come across some Python 2 installations for compatibility reasons. By default, Ubuntu comes with installed Python 3.
Method 1: Removing Python Using APT
Step 1: Finding the Installed Python Package
Run the following commands:
- We ask the Python executable to print its version.
- Ubuntu 22.04 comes with pre-installed Python 3. So, the first command returns a version number.
- Ubuntu 22.04 doesn’t come with pre-installed Python 2. So, the expected result is an error. However, if the command returns a version number, Python 2 is later installed.
On Debian/Ubuntu, the core Python packages are as follows:
Step 2: Uninstalling Python
Now that we know what Python version is currently installed on the system, we can start to work on uninstalling them.
To remove Python 2, run the following command:
To remove Python 3, run the following command:
Optional: Remove All Python Packages
Removing all the Python-related packages is generally not recommended since various parts of the system depend on them. If you wish to proceed, ensure that you backed up all your important data.
To remove all the Python packages from the system, run the following command:
- The APT package manager looks for packages that match the given regular expression (*python*). The matching packages are marked for removal.
- APT also marks the dependencies of those packages for removal.
Method 2: Removing Python from the Source
If Python was compiled and installed from its source code, APT won’t be able to recognize the installation. In that case, the uninstallation process will be different.
Assuming you still have the source directory which contains the compiled Python package, run the following commands:
If you removed the source directory, you could try removing the installed libraries and binaries manually:
Note that this is meant to be the last resort. It may lead to corrupted and broken configurations throughout the system.
Method 3: Removing PyPy
PyPy aims to be a replacement for CPython (the default Python implementation). It’s built with RPython which was simultaneously developed with it. The key advantage to using PyPy other than CPython is performance. Although it’s an implementation of Python, certain differences can impact compatibility. Learn more about PyPy.
Similar to the classic Python, PyPy also has two major releases:
To install PyPy, run the following commands:
Similarly, to uninstall PyPy, run the following commands:
Bonus: Removing PIP
PIP is the de-facto standard package manager for Python packages/modules. By default, it’s configured to use the Python Package Index as the source of packages. Starting from Python 3 (v3.4 and later), PIP comes pre-installed with Python 3. The term “PIP” is a recursive acronym for “PIP Installs Packages”. Learn more about PIP.
Similar to the Python major releases, PIP has unique versions for both Python 2 (python-pip) and Python 3 (python3-pip).
If you removed all the Python packages, PIP is also uninstalled by default. However, if you desire to specifically uninstall PIP, run the following commands:
Conclusion
We demonstrated the multiple ways of removing Python from Ubuntu 22.04. We demonstrated uninstalling both CPython and PyPy from the system using APT. We also discussed uninstalling Python if it is installed from the source code.
Need to reinstall Python? Check out this guide on installing Python on Ubuntu 22.04. Interested in starting your journey with Python? The following guide features 30 example scripts to get started. The Python sub-category also contains numerous guides on various aspects of Python programming.
About the author
Sidratul Muntaha
Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.
zhensongren / uninstall_python3.MD
bro did you find a way to fix it, my ubuntu also broken because of 2nd command. It would be grateful if you can help me with this.
Very careful before using above command.
In case you successfully uninstalled default package, use below command 😉
sudo apt-get install ubuntu-desktop
thank you my ubuntu was broken!! 🙂
bro did you find a way to fix it, my ubuntu also broken because of 2nd command. It would be grateful if you can help me with this.
I just used this command to fix ubuntu :
sudo apt-get install ubuntu-desktop
Very careful before using above command.
In case you successfully uninstalled default package, use below command 😉
sudo apt-get install ubuntu-desktop
Thank you!
My ubuntu was also broken
Destroyed my linux purging python, be careful
sudo apt-get purge . destroys dependencies . Be careful .
sudo apt-get install ubuntu-desktop
helps recover dependencies but not all
This is NOT the way to. do this.!
You will trash your system if you start purging like this.
Thank you for this. I had installed python3.10, it was causing problems and I couldn’t uninstall it through rm. I am still fairly new to linux so thank you.
yes, I’ve destroyed my computer and lost everything trying that, but my question still the same, how I can upgrade or deleted and then re-install the latest python version?
I use different python versions (3.8, 3.10) on my ubuntu. I decided to uninstall pythion3.10.
Now, anytime I install a package with pip, it is downloaded in ‘python3.10/site-packages/’ not ‘python3.8/site-packages/’
Does anyone know how I can resolve this? I cannot also install packages in a venv because it is always installing globally to ‘python3.10/site-packages/’
Please help. Thank you for your response
⚠ ⚠ ⚠ These commands ruined the whole WSL Ubuntu successfully! ⚠ ⚠ ⚠
thank you!
for readers in the future: just try searching and use other methods.
to repair python2.7 on Ubuntu: sudo apt-get install —reinstall python2.7 .
Note to self: Do not run commands on a github Gist without reading the comments section xD
I CANT BELIEVE UBUNTU CRASHED!! SUCH A FRAGILE USELESS OS
Some commands are best tried on virtual system first. Be careful.
installed and have no graphical interface
sudo apt install —reinstall ubuntu-desktop fixed
I ran this command and I’m using a WSL with Ubutu. Any ideas on how I can fix mine?
yall really testing these commands on your laptops ? what docker or vm did to you to hate using it so much
Very nice. I just blew my VM. Thanks for nothing.
What the hell, my terminal got removed
Ye kisne de diye baccho ke haath mein laptop
1st result when searching «ubuntu uninstall python» lmao
Thank you! My ubuntu was also broken 😅
Don’t try it or will distroy Your ubuntu
too late to read the comments
Thank you. I Just destroyed my ubuntu server
Destroyed my entire fing Ubuntu install, If anyone sees this comment, I would highly advise against using these commands, It will trash your system, if you fall victim to this, enter TTY console if you can, and type sudo apt-get update —fix-missing, then once this finishes, enter sudo apt install -f, this will fix the Ubuntu install, if it doesn’t you work you must reinstall Ubuntu somehow. For me, this method worked.
Footer
You can’t perform that action at this time.