Home » Php » php – RuntimeException: Unable to create the cache directory (/var/www/sonata/app/cache/dev)

php – RuntimeException: Unable to create the cache directory (/var/www/sonata/app/cache/dev)

Posted by: admin April 23, 2020 Leave a comment


I installed the Sonata admin bundle.
After installation i refresh my page there is the cache problem then i use the following command to remove the cache:

rm -rf app/cache app/log

Then I recreate the directory:

mkdir app/cache app/log

But I got the following error:

Runtime Exception : Unable to create the cache directory (/var/www/sonata/app/cache/dev).

How to&Answers:

It looks like a file/directory permission problem. The directory has to be writeable by the webserver. After creating the directory you should adjust the permissions with

chown -R www-data:www-data app/cache
chown -R www-data:www-data app/log

Or for Symfony 4+:

chown -R www-data:www-data var

This only works on linux systems. The user and group depends on your distribution. On Debian and Ubuntu this should be www-data, on CentOS it’s afaik apache.

Another solution would be not to delete the whole folders but only their contents via

$ rm -rf app/log/* app/cache/*

But please be careful with this command.


This solution is correct : https://stackoverflow.com/a/20128013/2400373

But is necessary change the 2 commands in Symfony3:
First you should is inside the folder of project:

$ sudo chown -R www-data:www-data var/cache
$ sudo chown -R www-data:www-data var/logs

After delete the cache:

$ sudo rm -rf var/cache/*
$ sudo rm -rf var/logs/* 



And for centos:

chown -R apache:apache app/cache

if you’re coming here for Symfony Help you might have to do this as well if you delete the entire app/logs folder

chown -R apache:apache app/logs


I solved it changing user and group of folder var/cache and var/logs, then I cleaned cache:

sudo chown -R www-data:www-data var/logs
sudo chown -R www-data:www-data var/cache

sudo rm -rf var/logs/* var/cache/*


It is mostly the permission issue.

I have got resolved it on MAC using command: $ sudo chmod -R 777 <path/to/cache/directory>

You might try: $ sudo chmod -R 777 /var/www/sonata/app/cache


Changing the CHMOD might help but in case the cache annoys you during the deveopment you can just deactivate it.
Navigate to your app config file (located in ../app/config/config.yml from your root directory). Scroll to the twig configuration settings (under twig:) and change the cache value (which should be pointing to the cache directory) to false like so:

    cache:  false

If you do not see any cache configuration entry, simply add the line above.


I found a similar problem when I was using the PHP symfony as the error picture.
I found it after running command

php bin/console cache:clear

I solved it by removed everything inside the folder /app/var/cache

enter image description here


What is likely happening is that you are trying to create the file under apache/nginx. By default apache or nginx has umask set to 0022.

From: http://www.cyberciti.biz/tips/understanding-linux-unix-umask-value-usage.html

Explain Octal umask Mode 022 And 002

As I said earlier, if the default settings are not changed, files are created with the access mode 666 and directories with 777. In this example:

The default umask 002 used for normal user. With this mask default directory permissions are 775 and default file permissions are 664.
The default umask for the root user is 022 result into default directory permissions are 755 and default file permissions are 644.
For directories, the base permissions are (rwxrwxrwx) 0777 and for files they are 0666 (rw-rw-rw).

You will need to manually set the umask to 0002, and reset it back to its previous setting before you can create the directories.


On a mac computer it will be:

$ sudo chown -R _www:_www var/cache
$ sudo chown -R _www:_www var/logs

If none of above work to you, call a “phpinfo()” on your php file and find for “User/Group” value. That’s the user group to give permission to.


Also check the path. In my case, I had

return dirname(__DIR__) . '../../../../var/cache/

instead of

return dirname(__DIR__) . '/../../../../var/cache/ (missing /)


to generalize so everyone can use this two commands can solve everyone problem.

sudo chown -R $USER var/cache
sudo chown -R $USER var/logs

Further Info: $USER automatically replaces your username so it can be used by anyone.