How to fix the «Can't create directory 'generated/code' in Magento/Framework/Code/Generator.php» Magento 2.2 issue?

A post was split to a new topic: How to fix “Command line user does not have read and write permissions on generated directory. Please address this issue before using Magento command line”