Score:0

How to fix broken apt in Ubuntu 18.04 without reinstalling from scratch

cn flag

I have an old server running Ubuntu 18.04.6 LTS, and I suspect that someone tried to install or upgrade to the wrong Ubuntu 20.04 packages, resulting in a broken apt and a so-called "Frankensystem."

I would like to fix this issue without resorting to reinstalling the entire system, especially since the server is located in a remote location. Is it possible to fix the broken packages one by one, and if so, how can I do this?

Here are the errors I encounter:

When running sudo apt update:

$ sudo apt update
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease
Get:3 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Fetched 222 kB in 2s (118 kB/s)
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
ImportError: libapt-pkg.so.6.0: cannot open shared object file: No such file or directory
Reading package lists... Done
E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/lib/command-not-found/ -a -e /usr/lib/cnf-update-db; then /usr/lib/cnf-update-db > /dev/null; fi'
E: Sub-process returned an error code

when running sudo apt --fix-broken install:

$ sudo apt --fix-broken install
Reading package lists... Done
Building dependency tree... Done
Correcting dependencies... failed.
The following packages have unmet dependencies:
 dirmngr : Depends: init-system-helpers (>= 1.52) but 1.51 is installed
 gir1.2-gtk-3.0 : Depends: gir1.2-gdkpixbuf-2.0 (>= 2.40.0) but 2.36.11-2 is installed
                  Depends: gir1.2-pango-1.0 (>= 1.41.0) but 1.40.14-1ubuntu0.1 is installed
                  Depends: libgtk-3-0 (>= 3.24.7) but 3.22.30-1ubuntu4 is installed
 gnupg : Depends: gpgv (>= 2.2.19-3ubuntu2.2) but 2.2.4-1ubuntu1.6 is installed
 gnupg-utils : Depends: libreadline8 (>= 6.0) but it is not installed
 gpg : Depends: libreadline8 (>= 6.0) but it is not installed
 gpg-agent : Depends: init-system-helpers (>= 1.52) but 1.51 is installed
 gpgconf : Depends: libreadline8 (>= 6.0) but it is not installed
 gpgsm : Depends: libreadline8 (>= 6.0) but it is not installed
 hplip : Depends: hplip-data (= 3.20.3+dfsg0-2) but 3.17.10+

when I check /usr/lib/x86_64-linux-gnu I found libapt-pkg.so.5.0 exists and but no libapt-pkg.so.6.0

Also, in /usr/lib/python3/dist-packages I found apt_pkg.cpython-38-x86_64-linux-gnu.so which is strange, because the python3 version is 3.6.9

Regarding backups, I found these files

/var/backups$ ls -l
total 7276
-rw-r--r-- 1 root root    153600 Apr 10 00:08 alternatives.tar.0
-rw-r--r-- 1 root root      9783 Jun 24  2022 alternatives.tar.1.gz
-rw-r--r-- 1 root root      9808 Nov 27  2021 alternatives.tar.2.gz
-rw-r--r-- 1 root root      9421 Mar 27  2021 alternatives.tar.3.gz
-rw-r--r-- 1 root root      6161 Mar 18  2021 alternatives.tar.4.gz
-rw-r--r-- 1 root root      6090 Jan 25  2021 alternatives.tar.5.gz
-rw-r--r-- 1 root root      6061 Nov 28  2020 alternatives.tar.6.gz
-rw-r--r-- 1 root root    154853 Apr  9 23:02 apt.extended_states.0
-rw-r--r-- 1 root root     12962 Apr  9 21:44 apt.extended_states.1.gz
-rw-r--r-- 1 root root     12828 Jul 26  2022 apt.extended_states.2.gz
-rw-r--r-- 1 root root     12821 Jul  4  2022 apt.extended_states.3.gz
-rw-r--r-- 1 root root     12806 Jun 23  2022 apt.extended_states.4.gz
-rw-r--r-- 1 root root     13304 Jun 17  2022 apt.extended_states.5.gz
-rw-r--r-- 1 root root     13269 Nov 26  2021 apt.extended_states.6.gz
-rw-r--r-- 1 root root      4649 Jun 23  2022 dpkg.diversions.0
-rw-r--r-- 1 root root       657 Jun 23  2022 dpkg.diversions.1.gz
-rw-r--r-- 1 root root       657 Jun 23  2022 dpkg.diversions.2.gz
-rw-r--r-- 1 root root       657 Jun 23  2022 dpkg.diversions.3.gz
-rw-r--r-- 1 root root       657 Jun 23  2022 dpkg.diversions.4.gz
-rw-r--r-- 1 root root       657 Jun 23  2022 dpkg.diversions.5.gz
-rw-r--r-- 1 root root       657 Jun 23  2022 dpkg.diversions.6.gz
-rw-r--r-- 1 root root       348 Mar 18  2021 dpkg.statoverride.0
-rw-r--r-- 1 root root       213 Mar 18  2021 dpkg.statoverride.1.gz
-rw-r--r-- 1 root root       213 Mar 18  2021 dpkg.statoverride.2.gz
-rw-r--r-- 1 root root       213 Mar 18  2021 dpkg.statoverride.3.gz
-rw-r--r-- 1 root root       213 Mar 18  2021 dpkg.statoverride.4.gz
-rw-r--r-- 1 root root       213 Mar 18  2021 dpkg.statoverride.5.gz
-rw-r--r-- 1 root root       213 Mar 18  2021 dpkg.statoverride.6.gz
-rw-r--r-- 1 root root   2529012 Jul  3 16:58 dpkg.status.0
-rw-r--r-- 1 root root    708160 Apr 10 00:09 dpkg.status.1.gz
-rw-r--r-- 1 root root    708160 Apr 10 00:09 dpkg.status.2.gz
-rw-r--r-- 1 root root    708160 Apr 10 00:09 dpkg.status.3.gz
-rw-r--r-- 1 root root    708160 Apr 10 00:09 dpkg.status.4.gz
-rw-r--r-- 1 root root    708160 Apr 10 00:09 dpkg.status.5.gz
-rw-r--r-- 1 root root    708160 Apr 10 00:09 dpkg.status.6.gz
-rw------- 1 root root      1336 Apr  9 22:33 group.bak
-rw------- 1 root shadow    1107 Apr  9 22:33 gshadow.bak
-rw------- 1 root root      3518 Apr  9 22:33 passwd.bak
-rw-r--r-- 1 root smmsp    64178 Mar 18  2021 sendmail.cf.bak
-rw-r--r-- 1 root smmsp     4058 Mar 18  2021 sendmail.mc.bak
-rw------- 1 root shadow    2479 Apr  9 22:33 shadow.bak
-rw-r--r-- 1 root smmsp    44618 Mar 18  2021 submit.cf.bak
-rw-r--r-- 1 root smmsp     2375 Mar 18  2021 submit.mc.bak

Or can I use any of these files to restore the system to a working apt?

guiverc avatar
cn flag
[Ubuntu 18.04 LTS has reached the end of it's *standard* support life](https://fridge.ubuntu.com/2023/05/13/extended-security-maintenance-for-ubuntu-18-04-bionic-beaver-begins-31-may-2023/) thus is now off-topic here unless your question is specific to helping you move to a *fully supported* release of Ubuntu. Ubuntu 18.04 ESM support is available, but not on-topic here, see https://askubuntu.com/help/on-topic See also https://ubuntu.com//blog/18-04-end-of-standard-support
Marco M avatar
cn flag
Yes, I would like to move to 20.04 LTS (focal). Is this possible from my current situation ? can I use those backup files for such purpose ?
user535733 avatar
cn flag
It's a big, ugly job. (Sorry). It will take a lot of time and require a lot of learning. Your first step is to determine if the older 18.04 version of Python is still installed. If so, use it to run apt. Then you can actually start the real job. Since 18.04 is no longer supported here, you're over a month too late to get more detailed help. An on-site trip to reinstall could be considered the price of delay.
Marco M avatar
cn flag
apt 1.6.14 is installed and python 3.6.8 too. However, `sudo apt update` fails with `ModuleNotFoundError: No module named 'apt_pkg'` Is it impossible to do this without a working apt? perhaps reinstallation is indeed the only way. thanks!
guiverc avatar
cn flag
There are a load of tools that require the specific python3 version the system came with, and `apt` is an example of a tool that relies on python3. You can use `dpkg` to install with an incorrect python3 setup/version, and `wget` to download, however user-level tools like `apt` that make those lower-level tools easier may not be available on a *frakensystem*. (*Note: its not just installed, but default versions do matter.. so setup & not installation need to be correct.. Ubuntu will work with many python3 installed, but the default version is what matters*)
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.