Laravel cannot find the .env file on a new clone or server
file_get_contents(/var/www/app/.env): Failed to open stream: No such file or directoryShort answer
The .env file is not in version control, so a fresh clone or a new server does not have it. Copy .env.example to .env in the project root, set the values, and run the failing command again. If your team stores .env.encrypted instead, decrypt it with env:decrypt.
Fix it fast
1Find out which cause you have
Check whether the file exists in the project root, and whether an encrypted copy or an example file is there to start from.
ls -la /var/www/app | grep -E "\.env"2Match what you see, then jump to its fix
What does the error look like?
file_get_contents(/var/www/app/.env): Failed to open stream: No such file or directory
Cause 1 of 3
The project was cloned and .env was never created
1Check
Laravel's docs say a fresh install contains .env.example, copied to .env during installation, and that .env must not be committed to source control. A clone therefore never contains it.
ls -la .env .env.example2Fix
Copy the example file and edit the values for this machine.
cp .env.example .env3Verify
A Laravel command that reads the config now runs.
php artisan about --only=environmentConfirmed by laravel.com
Cause 2 of 3
The team keeps an encrypted environment file
1Check
The configuration docs describe env:encrypt, which writes .env.encrypted so it can be committed. The file must be decrypted on each machine.
ls -la .env.encrypted2Fix
Decrypt with the key the team keeps in a password manager.
php artisan env:decrypt --key=THE_KEY_FROM_YOUR_PASSWORD_MANAGER3Verify
.env now exists and the app boots.
php artisan about --only=environmentConfirmed by laravel.com
Cause 3 of 3
The deploy replaced the folder and the .env went with it
1Check
Each server needs its own .env, because it is not part of the repository. A deploy that swaps the whole directory removes the file unless it lives outside the release.
ls -la /var/www/app/.env /var/www/shared/.env2Fix
Keep one .env on the server outside the release folders and link it in during each deploy. The env path is an example: use your own layout.
ln -sfn /var/www/shared/.env /var/www/app/.env3Verify
Open the site and run an Artisan command.
php artisan about --only=environmentConfirmed by laravel.com
How do I stop it coming back?
- Add a 'create .env' step to the project README and to the server setup script.
- Keep .env.example current: every new variable goes into it with a placeholder.
- Never commit .env: Laravel's docs warn that it exposes credentials to anyone with repository access.
- After editing .env on a server that uses config:cache, run php artisan config:clear: the docs say the cached configuration ignores the file.
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
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack end
Why a Filament panel feels slow on a Laravel server
The panel is fast on a small demo and slow once there are many resources, pages and widgets
A slow Filament panel on a server is most often running without the production caches.
3 causes, each with a fixBack end
Intervention Image will not save the file in a Laravel app
Can't write image data to path (/var/www/app/public/uploads/photo.jpg)
Intervention Image writes straight to a file path.
3 causes, each with a fixBack end
Laravel file upload returns 413 behind nginx
413 Content Too Large
A 413 means a limit refused the request body before Laravel could use it.
3 causes, each with a fixBack end
Laravel says the bootstrap/cache directory must be present and writable
The /var/www/app/bootstrap/cache directory must be present and writable.
Laravel writes its package manifest into bootstrap/cache and throws when that folder is missing or the current user cannot write to it.
2 causes, each with a fixBack end
Carbon::now() and now() return the wrong hour in Laravel
created_at values do not match the local time
Carbon::now() uses the PHP default timezone, and Laravel sets that from app.timezone in config/app.php on every boot.
3 causes, each with a fix