How to repair broken packages in Ubuntu


Published on 04 February 2023


How to repair broken packages in Ubuntu

Issues while using Ubuntu or Linux

Fixing packages on Ubuntu is essential if you run into any issues while using the operating system. Package management on Ubuntu is similar to what you would find on other Linux distributions, making it fairly easy to fix problems with software. However, it's a good idea to master all your options when fixing packages. This overview will help you do just that.

Fix broken packages using the command line tools

You can fix broken packages without accessing package files or source code if you are adept at using the command line tools. Many commands let you fix broken packages without accessing package files or source code- some of them include:

  • apt update
    Command designed to update system packages.
  • apt-get dist-upgrade
    Updates Linux distributions.
  • apt-get remove package
    Remove installed packages, but leave their configuration files behind
  • apt-get clean
    Removes all packages from the cache.
  • apt-get autoremove
    Removes missing packages, usually installed as dependencies of other installations, which are no longer needed.
  • apt-get autoclean
    Removes old and unusable version packages from the local repository.
  • apt-get install
    Installs programs or packages.

Basically, there are dozens of commands available for performing system maintenance; some are specifically designed for fixing packages but work for other problems as well.

Fix broken packages in Ubuntu or Debian using APT

Run the updates and check that the packages are up to date.

sudo apt update --fix-missing

The following command forces the package manager to find broken packages and install them.

sudo apt install -f

Fix broken packages using apt in Ubuntu

Fix broken packages in Ubuntu or Debian using apt and list file

Another method to resolve broken package errors in Ubuntu is to edit the "/etc/apt/sources.list" file and add sites with new versions of available packages.

sudo nano /etc/apt/sources.list

Add repositories deb file in Ubuntu

When we open this file we get a list of all the repositories on our system. 

  • The repositories starting with # are those that are in use.
  • Repositories starting with # are those that we have installed, but are disabled.

To add the default Ubuntu software repositories we must add the URL.
If you want to install software from a third-party repository you should look for the URL from the list of available Ubuntu repositories or from another reliable website.

The safest way to add a repository is from the terminal in the following way:

deb http://es.archive.ubuntu.com/ubuntu/ focal main restricted
sudo apt update

Sources list Ubuntu

Fix broken packages in Ubuntu or Debian using autoremove and remove

Update the repository.

sudo apt update

Clean the local repository.

sudo apt clean

Remove all unnecessary packages.

sudo apt autoremove

Force the removal of a broken package.

sudo dpkg --remove -force --force-remove-remove-reinstreq package_name

Fix broken packages in Ubuntu or Debian using autoremove and remove

Fix broken packages in Ubuntu or Debian using dpkg

Reconfigure all pending packages that are already unpacked but need to be configured.

sudo dpkg --configure -a

Remove all broken packages.

sudo dpkg --remove --force-remove-reinstreq

Clear package cache and install scripts.

sudo apt clean

Update the system package lists.

sudo apt update

Package repair through backups

Most package files are backed up on Ubuntu. This way you can easily recover your data if something goes wrong.

If we have our system in a good state with the software installed, we can obtain a list of the installed packages:

  1. Create a backup folder.
    sudo mkdir ~/backup
  2. Create the packages.log file.
    sudo nano ~/backup/packages.log
  3. Change the permissions of the created file to be able to write to it.
    sudo chmod 777 ~/backup/packages.log 
  4. Save the list of currently installed packages.
    dpkg --get-selections > ~/backup/packages.log
  5. Make a backup copy of the sources.list file.
    sudo cp /etc/apt/sources.list ~/backup/sources.bak
  6. Create the repositories.keys file. Change the permissions. Copy the list of trusted keys from your apt.
    sudo apt-key exportall > ~/backup/repositories.keys
  7. Finally we change the permissions of the files to a safe mode. 
    sudo chmod 644 ~/backup/repositories.keys

With this we will be able to restore our repository whenever we have serious problems in our system.

To restore the repository we follow the steps below:

  1. Restore the sources.list file.
    sudo cp ~/backup/sources.bak /etc/apt/sources.list

  2. Restore the key file.
    sudo apt-key add ~/backup/repositories.keys

  3. Update your source lists.
    sudo apt-get update
  4. Restore the packages

    sudo dpkg --clear-selections
    sudo dpkg --set-selections < ~/backup/packages.log && sudo apt-get dselect-upgrade

You should note that only root users have access to the backup folder, so be sure to follow the instructions above whenever you fix packages as root.

Editing the package source code directly

If you don't want to recover data from a backup, you can still fix broken packages without restoring files. The most common way to fix a problem is by editing the package source code directly. To do this, you will need to become root and temporarily edit the program's source code on your local computer.

This way you can make sure your computer is operating on the same version of the software as the distribution's version control system. After making any necessary changes, save your work and exit your local LXC/container. You can then copy and paste new code into the program's .srcinfo file and replace incorrect code with your edits. This works because all file access is blocked while editing .srcinfo files. Once finished, upload your edited code and replace the original package with your modified code- this should fix your problem.

Mastering these options will help you fix problems with packages whenever they occur on Ubuntu. You can easily recover data from backed up packages if you don't have administrative rights or if you don't know how to use LXC/container technology. Or, if fixing broken packages seems too complicated for you, there are other methods available for fixing packages without accessing package files or source code. Either way works fine since all of these options work flawlessly on Ubuntu without any user skill requirements.

 


Tips on SEO and Online Business

Next Articles

Previous Articles