7

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,236
  • 4
  • 48
  • 98
  • 2
    Have you tried sudo apt-get -f install? – valiano Sep 12 '18 at 21:22
  • If I try that it returns me another error: Sub-process /usr/bin/dpkg returned an error code(1) – Fernando Sep 12 '18 at 21:24
  • Some of the suggestion soutions here might help: https://askubuntu.com/questions/688338/dependency-errors-after-installing-and-attempting-to-remove-google-chrome – valiano Sep 12 '18 at 21:28
  • 1
    If you enter python --version what version do you have default (it should be 2.7.x; it's 2.7.15+ on my 18.10 box) Python (2.7) & Python3 (3.6.6 on my box) are installed by default, and Ubuntu still requires python to run 2.7; you use python3 if you need python 3.x (did you try and make python3 default??). For @valiano's suggestion; the actual error was above your post (dpkg returned code was from calling routine, the subprocess it called reported the error earlier in the output & you only listed the end summary.. I'd re-run sudo apt-get -f install and paste output to your question – guiverc Sep 12 '18 at 23:48
  • This fixed the issue for me: https://bugs.launchpad.net/ubuntu/+source/command-not-found/+bug/1876034/comments/2 – Daniel S. Sterling Oct 24 '20 at 08:01

3 Answers3

8

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
  • 3,931
  • 3
  • 21
  • 34
  • I ended up re-installing Ubuntu since I had installed a few days ago anyway. But thank you very much for you answer, it totally makes sense what you say about the python pointing. It made me understand the problem and it may help another person. – Fernando Sep 14 '18 at 07:09
  • You can do it with whichever python version: 2 or 3 – joninx Jun 11 '20 at 08:40
  • @russellhoff sure you can change it... and it will break packages that expect python2 linked to /usr/bin/python – Simon Sudler Jun 11 '20 at 10:51
  • In my case this resulted in a slightly different error message:

    Traceback (most recent call last): File "/usr/lib/cnf-update-db", line 8, in from CommandNotFound.db.creator import DbCreator File "/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py", line 11, in 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.

    – stemadsen Dec 22 '21 at 13:02
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
  • 26,018