How to fix Unable to Lock Directory /var/lib/apt/lists/ in Ubuntu?

Many are upset and are looking for how to fix unable to lock directory /var/lib/apt/lists/ in Ubuntu as they have been faced with this issue and find out here how to fix unable to lock directory /var/lib/apt/lists/ in Ubuntu and more about this error.

by Janani | Updated Mar 21, 2023

Fresherslive

What is unable to lock directory /var/lib/apt/lists/ error on Ubuntu?

The "unable to lock directory /var/lib/apt/lists/" error on Ubuntu typically occurs when the APT package management system is already running or has crashed. APT is a command-line tool used for managing packages on Ubuntu and other Debian-based Linux distributions.

This error message usually appears when you try to run an update or install command using APT, and it indicates that the APT system is currently locked and cannot be accessed. This can happen if another process or command is using APT, such as another update or installation command that is already running in the background.

How to fix unable to lock directory /var/lib/apt/lists/ in Ubuntu?

1. Reboot the system

You could reboot the system to fix the error. Once, you see the error, wait for some time with patience. After that, try to install your software again. Make sure that nothing is being installed, then you could reboot the machine and try again.

You can adjust the settings for automatic updates, using the below steps:

  • First, tap Activities and then Search.
  • Next, type Updates.
  • Now, tap the icon for Software & Updates.
  • Then, with the help of the checkboxes and drop-down menus choose your preferred update schedule.

2. Check the Running Processes

  • You could use the below command to check which installation services are running.

             ps aux | grep -i apt

  • If there are any applications using apt, they might be shown on the list. So, see for an entry similar to:

             /usr/lib/apt/apt.systemd.daily update

  • If the daily update appears then your system is running normal updates. Then, you must wait for this process to complete,  but no other action is needed. You might have another apt process that runs. 

             root    2810  0.0    0.0    72948 4312  pts/0  S+ 15:03  0:00   sudo apt-get remove logstash

  • The second column, tells the PID (process ID), and the last column shows the service that’s using apt.
  • Then,  you may have a dpkg service that runs. To locate it, use the below command:

              ps aux | grep -i dpkg

  • The dpkg output will be the same as the apt output.

3. Provide Address to the Stuck Apt Service

When you see a mystery apt or dpkg service running, end it and check whether that resolves your error. For that, use the below command.

        sudo kill 8808

Now, replace 8808 with the actual process ID (PID). It must finish and come back to a new command prompt line. If it that doesn’t happen, then just end the action by including the -9 option. 

         sudo kill -9 8808

4. Erase Lock Files 

The error in Ubuntu may be displayed below: 

       /var/lib/dpkg/lock

       /var/lib/dpkg/lock-frontend

       /var/lib/apt/lists/lock

       /var/cache/apt/archives/lock

These are lock files, which could prevent two instances of apt or dpkg from using the same files simultaneously. 

This could occur if an installation is needed or did not finish. Just remove the lock files.

To delete or erase the lock files, use the rm command:

        sudo rm /var/lib/dpkg/lock

        sudo rm /var/lib/apt/lists/lock

        sudo rm /var/cache/apt/archives/lock

5. Reconfigure dpkg

  • You could run the --configure command after you have deleted the lock files.
  • Type the command as shown below:

           sudo dpkg --configure -a

  • This clears the error code. 

Cause of unable to lock directory /var/lib/apt/lists/ error on Ubuntu

The error "unable to lock directory /var/lib/apt/lists/" in Ubuntu usually occurs when another process is using the APT package management system. This could be due to one of the following reasons:

  • Another package manager is running: If another package manager, such as Synaptic or the Ubuntu Software Center, is running, it will lock the APT system preventing other processes from using it.

  • Another APT process is running: If another APT process, such as an update or upgrade process, is running in the background, it will also lock the APT system.

  • Insufficient permissions: If you do not have sufficient permissions to access the APT system, you may encounter this error.

Disclaimer: The above information is for general informational purposes only. All information on the Site is provided in good faith, however we make no representation or warranty of any kind, express or implied, regarding the accuracy, adequacy, validity, reliability, availability or completeness of any information on the Site.

How to fix unable to lock directory /var/lib/apt/lists/ in Ubuntu - FAQs

1. What is directory?

A directory is a unique type of file that contains only the information needed to access files or other directories. 

2. Where is the computer directory?

This button can be found in the lower-left corner of the screen, and may just be a Windows logo. Click the Computer or File Explorer button. In Windows 10, this looks like a folder and can be found on the left side of the menu, or in your Windows task bar at the bottom of the screen.

3. What is files and directory?

A file is a named collection of data that you can move around, copy, rename, or delete. Files are stored in a filesystem, a storage area on your computer's hard disk or disks. A filesystem is split into directories, which are smaller storage areas that make it easier to locate individual files.

4. Is the directory and folder the same?  

Like a directory, a folder is a container for organizing digital data. Unlike a directory, however, it can only store files. While a directory is a hierarchical system that allows organizing data on different levels, a folder simply organizes data into one level

5. Why is it called a directory?  

The term directory refers to the way a structured list of document files and folders is stored on the computer. It is comparable to a telephone directory that contains lists of names, numbers and addresses and does not contain the actual documents themselves.