8

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?

wjandrea
  • 14,504

3 Answers3

9

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.

Python version and Ubuntu version

This answer is from 2018... and therefore it applies for Ubuntu 18.04 and probably older versions. For Ubuntu 20.04 the python v2 is more or less deprecated and it must be installed with the meta package sudo apt install python-is-python2. Also the /usr/bin/python link does not exists if only python v3 is installed.

So it is save to say: This answer is not valid for python3 and Ubuntu versions greater then 18.04.

Simon Sudler
  • 4,111
0

In my case, making python point to python2 just resulted in a slightly different error message:

Traceback (most recent call last):
  File "/usr/lib/cnf-update-db", line 8, in <module>
    from CommandNotFound.db.creator import DbCreator
  File "/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py", line 11, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

I have python 3.9 installed, and what solved it for me was to make python3 point to python3.8 instead of python3.9:

$ ls -l /usr/bin/python*
lrwxrwxrwx 1 root root      16 Dec 22 13:53 /usr/bin/python -> /usr/bin/python2
lrwxrwxrwx 1 root root      18 Dec 22 13:54 /usr/bin/python2 -> /usr/bin/python2.7
-rwxr-xr-x 1 root root 3674216 Mar  8  2021 /usr/bin/python2.7
lrwxrwxrwx 1 root root      18 Dec 22 13:56 /usr/bin/python3 -> /usr/bin/python3.8
-rwxr-xr-x 1 root root 5490488 Nov 26 21:14 /usr/bin/python3.8
lrwxrwxrwx 1 root root      33 Nov 26 21:14 /usr/bin/python3.8-config -> x86_64-linux-gnu-python3.8-config
-rwxr-xr-x 1 root root 5803968 Nov 23 16:27 /usr/bin/python3.9
stemadsen
  • 101
0

In my case

python link was broken So, I was make soft link as below

ln -s /usr/bin/python2.7 /usr/bin/python2

ln -s /usr/bin/python3.8 /usr/bin/python3

complete!

Artur Meinild
  • 31,035