Search code examples
phplaravellaravel-artisan

Failed to open stream: .env


I want to start a Laravel application, but when running composer install, it can not find the .env file. Nor does it create one.

I thought it was a path url problem, because there are slashes and the backslashes provided in the path url.

Here is the output:

Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postInstall

> php artisan key:generate

Gives error:

[ErrorException] file_get_contents(C:\xampp\htdocs\tekom-web/.env): failed to open stream: No such file or directory

Script php artisan key:generate handling the post-install-cmd event returned with error code 1


Solution

  • .env files are gitignored by default and not included in version control due to many reasons, including security of project and API keys. Therefore, when you clone a Laravel application, you won't have one by default and you can not use an artisan command to create one.

    The best solution is to copy the .env.example and rename it to .env, then use the artisan key:generate command and it should work! The reason I suggest copying the example .env file is that, if you cloned this project, developers often use the example env and include all necessary environment variables right out of the box. By copying it, you ensure consistency with the project requirements.

    Hope this helps!