Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
You’re not wrong but you are possibly confused. I was too confused encountering similar problems.
TLDR:
The lock is being released between apt-get update and apt-get install commands. That the opposite may appear true is a red herring.
Another process (likely the digitalocean local agent) is installing packages by running apt-get install.
Use apt install instead of apt-get install. The former waits for the lock to be released.
Detailed answer: According to an answer on a related question “DigitalOcean’s local agent is also doing an apt-get update right after the droplet comes alive”.
If the digitalocean local agent is running apt-get <cmd> at the same time that you are running apt-get <cmd> (as I think was the case for me), you may think that the error relates to your apt-get process whereas it may not. This was the source of my confusion.
Solution (of sorts …):
Use apt install instead of apt-get install. The former waits for the lock on /var/lib/dpkg/lock-frontend to be released.
Troubleshooting help:
Here is an example of how I helped myself figure out what was going on and helped me realised that it was not my apt-get process that was being referred to in the error message.
# Install some packages
sudo apt install -y <packages>
# Output the PID of this script
echo $$
# What apt-ish processing are running?
sudo ps aux | grep -i apt
Running echo $$ shows our script’s PID. The ps output for the process running apt-get install will show a different PID.
And on close inspection, the currently-running apt-get install command that you will see listed will very likely not match your own apt-get install command (unless by extreme coincidence).
I tried switching from apt-get install to apt install purely to be sure that if apt-get install is seen to be running then I know it is not of my doing. By sheer luck this also highlighted a lovely workaround which I’m happy with.