It might be related to automatic updates.
I did a Debian install for a friend, they wanted the standard Gnome3 desktop, so I used the full Debian live installer, rather than my usual Debian minimal.
And after installing - my friend reported that they sometimes had this problem whilst trying to install new programs. Sometimes they could install - other times they got errors about dpkg being locked.
And the problem turned out to be related to the automatic updates.
They had tweaked their system to check for updates multiple times per day.
If they decided to try to manually install something whilst the automatic updates were running in the background, they'd get error messages about dpkg being locked.
In which case, the solution is simply to wait until the updates have finished before trying to install/uninstall anything.
Personally, I disable ALL automatic updates and manually update via a bash script I have in my personal bin directory.
As a developer, I pretty much live in the terminal when I'm using Linux. So typically, I'll run my update script manually when I first log-in, so I can see when the updates have finished (if there even are any) before I try to install anything new.
So I'm guessing that the OP may be having this issue. Automatic updates are occurring in the background and are blocking them from being able to use apt.
Failing that, this also sometimes happens if you try installing some new packages and you kill the process before all of the new packages have been fully installed and configured by dpkg.
If you accidentally kill the installation process before it's finished - dpkg will have set up some lock files.
Typically the following ones:
/var/lib/dpkg/lock
/var/lib/apt/lists/lock
/var/cache/apt/archives/lock
In that scenario, you will need to:
Step 1:
Ensure that any processes that are still using the lock files are terminated.
You can find out the PID's of any processes that are still using the lock files using the following commands:
Bash:
sudo lsof /var/lib/dpkg/lock
sudo lsof /var/lib/apt/lists/lock
sudo lsof /var/cache/apt/archives/lock
If lsof tells you that some processes have any of those files open, proceed to step 2. to kill them.
Otherwise go to step 3.
Step 2:
Once you have identified the PIDs of any processes using those files, you can kill each one using:
NOTE: Replace {PID} in the above with the PID number of the process that has the files open. So if lsof says that process 2125 has the lock-files open, you would use:
Again 2125 is just an example, replace 2125 with whatever PID was in the output from your lsof commands.
Also note the -15 parameter in the above specifies to send signal15 to the process.
Signal 15 is SIGTERM, which tells the process to cleanly shut itself down - so there is less risk of loss, or corruption of data.
2.a.
Next you need to verify that the process with PID 2125 has closed using:
That should tell you that the process has been terminated.
2.b.
If the process has
not been terminated, then you can issue the following command:
This sends signal 9 (SIGKILL) which will force the process to immediately shut down. Generally speaking, it's good practice to try killing processes with -15 (SIGTERM) first and then only use signal -9 (SIGKILL) when you absolutely have to.
Repeat the above steps until ALL processes using those lock files are killed. Typically there will only be a single process that has these files open, but you never know!!
Step 3:
Manually remove the lock-files:
Bash:
sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock
Step 4:
Run the following command to ensure that any partially installed/configured packages are installed cleanly:
Once that completes, you should be able to install/remove new packages using apt in the terminal, or the software centre (or whatever it's called this week!) in the GUI.