Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Symfony RuntimeException: Unable to create the cache directory

ErrorRuntimeException: 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.

  • Applies to Symfony 7.4, PHP 8.3, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 -3
  2. 2Match what you see, then jump to its fix

    1. 1var/ 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. 2The 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. 3setfacl fails with Operation not supported on a network mountThe project sits on NFS, where setfacl does not workFix

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 writable

2Fix

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 var

3Verify

Clear the cache as your own user, then request a page in the browser.

APP_ENV=prod php bin/console cache:clear

Confirmed by symfony.com

Did this fix it?

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) | head

2Fix

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-fpm

3Verify

List the cache folder and clear the cache without a permission error.

ls -l var/cache/prod/
APP_ENV=prod php bin/console cache:clear

Confirmed by symfony.com

Did this fix it?

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 var

2Fix

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:clear

Confirmed by symfony.com

Did this fix it?

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

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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.