I am having a problem with the package manager on 18.04 LTS

Solution 1:

From the comments I can read that you have cleaned out your sources.list file, so let's fix this first by copying the following block over into your terminal:

sudo su
cat > /etc/apt/sources.list << "EOF"
deb http://archive.ubuntu.com/ubuntu bionic main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu bionic main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu bionic-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu bionic-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu bionic-backports main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu bionic-backports main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu bionic-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu bionic-security main restricted universe multiverse
#deb http://archive.ubuntu.com/ubuntu bionic-proposed main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu bionic-proposed main restricted universe multiverse
deb http://archive.canonical.com/ubuntu bionic partner
deb-src http://archive.canonical.com/ubuntu bionic partner
EOF
exit

Now we can begin to fix your installation which seems to have ended up in a weird state.

First remove the removable packages after having run an update to get the proper package archives updated:

sudo apt update
sudo apt autoremove --purge

This should remove all removable packages so far except the one which managed to get messed up which we can then remove then by installing it and then removing it properly:

cd /var/cache/apt/archives
sudo dpkg -i linux-modules-4.15.0-22-generic_4.15.0-22.24_amd64.deb
sudo dpkg -r linux-modules-4.15.0-22-generic_4.15.0-22.24_amd64.deb

Now cleaning up the archives by running:

sudo apt clean

And now we can reinstall the packages properly after having run an complete update process:

sudo apt update
sudo apt dist-upgrade

with the following line:

sudo apt install --reinstall linux-generic

This should have fixed your system into a complete working state again. Have fun and welcome to the Linux community.

Solution 2:

If all the above solution doesn't work and facing issue while doing "sudo apt autoremove --purge" or most of apt or dpkg related commands similar to "sudo dpkg --configure -a" with following reason pasting exact real scenario issue:>

    sudo dpkg --configure -a
    Setting up linux-image-4.15.0-143-generic (4.15.0-143.147) ...
    dpkg (subprocess): unable to execute installed linux-image-4.15.0-143- 
    generic package post-installation script (/var/lib/dpkg/info/linux-image- 
    4.15.0-143-generic.postinst): Permission denied
    dpkg: error processing package linux-image-4.15.0-143-generic (--configure):
    installed linux-image-4.15.0-143-generic package post-installation script 
    subprocess returned error exit status 2
    Errors were encountered while processing:
    linux-image-4.15.0-143-generic

Even after giving full permission to permission denied using

    sudo chmod +x /var/lib/dpkg/info/linux-image-4.15.0-143-generic.postinst

    or
    cd /var/lib/dpkg/info/
    ls -lrth //Check the existing permission then change either full or only for root access     
    sudo chmod -R +755 /var/lib/dpkg/info/

Even then still facing the issue that permission is denied its not the issue of permission as eventhough permission given its due to grub issue follow below steps

    "CAUTION:>After taking proper checkpoint or snapshot of data to be in safe side as we 
    are reinstalling bootloader if reboot happens by mistake without proper 
    bootloader the vm becomes useless until data_mountpoint is present for all 
    configuration file also stop replication if present to avoid copying to 
    replica also proper internet access to fetch new bootloader".

Do the following:>

    apt-get purge grub-pc grub-common //This will purge the current bootloader setup new or update linux-image-*-generic and update-initramfs by generating /boot/initrd.img-*-generic  if menu.lst not found eventhough present in the directory is found do the following 

(If any issue menu.lst not found)

    touch /boot/grub/menu.lst
    apt-get purge grub-pc grub-common //Use the latest maintainer version to override the menu.lst it will make appropriate latest menu.lst changes if asked for in purge itself it would be mostly asked at "apt-get install grub-pc grub-common" as it finds multiple menu.lst will ask for compare but choosing maintainer version overide the files and will update with latest.

Else proceed with following:>

    rm -r /etc/grub.d/ //Check if grub is present if present will be removed mostly will be removed using purge and should show no files to remove or file doesn't exist 

Install the maintainer version if prompted if unable to choose where the bootloader needs to be installed proceed without choosing the disk the gui will prompt warning if using scsi disk sd(a,b,c) partition disk comes with 1,2,3 appended for number of partition inside if HDD is used hd(a,b,c) partition disk comes with 1,2,3 appended for number of partition inside ex:hda1,hda2,hdb1

    apt-get install grub-pc grub-common

sda or hda This will install in primary partition or root partition of the disk assigned for boot.

    grub-install /dev/sda

Check below command if updated without issue you have got the latest bootloader and all the issue related with the update will be removed and apt and dpkg commands will work normally.

    update-grub