78

I installed Python 2.7.13 on Ubuntu 16.04 according to this guide, and it became the default version as an alternative to the version 2.7.12. But, I wanted to completely remove Python 2.7.13 and return back to the version 2.7.12 as the default version since the pip command does not work with the following error.

bash: /usr/local/bin/pip: /usr/bin/python: bad interpreter: No such file or directory

Could you please help me how to completely remove Python 2.7.13 from Ubuntu 16.04? Otherwise, could you please suggest how to fix the above error?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Lucas
  • 920
  • 1
  • 6
  • 9
  • 6
    before doing anything else i suggest you run `$ sudo apt install --reinstall python python-apt python2.7-minimal`; ubuntu relies on a working python2 installation! and your question might be better suited here: https://askubuntu.com/ – hiro protagonist Jun 17 '17 at 08:04

6 Answers6

123

How I do:

# Remove python2
sudo apt purge -y python2.7-minimal

# You already have Python3 but 
# don't care about the version 
sudo ln -s /usr/bin/python3 /usr/bin/python

# Same for pip
sudo apt install -y python3-pip
sudo ln -s /usr/bin/pip3 /usr/bin/pip

# Confirm the new version of Python: 3
python --version
BntMrx
  • 2,187
  • 3
  • 18
  • 29
  • In my case, instead of **sudo apt purge -y python2.7-minimal** this **sudo apt purge python2.7-minimal** worked!! But after pressing **Enter** key I have to type **Yes, do as I say!** N.B. I'm using this in Linux Mint 19.3 not Ubuntu. Thanks @Benoit Mrx – mefahimrahman Jul 04 '20 at 10:01
  • 3
    So, if you add the -y no need to say yes! – BntMrx Jul 04 '20 at 18:31
  • Yes!! But simple it did not word using -y!! Idk why but it worked only giving permission by retyping the confirm string **Yes, do as I say!** – mefahimrahman Jul 05 '20 at 15:27
  • no we won't do as you say, because it's not logical that this would impact any difference – Conor Aug 30 '22 at 10:03
87

caution : It is not recommended to remove the default Python from Ubuntu, it may cause GDM(Graphical Display Manager, that provide graphical login capabilities) failed.

To completely uninstall Python2.x.x and everything depends on it. use this command:

sudo apt purge python2.x-minimal

As there are still a lot of packages that depend on Python2.x.x. So you should have a close look at the packages that apt wants to remove before you let it proceed.

Thanks, I hope it will be helpful for you.

Subarata Talukder
  • 5,407
  • 2
  • 34
  • 50
34

try following to see all instances of python

whereis python
which python

Then remove all instances using:

sudo apt autoremove python

repeat sudo apt autoremove python(for all versions) that should do it, then install Anaconda and manage Pythons however you like if you need to reinstall it.

Sean
  • 680
  • 7
  • 10
  • 5
    can't uninstall in a such way. anyway it's remain in the system... `sudo apt autoremove python Reading package lists... Done Building dependency tree Reading state information... Done Package 'python' is not installed, so not removed 0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.` then again: `sudo apt remove python2.7` then: `python --version` output: `Python 2.7.12` – littleAlien Apr 27 '18 at 14:34
  • 5
    this command removed some of the dependencies of my GUI and made my ubuntu-desktop stop working, this also removed google-chrome-stable and another packages. I really don't know why I run it, heheh – valdeci May 05 '19 at 15:09
  • 4
    DON'T EXECUTE AUTOREMOVE, it will remove other packages as well. – David Gomes Apr 16 '20 at 18:47
  • 2
    If I'm not wrong `autoremove` will remove the packages or dependencies which are no longer needed in the environment. – Parvathirajan Natarajan Jun 06 '20 at 17:39
  • 1
    nice comment to ruin SO – Egg Dec 13 '20 at 12:44
  • How to recover from this command? – Kevin Patel Mar 12 '22 at 19:17
  • Kevin - 1. sudo apt-get install python3.8.... 2. sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1.... 3. sudo update-alternatives --set python /usr/bin/python3.8 – Conor Aug 30 '22 at 10:48
  • run this command: sudo update-alternatives --config python ....to visually see and set the python path – Conor Aug 30 '22 at 10:54
3
sudo apt purge python2.7-minimal
Kevin
  • 16,549
  • 8
  • 60
  • 74
tayfun Kılıç
  • 2,042
  • 1
  • 14
  • 11
3

Sometimes you need to first update the apt repo list.

sudo apt-get update
sudo apt purge python2.7-minimal
Radzor
  • 162
  • 1
  • 7
2

This is what I have after doing purge of all the python versions and reinstalling only 3.6.

root@esp32:/# python
Python 3.6.0b2 (default, Oct 11 2016, 05:27:10) 
[GCC 6.2.0 20161005] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
root@esp32:/# python3
Python 3.8.0 (default, Dec 15 2019, 14:19:02) 
[GCC 6.2.0 20161005] on linux
Type "help", "copyright", "credits" or "license" for more information.

Also the pip and pip3 commands are totally f up:

root@esp32:/# pip
Traceback (most recent call last):
  File "/usr/local/bin/pip", line 7, in <module>
    from pip._internal.cli.main import main
  File "/usr/local/lib/python3.5/dist-packages/pip/_internal/cli/main.py", line 60
    sys.stderr.write(f"ERROR: {exc}")
                                   ^
SyntaxError: invalid syntax

root@esp32:/# pip3
Traceback (most recent call last):
  File "/usr/local/bin/pip3", line 7, in <module>
    from pip._internal.cli.main import main
  File "/usr/local/lib/python3.5/dist-packages/pip/_internal/cli/main.py", line 60
    sys.stderr.write(f"ERROR: {exc}")
                                   ^
SyntaxError: invalid syntax

I am totally noob at Linux, I just wanted to update Python from 2.x to 3.x so that Platformio could upgrade and now I messed up everything it seems.