This error message is fairly common on any Ubuntu or any other Debian based Linux system.

The meaning of the Could not get lock message is rather simple. At the time you attempt to install new software or update your operating system, there is another software installation or software upgrade underway which runs in the background and was launched on another remote or local terminal by other user with administrative privileges.
E: Could not get lock /var/lib/apt/lists/lock – open (11: Resource temporarily unavailable)
Example of E: Could not get lock /var/lib/apt/lists/lock – open (11: Resource temporarily unavailable) error message on Ubuntu 18.04
Another rather destructive cause for other related below error messages:
E: Could not get lock /var/lib/apt/lists/lock – open (11: Resource temporarily unavailable)
E: Unable to lock directory /var/lib/apt/lists/ 
E: Could not get lock /var/lib/dpkg/lock – open (11: Resource temporarily unavailable) 
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?
is that the software installation or upgrade process using either apt or dpkg command was interrupted.

The way how package management tool on Ubuntu/Debian or any other Linux operating system works is that every time package installation or update is initiated, the package management tool, in this case apt or dpkg, creates a lock file /var/lib/apt/lists/lock or var/lib/dpkg/lock to prevent concurrent execution of another software installation or update process.



In this tutorial you will learn:
  • What is the most likely cause of E: Could not get lock /var/lib/apt/lists/lock on Ubuntu Linux system
  • How to avoid the could not get lock error message on Ubuntu Linux
  • How to find process holding a lock on /var/lib/apt/lists/lock
  • How to unlock the /var/lib/apt/lists/lock lock
  • How to recover from prematurely terminated apt or dpkg installation process

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu/Debian or any DEB based Linux Distribution
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

Could not get lock /var/lib/apt/lists/lock caused by background update process

On Ubuntu Linux systems the prevalent number of cases when the E: Could not get lock /var/lib/apt/lists/lock error message appears on Ubuntu Linux systems occurs because the package repository index update is executed automatically by the Ubuntu system itself in the background. This simply creates a lock file and prevents user to use the apt or dpkg tools at the same time.
SOLUTION
The simplest and the only recommended solution is to wait and let the background package management tool update finish its work and release the lock file.

How to avoid the "could not get lock" error message on Ubuntu Linux

Having the Ubuntu system trigger background update and preventing the user from using the apt or dpkg command can be very frustrating situation.

The solution is to stop automatic background system package list updates and run updates manually. To turn off automatic updates edit /etc/apt/apt.conf.d/20auto-upgrades using your favorite text editor:


$ sudo nano /etc/apt/apt.conf.d/20auto-upgrades
Once you have the file opened, switch off the Update-Package-Lists directive from 1 to 0 as shown below on Line 1:

APT::Periodic::Update-Package-Lists "0";
APT::Periodic::Unattended-Upgrade "1";

How to find process and user holding a lock

The following steps can be used to identify the user and the process holding a lock preventing user to successfully execute the apt or dpkg command:

  1. Take a note of the file lock that is being locked. For example:
    E: Could not get lock /var/lib/apt/lists/lock – open (11: Resource temporarily unavailable)
    E: Unable to lock directory /var/lib/apt/lists/ 
    
  2. Use the fuser command to find a process ID (PID) responsible for the lock:
    $ sudo fuser /var/lib/apt/lists/lock
     /var/lib/apt/lists/lock: 3384
    
  3. Based on the previously retrieved PID find the user and execute the command:
    $ ps -p 3384 -o user,comm,args
    USER     COMMAND         COMMAND
    root     apt             apt update
    
  4. Finding the user and process responsible for holding a lock on /var/lib/apt/lists/lock file
    Finding the user and process responsible for holding a lock on /var/lib/apt/lists/lock file on Ubuntu Linux system.

    How to unlock the "/var/lib/apt/lists/lock" lock

    In the scenario that you are able to identify the user and the process holding a file lock as per the previous section, investigate further and see if you can let the process finish gracefully.
    WARNING
    Do not forcefully remove lock file if there is a chance to let process finish gracefully. This my harm your system and in same cases even without the option for the recovery.
    In the event that the package management tool got stuck, hence, unable to finish and remove the lock file you may attempt to kill the process. For example this can be accomplished by:


    $ sudo fuser -vki /var/lib/apt/lists/lock
    [sudo] password for linuxconfig: 
                         USER        PID ACCESS COMMAND
    /var/lib/apt/lists/lock:
                         root       3384 F.... apt
    Kill process 3384 ? (y/N)
    
    or by using the kill command and previously retrieved PID eg. 3384:
    $ sudo kill -9 3384
    
    In case that you killed the process manually or are unable to find the process and user holding the lock, the chances are that the previously executed software installation or software upgrade exited prematurely without giving the package management tool the chance to remove the lock.

    In this scenario remove the lock file manually:
    $ sudo rm /var/lib/apt/lists/lock
    $ sudo rm /var/lib/dpkg/lock
    

    How to recover from prematurely terminated APT or DPKG installation process

    When the installation process is interrupted prematurely your system may be temporarily broken preventing you to perform any additional software installations resulting in an error similar to the one below:
    E: Sub-process /usr/bin/dpkg returned an error code (2)
    
    Try the following commands to recover the broken DPKG system:
    $ sudo dpkg --configure -a
    $ sudo apt install -f
    
    For an interrupted system upgrade execute:
    $ sudo apt upgrade --fix-broken
    
ARE YOU LOOKING FOR A LINUX JOB?
Submit your RESUME or create a JOB ALERT on LinuxCareers.com job portal.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in:



Comments and Discussions