psysh: Error: mkdir(): Permission denied

I am using psysh as part of larvel’s tinker command, however after updating to the latest version (0.8.15) I am just getting the followin error:

In Configuration.php line 352:

  mkdir(): Permission denied

I am unable to get a stacktrace, so I have no clue how to debug the error, any ideas?

PHP Version info:

PHP 7.1.12-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Nov 29 2017 10:03:25) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.12-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 1
  • Comments: 16 (4 by maintainers)

Commits related to this issue

Most upvoted comments

This error occurs because you switch user (guessing using su) without “dash” option.

Replace su www-data with su - www-data and everything should works!

psysh tries to write runtime data in “original user” folder, instead of in “current user” folder…

PS my “current user” has XDG_RUNTIME_DIR env defined. Switching user without -, forced www-data to use the same env variable, while www-data hasn’t write permission in that folder.

I’m reposting my workaround from two days ago on issue #34 over at laravel/tinker here:


Had this for a while on a shared server but thanks to the posts by @ingria and @groovix I looked into it and a workaround (until someone knowledgeable fixes it) is to tell psysh to run in another directory where one has write access.

This can be done through either placing a config-file in ~/.config/psysh/config.php or as .psysh.php in the root of a Laravel-project (ref).

Here’s my sample config-file (note that the psysh wiki doesn’t mention runtimeDir as a config option but the source code lists it as a valid option):

// Temporary fix for issue #34 in laravel tinker where psysh
// tries to run in `/run/user/<uid>/psysh`.
//
// This overrides that behaviour to `.psysh/` inside the project
return [
	'runtimeDir'	=> './.psysh',
];

Can’t get this to work in a shared CloudLinux environment despite adding the suggested code. Any ideas?

[vividwelldev@orcus ~]$ php artisan tinker
// Temporary fix for issue #34 in laravel tinker where psysh
// tries to run in `/run/user/<uid>/psysh`.
//
// This overrides that behaviour to `.psysh/` inside the project

return [
        'runtimeDir'    => './temp/.psysh',
];
   ErrorException  : mkdir(): Permission denied

  at /home/vividwelldev/vendor/psy/psysh/src/Configuration.php:363
    359|             $this->runtimeDir = ConfigPaths::getRuntimeDir();
    360|         }
    361| 
    362|         if (!is_dir($this->runtimeDir)) {
  > 363|             mkdir($this->runtimeDir, 0700, true);
    364|         }
    365| 
    366|         return $this->runtimeDir;
    367|     }

  Exception trace:

  1   mkdir("/run/user/5559/psysh")
      /home/vividwelldev/vendor/psy/psysh/src/Configuration.php:363

  2   Psy\Configuration::getRuntimeDir()
      /home/vividwelldev/vendor/psy/psysh/src/Shell.php:198

  Please use the argument -v to see more details.

EDIT: Doh! Of course the config file needs an opening <?php tag. @TheBigK I wonder if this may fix for you too, or perhaps you are smarter than I. 😛 I’ll leave this here just in case anyone else has the issue.

@rvsth - When you are switching user; use su - USER instead of su USER. That - after su makes all the difference. Give it a try.

I ran into this on a shared hosting server running CloudLinux OS where my env was: env|grep RUNTIME XDG_RUNTIME_DIR=/run/user/1022 That directory didn’t exist so i exported XDG_RUNTIME_DIR in .bashrc instead: export XDG_RUNTIME_DIR=/tmp (note that in cloudlinux with cagefs /tmp doesnt mean the shared /tmp on the server but a directory in the users homedir.) After that it worked fine for me.

You can also set XDG_RUNTIME_DIR on the fly if you do not want to create an extra config file:

XDG_RUNTIME_DIR=../.psysh php artisan tinker

Or if you want to use a temp directory (though a new psysh environment is created each time):

XDG_RUNTIME_DIR=$(mktemp -d) php artisan tinker

I’m facing this issue as well. In my case, even adding

return [
	'runtimeDir'	=> './.psysh',
];

in the laravel root directory didn’t work. Would really appreciate a fix.

make sure you are making a php file, meaning dont forget to put “<?php” tag at the very beginning

A quick side note from today: When I added runtimeDir to the config options on the psysh wiki, I noticed that the runtimeDir could be overriden by specifying the environment variable XDG_RUNTIME_DIR. In my case with my shared server, it was set to /run/user/1011, which didn’t exist on and nor did my user have the neccessary permissions to create it either.

So I believe this was the culpit at least in my case.

Maybe a fix could be to catch the ‘Permission denied’ on Configuration.php:L352 and revert to using sys_get_temp_dir() as a fallback? This is already done in ConfigPaths.php:L149 so maybe could be moved to it’s own method for falling back on?

Just thinking out loud - take it with a grain of salt =).

@BafS That directory’s existence used to be necessary for PsySH to run, but at this point I believe it’s only used for the edit command. I just pushed a branch that (should) make a writable runtime directory optional, and the only weirdness (should) be if you try to use edit and your system can’t find a suitable fallback temp directory. Wanna give that branch a shot?

I’m facing this issue as well. In my case, even adding

return [
	'runtimeDir'	=> './.psysh',
];

in the laravel root directory didn’t work. Would really appreciate a fix.