Too many open files

While running apt commands or restarting the service,  getting a Failed to allocate directory watch: Too many open files error.

You can see the current limits with sysctl fs.inotify. On debian , with the defaults, these are as follows:

fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 128
fs.inotify.max_user_watches = 65536


To fix the problem,  create a conf file /etc/sysctl.d/60-fs-inotify.conf with the following contents:

fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 512
fs.inotify.max_user_watches = 524288


To reload the new settings, you need to run :

sudo sysctl --system