Missing write permissions to the following directories: ‘pub/media’ in Magento2:

To do the database schema and data changes in magento, we have to execute following  command in your terminal

php bin/magento setup:upgrade

When you execute this command in magento, there is a possibility to face following error “Missing write permissions to the following directories: ‘pub/media’” or “pub/static“. Further you couldn’t continue the execution.

Reason : As per my understanding following are the reason for getting these error.

  1. Write permission is missing for var & pub
  2. symlinks” is missing for the pub/static , pub/media or var/cache/* When run magento with Elastic/Network File System . Magento will checks the permission of the actual file instead of symlinked file during setup:upgrade. Ex: Actual file is having enough write permission but not symlink file.

Following magento file is checking the permission of the folder during a run time. Below  is the file class (only file path) playing major role when you execute the upgrade command on terminal.

vendor\magento\magento2-base\setup\src\Magento\Setup\Console\Command\UpgradeCommand.php
Go to : vendor\magento\magento2-base\setup\src\Magento\Setup\Model\Installer.php and refer checkInstallationFilePermissions() method. Following method is checking the folder permission during the execution time.

Following are the major methods in that file to verity the directories are having a writable or not.

vendor\magento\framework\Setup\FilePermissions.php

Method : 1


Method : 2

———————-  Solution : 1 ————————–
Check Write Permission : Either Write permission is missing (When you use single instance/user) Using following command check your folders and sub-folders are having enough write permission or not.

ls -la <M2 root>/htdocs/var  or sudo find <M2 root>/var/ -type f -exec chmod 664 {} \; sudo find <M2 root>/var/ -type d -exec chmod 775 {} \;

If folder is doesn’t have enough permission then set write permission to that folder and do following steps.

  • remove pub/static
  • remove cache/* & page_cache/* folders
  • Run php bin/magento s:s:d
  • Cache clean/flush

———————-  Solution : 2 —————————

  • We can exclude the “MEDIA” is_writable check when we run the media folder with Elastic/Network File System.
  • If we exclude the required directory (Ex: Media folder)from the recursive check then the application will return the successful result when you run the upgrade command.

———————-  Solution : 3 —————————

You comment out below method script before run the setup upgrade. Once after successful execution of the command revert back to the actual script.
THIS IS NOT SUGGESTED SOLUTION BUT YES YOU CAN DO THIS CHANGE WHEN THIS RUN YOUR WEBSITE ON DEFAULT OR DEVELOPER MODE.

For more Info:
https://www.maxpronko.com/improving-performance-when-upgrading-magento-2-with-elastic-file-system/
https://alanstorm.com/missing-write-permission-during-binmagento/

Save WATER !!! It will save you LATER!!!