Symfony RuntimeException: Unable to create the cache directory
RuntimeException: Unable to create the cache directory (/var/www/app/var/cache/dev)Short answer
Symfony needs var/cache and var/log to be writable by the web server user and by the user who runs bin/console. The error means one of them cannot write there, usually because a root-run command created root-owned files. Find who owns var/, then grant both users access with setfacl, or run both as one user.
Fix it fast
1Find out which cause you have
Look at who owns var/ and which user runs the web server. A mismatch between the two is almost always the cause.
ls -ld var var/cache var/log ps axo user,comm | grep -E '[a]pache|[h]ttpd|[w]ww-data|[n]ginx|[p]hp-fpm' | grep -v root | head -32Match what you see, then jump to its fix
- 1
var/ is owned by your login user or root, with no write access for www-data, apache or nginxThe web server user cannot write to var/cacheFix - 2
The site worked until someone ran a bin/console command with sudo, after which var/cache was owned by rootConsole and web server run as different usersFix - 3
setfacl fails with Operation not supported on a network mountThe project sits on NFS, where setfacl does not workFix
- 1
What does the error look like?
RuntimeException: Unable to create the cache directory (/var/www/app/var/cache/dev)Unable to create the cache directory
Cause 1 of 3
The web server user cannot write to var/cache
1Check
Symfony's documentation requires var/cache writable by the web server user when the cache is filesystem-based, and var/log writable by both the web server user and the terminal user.
sudo -u www-data test -w var/cache && echo writable || echo NOT writable2Fix
Use ACLs, which Symfony recommends as the safest method on Linux. Replace www-data with your web server user if it differs.
HTTPDUSER=www-data
sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX var
sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX var3Verify
Clear the cache as your own user, then request a page in the browser.
APP_ENV=prod php bin/console cache:clearConfirmed by symfony.com
Cause 2 of 3
Console and web server run as different users
1Check
Files created by one user under var/ may not be writable by the other. Symfony's guide describes two fixes without ACLs: run PHP-FPM and the console as one user, or share a group and set a umask of 0002 on both sides.
find var -maxdepth 2 ! -user $(whoami) | head2Fix
Make the PHP-FPM pool run as the same user you deploy with, then take ownership of var/. Avoid running console commands with sudo.
# in the PHP-FPM pool file: user = deployer, group = deployer
sudo chown -R deployer:deployer var
sudo systemctl restart php8.3-fpm3Verify
List the cache folder and clear the cache without a permission error.
ls -l var/cache/prod/
APP_ENV=prod php bin/console cache:clearConfirmed by symfony.com
Cause 3 of 3
The project sits on NFS, where setfacl does not work
1Check
Symfony's guide notes that setfacl is not available on NFS mount points, so the ACL method cannot be used there.
df -T var2Fix
Use the shared-group method: add both users to one group, give var/ that group with the setgid bit, and set umask 0002 for the web server and the shell.
sudo groupadd symfony
sudo usermod -aG symfony www-data
sudo usermod -aG symfony $(whoami)
sudo chgrp -R symfony var
sudo find var -type d -exec chmod 2775 {} \;
sudo find var -type f -exec chmod 664 {} \;3Verify
Start a new session so the group applies, then clear the cache.
APP_ENV=prod php bin/console cache:clearConfirmed by symfony.com
How do I stop it coming back?
- Never run bin/console with sudo; run it as the deploy user.
- Set up var/ permissions once in your deploy script, not by hand on each server.
- Do not use chmod 777 or umask 0000 except as a last resort: Symfony warns it makes files writable by any user on the machine.
- Clear the cache as the user that serves the site at the end of each deploy, as the deployment guide describes.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fixBack end
CodeIgniter 4 cannot write its cache files
CodeIgniter\Cache\Exceptions\CacheException: Cache unable to write to /var/www/app/writable/cache/
CodeIgniter 4's file cache needs a cache directory that the web server can really write to.
2 causes, each with a fixBack end
CodeIgniter 3 loads the model but the property is undefined
The model name you are loading is the name of a resource that is already being used: user
CodeIgniter 3 expects a model file named like its class, with the first letter capitalised, for example User_model.php holding class User_model.
2 causes, each with a fixBack end
CodeIgniter 3 stops with "Your system folder path does not appear to be set correctly"
503 Service Unavailable
CodeIgniter 3's front controller resolves $system_path and then checks that the result is a directory.
3 causes, each with a fix