1. Home
  2. Linux
  3. Fix the could not get lock error on ubuntu

How to fix the “could not get lock” error on Ubuntu

Sometimes, when attempting to install software on Ubuntu in the terminal, you’ll see the could not get lock error. This error is irritating because it locks out the system and effectively prevents you from installing the software you need.

Could not get lock error

The reason this error happens is that often times Ubuntu users will close a terminal that is installing something prematurely while it is running. When they open up a second terminal and attempt to use the package manager, the “could not get lock” error appears because it doesn’t allow more than one instance of the packaging system to run at once.

While it’s easy to see why the package manager would lock itself, it’s annoying. Sadly, when Ubuntu users run into this issue, they panic because the operating system doesn’t explain how to fix it.

Fixing the “could not get lock” error is quite easy, even though the terminal prompt makes it seem scary and complicated. In this post, we’ll show you exactly what to do when you run into it, how to fix it, and how to prevent running into the error in the future.

Note: Are you a Debian user? Feel free to follow along! Ubuntu is built upon the Debian operating system, so the commands we use in this post will match perfectly! This method will also work for Linux Mint, Elementary OS, and other Ubuntu derivatives as well.

Kill Apt/Apt-get

As stated in the introduction to this post: the reason the “could not get lock” error appears when the Ubuntu package manager is already running (in the background), and the user attempts to start it a second time.

For as long as the problem Ubuntu package manager persists in the background, you won’t be able to use the install package tool to install programs, and that annoying error will persist.

Thankfully, the Ubuntu package manager is a program like any other and can be managed like one.

There are a few ways to kill processes on Linux. The best (and quickest way) is to use the ps command to get the process ID of the problem process (in this case Apt/Apt-get). Then, use the kill command to stop it.

So, to start, launch a terminal and run the ps aux command with Apt at the end of it (or Apt-get if you use that).

ps aux | egrep 'apt'

Or, for Apt-get users:

ps aux | egrep 'apt-get'

Look at the terminal, and read the numbers that appear in the output. That’s the process number for the package manager. Take that number and plug it into the kill command to stop.

Note: when using the kill command on system utilities, always use the “SIGTERM” or “SIGKILL” switch. If you don’t, the process may not stop running.

sudo kill -SIGKILL process-id-number

Or

sudo kill -SIGTERM process-id-number

Running the kill command should end the package manager process. If you’re unsure that the Ubuntu package manager has indeed been stopped, feel free to re-run the ps aux command above once more. If no number comes back, the problem process is gone, and you’ll be able to use the Ubuntu package manager once more!

Delete lock files

Most of the time, if the Apt package manager is stuck in the background and unresponsive the process can be ended with the kill command. Sometimes, however, ending the process isn’t enough.

If you’re running into the error and killing the Ubuntu package manager isn’t helping, there’s an alternative method you can attempt. It involves directly tinkering with the “locks” that the packaging tool puts on your computer.

The lock files are in three separate directories. To access the files, your terminal session must have root access. Root access on Ubuntu is gained with the sudo command.

sudo -s

After gaining root access, CD into the first directory.

cd /var/lib/dpkg/

rm lock

Now that you’ve taken care of the first lock file, it’s time to deal with the second one.

cd /var/lib/apt/lists/

rm lock

With the second lock file taken care of, get rid of the third and final lock file.

cd /var/cache/apt/archives/

rm lock

Once all three lock files are deleted with the rm command, you’ll be good to go, and the Ubuntu package manager should start working again. To test it run:

sudo apt update

Fix broken packages

When you close a terminal prematurely on Ubuntu while programs are installing, packages tend to break. To fix this, you’ll need to run the Dpkg command alongside the “configure” option.

dpkg --configure -a

After you finish running the command above, everything should be fixed and the broken packages should be ready to use.

1 Comment