How to remove Python 2 from Ubuntu 20.04?

21,190

Solution 1

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

Solution 2

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.

Hope it helps you.

Share:
21,190
s.ouchene
Author by

s.ouchene

Updated on September 18, 2022

Comments

  • s.ouchene
    s.ouchene over 1 year

    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

  • s.ouchene
    s.ouchene almost 4 years
    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.
  • Thomas Ward
    Thomas Ward almost 4 years
    @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.
  • s.ouchene
    s.ouchene almost 4 years
    @ThomasWard: Yes, I am stuck because some packages I use frequently still depend on Python2: VirtualBox, paraviewopenfoam, etc.
  • N0rbert
    N0rbert almost 4 years
    @Navaro then use Python 2 as usual. And do not remove it and its dependents.