Apt, Ubuntu’s package manager, is among the most powerful and intuitive, but that doesn’t mean that things can’t go wrong. Occasionally, a package install goes wrong on Ubuntu, and you’re left to pick up the pieces. It’s a good thing Ubuntu’s package utilities, including Apt itself, have built-in functionality to help remedy the situation and get things back to normal.

These tips and tricks should help you get unstuck in most instances. They’re all fairly universal, but every situation is different, so keep that in mind when trying to debug your own situation.

Related: How to Fix Ubuntu Update Errors

With Apt/Apt-Get

Apt has a couple of flags that you can use in order to fix missing dependencies or packages that broke for one reason or another during install. A common use here would be installing a third-party .deb and finding that it had dependencies you didn’t know about. Those dependencies probably won’t be pulled in on their own, and dpkg will complain that the package is missing in them. In any case, you can try these steps.

First, run an update to make sure that there aren’t newer versions of the required packages.

Next, you can try forcing Apt to look for and correct any missing dependencies or broken packages. This will actually install any missing packages and repair existing installs.

With DPKG

Another place where error can pop up in package installations is the configuration process. Behind the scenes, dpkg is taking care of this part, not Apt, so it would stand to reason that when a package fails during configuration, dpkg is the tool to turn to in order to fix it.

Start by trying to force dpkg to reconfigure any broken or partially configured packages.

If that doesn’t solve the problem, you can take a more forceful approach. Start by listing any packages that dpkg marked as requiring a reinstall.

You should see the problematic package(s) there. As long as there’s nothing that seems not to belong there, you can force-remove the broken packages.

When dpkg is done, try to clean up with Apt.

With any luck that’ll get you back to where you started. It won’t get you the broken packages that you tried to install, but at least Apt will be functional again.

Related: Ubuntu Software Center Not Working? Here Are the Fixes

Permanent DPKG Lock

There’s a less common issue with ridiculous dpkg locks preventing you from doing anything. Every time you try to use Apt or dpkg, you get an error saying another application already is … when it isn’t. It’s actually easy to delete the lock file preventing you from using Apt and get back to doing what you need to. Sometimes these lock files remain in place after an error occurs in an install, derailing the process and preventing the file from being removed automatically. In this case, you’ll need to do it yourself.

For good measure, delete the lock in cache.

Conclusion

Hopefully, one of these fixes worked for you, and you’re back working on Ubuntu normally. Remember that the best way to handle a totally out-of-hand situation is to try to return to the way it was before. Don’t try to power through and add more things to solve a breakage by adding more things, unless you know exactly what you’re doing. Chances are you’ll end up with a tangled mess of broken things that’ll be harder to sort out.