Maaz Zindani

Loading the index…

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

Laravel cannot find the .env file on a new clone or server

Errorfile_get_contents(/var/www/app/.env): Failed to open stream: No such file or directory

Short 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.

  • Applies to Laravel 12, PHP 8.3, Composer 2, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

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

    1. 1ls shows .env.example but no .envThe project was cloned and .env was never createdFix
    2. 2ls shows .env.encrypted and no .envThe team keeps an encrypted environment fileFix
    3. 3The site worked before a deployment and fails right after itThe deploy replaced the folder and the .env went with itFix

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.example

2Fix

Copy the example file and edit the values for this machine.

cp .env.example .env

3Verify

A Laravel command that reads the config now runs.

php artisan about --only=environment

Confirmed by laravel.com

Did this fix it?

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.encrypted

2Fix

Decrypt with the key the team keeps in a password manager.

php artisan env:decrypt --key=THE_KEY_FROM_YOUR_PASSWORD_MANAGER

3Verify

.env now exists and the app boots.

php artisan about --only=environment

Confirmed by laravel.com

Did this fix it?

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/.env

2Fix

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/.env

3Verify

Open the site and run an Artisan command.

php artisan about --only=environment

Confirmed by laravel.com

Did this fix it?

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

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 fix

Next step

Want this handled for good?

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