Recently the most of my time are used Ubuntu 18.x and then I’ve an issue with Unable to lock the administration directory (/var/lib/dpkg/) so I came after searched on Google and I success to solve this matter. So I would like to share my thoughts how to fix this thing.
And this how to solve that issue
Find and Kill all apt-get or apt Processes
Run the command below to generate a list of all processes whose name comprises of the word apt, you will get a list inclusive of all apt or apt-get processes by using ps and grep commands together with a pipeline.
$ ps -A | grep apt
For each apt-get or apt process that you can see in the output of the command above, kill each process using the command below.
The process ID (PID) is found in the first column from the screenshot above.
sudo kill -SIGKILL -4576 OR sudo kill -9 4576
Delete the lock Files
lock file simply prevents access to another file(s) or some data on your Linux system, this concept is present in Windows and other operating systems as well.
Once you run an apt-get or apt command, a lock file is created under the any of these directories
This helps to avoid the apt-get or apt process that is already running from being interrupted by either a user or other system processes that would need to work with files being used by apt-get or apt. When the process has finished executing, the lock file is then deleted.
Important: In case a lock is still exiting in the two directories above with no noticeable apt-get or apt process running, this may mean the process was held for one reason or the other, therefore you need to delete the lock files in order to clear the error.
First execute the command below to remove the lock file in the
$ sudo rm /var/lib/dpkg/lock
Afterwards force package(s) to reconfigure like so:
$ sudo dpkg --configure -a
Alternatively, delete the lock files in the /var/lib/apt/lists/ and cache directory as below:
$ sudo rm /var/lib/apt/lists/lock $ sudo rm /var/cache/apt/archives/lock
Next, update your packages sources list as follows:
$ sudo apt update OR $ sudo apt-get update
In conclusion, we have walked through two important methods to deal with a common problem faced by Ubuntu (and its derivatives) users, while running
apt as well as aptitude commands.