The error:
InvalidArgumentException Source directory /tmp does not exist or is not a directory.
indicates that Laravel (or one of its dependencies) is trying to access the /tmp directory, but it either doesn't exist or your PHP process doesn't have permission to use it.
Why does it happen in one project but not the other?
- Different projects may use different PHP versions or configurations.
- One project may have a different
.env or cache state.
- File/directory permissions may differ between projects.
How to resolve:
-
Check if /tmp exists:
In your terminal, run:
ls -ld /tmp
If it does not exist, create it:
sudo mkdir /tmp
sudo chmod 1777 /tmp
-
Check permissions:
The /tmp directory should be world-writable and have the sticky bit set (mode 1777). If not, fix it:
sudo chmod 1777 /tmp
-
Check PHP configuration:
Run:
php -i | grep 'sys_temp_dir'
If sys_temp_dir is set to a non-existent directory, either create that directory or update your php.ini to point to a valid one.
-
Check your project’s cache:
Sometimes, cached files can cause issues. Clear Laravel’s caches:
php artisan config:clear
php artisan cache:clear
php artisan view:clear
php artisan route:clear
-
Check for custom factory paths:
If you have customized your factory paths or are using packages that do, ensure those directories exist and are writable.
-
Restart your terminal/IDE:
Sometimes, especially when using PHPStorm or Herd, restarting your IDE or terminal can help reload environment variables and permissions.
Summary:
This is almost always a system-level issue with the /tmp directory. Ensuring it exists and has the correct permissions should resolve the error.
Example Fix:
sudo mkdir -p /tmp
sudo chmod 1777 /tmp
After this, try running your factory command in Tinker again:
php artisan tinker
>>> \App\Models\YourModel::factory()->create();
If the error persists, double-check your PHP configuration and project-specific settings.