Search code examples
symfonyenvironment-variablesenvironmentsymfony4production-environment

PHP Fatal error: Uncaught RuntimeException: APP_ENV environment variable is not defined


I am deploying Symfony 4 project on production. Created .env file with a content:

APP_ENV=prod
APP_DEBUG=0

Run composer:

$ composer install --no-dev --optimize-autoloader

Getting error:

Script cache:clear returned with error code 255
!!  PHP Fatal error:  Uncaught RuntimeException:
 APP_ENV environment variable is not defined.
 You need to define environment variables for
 configuration or add "symfony/dotenv" as a Composer
 dependency to load variables from a .env file. in 
/var/www/symfony4_project/bin/console:20
!!  Stack trace:
!!  #0 {main}
!!    thrown in /var/www/symfony4_project/bin/console on line 20
!!  
Script @auto-scripts was called via post-install-cmd

Symfony documentation about deployment and environment setting is very narrow and foggy.

It is not clear where I should set the production environments.

From https://symfony.com/doc/current/configuration.html

sentence "If you decide to set real environment variables on production, the .env files will not be loaded if Symfony detects that a real APP_ENV environment variable exists and is set to prod."

Where should I look for this real APP_ENV existence?

As I found on stackoverflow and github, there are many questions realated, but not so many answers.

Thanks for help.


Solution

  • you need to install symfony/dotenv composer package to load your .env file. Take a look to the official documentation https://symfony.com/doc/current/components/dotenv.html The Dotenv Component parses .env files to make environment variables accessible.

    Run composer require symfony/dotenv this should work.