Ubuntu – Error when trying sudo apt-get update – Python related

aptpackage-management

I hope you can help me. I installed Ubuntu 18.04 a few days ago and, while trying to configure it and installing new packaged and software, I think I did something bad. I think it could be related with installing python. Anyway, netiher the gnome-terminal or gnome-tweaks canĀ“t start anymores. Also, when I try to update (sudo apt-get update) it gives me the following error:

sh 1: /usr/lib/cnf-update-db: not found
Reading package lists... Done
E: Problem executing scripts APT::Update::Post-Invoke-Succes 'if /usr/bin/test -w /var/lib/command-not-found -a -e /usr/lib/cnf-update-db > /dev/null; fi'
E: Sub-process returned an error code

I am relativeley new to Linux so I have no clue how can I fix this. Any idea? Even to restore defult configurations/settings?

Thank you very much!

Best Answer

It is, as suggested by @guiverc, most likely Python version related. It seems that many Python tutorials these days suggests to change the default Python version from 2 to 3. While this is nice and practical for Python development, it breaks the packages which are using Python 2 scripts in there installation process.

So check where the link /usr/bin/python is pointing to:

$ ls -la /usr/bin/python
lrwxrwxrwx 1 root root 9 Jan 24  2017 /usr/bin/python -> python2.7

It should point to python2, not to any python3 executable. If it points to python3 then do the following (man ln):

$ sudo rm -f /usr/bin/python
$ sudo ln -s /usr/bin/python2.7 /usr/bin/python

After that, the apt-get will start to work again.

Background on Python interpreter version

Many scripts use the Shebang to control which interpreter is used to executed the following script. In most Python 2 scripts the following lines are used:

#!/usr/bin/env python

For Python 3 this shebang is used:

#!/usr/bin/env python3

If the default link to the Python 2 (/usr/bin/python -> python2.7) interpreter is changed to any version of Python 3, all "old" Python 2 scripts will stop working.

Related Question