Zend certified PHP/Magento developer

Magento 2 – The requested class did not generate properly, because the ‘generated’ directory permission is read-only (Even though folder has 777)

I get “The requested class did not generate properly, because the ‘generated’ directory permission is read-only” during setup:di:compile even though the folder has 777 permission.

I even tried with sudo, but I get the same error.