This post explains why you may sometimes not be able to run apt commands and how to find out what is causing the issue.
Why apt sometimes cannot get a lock
apt-get is one of the package administration programs for a range of Linux based operating systems. This manages the list of packages you have installed (pieces of software). In addition this lets you install and remove packages from all your software repositories installed.
One of the ways this ensures that the package database is always kept in a correct state is by locking it. This ensures that only one process can be using and updating the package database at any one time.
The standard message you get when you fail to get the lock is reproduced below.
Unable to lock the administration directory (/var/lib/dpkg/) is another process using it?
This would occur if you tried to use your package manager while another process has the lock. Recently a number of distributions have included software that will automatically update apt. This can cause apt to be locked when you are trying to use it.
Checking what is using apt
On Ubuntu there are a number of programs that will use apt on the backend. While they are performing operations they will lock the apt database causing the error message. A couple possible programs that might lock apt when using it are:
- Ubuntu Software Center
- The Update Manager
- The apt-get or aptitude command line utilities.
- The Synaptic Package Manager
You can use the
ps command to find out what processes are running on your machine. We can pipe the output to
grepto filter out processes which might be using apt.
ps -e | grep -e apt -e adept | grep -v grep
By running the above command I get the following command input while my apt is locked:
chewett@yang:~$ ps -e | grep -e apt -e adept | grep -v grep
952 ? 00:00:00 apt.systemd.dai
2673 ? 00:00:00 aptd
The top line is showing the primary process that is blocking apt. The full name of the executable is
apt.systemd.daily. This is a process that runs every day to update your system packages. Typically this will be running after you have turned on your computer. If this is using apt you will typically just need to wait until the update process has finished. It is not recommended to try and stop this process.
There are ways to remove the lock from the apt database but I won’t describe them here. This is because 99.99% of the time this is a very bad idea.
Some of the information was taken from ask Ubuntu.