Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rseletsk's avatar

Executing app resulting from app:build results in a runtime error

Executing app resulting from app:build results in a runtime error: PHP Fatal error: Uncaught Dotenv\Exception\InvalidPathException: Unable to read any of the environment file(s) at ... bootstrap/../.env].

1 like
11 replies
LaryAI's avatar
Level 58

Certainly! Here is a solution for the issue described in the Laracasts post:


Problem Description:
After building your Laravel app (using something like php artisan app:build, e.g. with Laravel Zero), when you execute the built app, you receive the error:

PHP Fatal error:  Uncaught Dotenv\Exception\InvalidPathException: Unable to read any of the environment file(s) at ... bootstrap/../.env

Solution:

The error indicates that, when running the built application, it is trying to load the .env file but cannot find it at the expected location. This usually happens because:

  • The .env file is not present in the directory from which you're running the built application.
  • The built app is packaged, but the working directory or expected path for .env is incorrect.

How to Fix:

  1. Ensure there is a .env file available where the application expects it. This is usually in the same directory where you run the built app, or at bootstrap/../.env relative to when it's run.

  2. Copy the .env file to the correct place, for example:

    cp .env.dist .env
    

    Or manually ensure a .env file is present next to your built executable or where it expects to find it.

  3. Alternatively, if your built package is deployable (e.g., a PHAR/executable), and you want to support running it from anywhere, you can either:

    • Instruct users to place their .env file next to the executable, or
    • Modify your code to look for .env in the current directory or in a configurable place.

Example (Laravel Zero):

If you want your Laravel Zero app to not throw an error when .env is missing (i.e., optional env), you can update your app.php (or bootstrap/app.php) to make Dotenv loading optional:

// In bootstrap/app.php or similar
if (file_exists(__DIR__ . '/../.env')) {
    Dotenv\Dotenv::createImmutable(base_path())->safeLoad();
}

or, with Laravel built-in functionality:

$app->loadEnvironmentFrom('.env');

(But make sure .env exists.)

Extra:
If you are distributing a built app to others, include in your README/deployment instructions that users should copy and configure their own .env from your provided .env.example or similar file.


Summary:
Copy your .env file to the location expected by your built application. If it's not required, update your bootstrap to not throw on missing files.


Let me know if you need more help!

1 like
Glukinho's avatar

What is app:build? This command is not built-in, did you write it?

1 like
vincent15000's avatar

Give more details about your problem, please ... because app:build doesn't exist, so you probably have installed a package or you wrote it yourself.

rseletsk's avatar

php appname app:build -vvv results in runtime error: [_HumbugBox6ab58bf96c33\KevinGH\Box\Composer\Throwable\UndetectableComposerVersion] Could not detect the Composer version: The command "'/usr/local/bin/compose r' '--version' '--no-ansi'" failed.

Exit Code: 1(General error)

Working directory: /home/whatever

Output:

PHP's phar extension is missing. Composer requires it to run. Enable the ex tension or recompile php without --disable-phar then try again.

1 like
rseletsk's avatar

On local laptop (WSL): php appname app:build does work are generates the deployed executable appname under the builds directory. But, when execute appname (via ./appname) get: PHP Fatal error: Uncaught Dotenv\Exception\InvalidPathException: Unable to read any of the environment file(s) at ... bootstrap/../.env].

1 like
rseletsk's avatar

On local laptop (WSL) the appname above the builds directory executes without any runtime error

1 like
rseletsk's avatar

On remote serverthe appname above the builds directory executes without any runtime error

1 like
rseletsk's avatar

Is best practice to deploy the application to production with app:build and use the production executable?

1 like
vincent15000's avatar

You don't answer the question : where does the app:build command come from ?

rseletsk's avatar

From created a Laravel CLI app via laravel-zero new applicationName

Please or to participate in this conversation.