Mantaz
Mantaz

Reputation: 480

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.

Upvotes: 5

Views: 9954

Answers (3)

famas23
famas23

Reputation: 2280

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.

Upvotes: 4

Arthur Bouchard
Arthur Bouchard

Reputation: 485

If you've got this error in prod since Flex 1.2 in 2019 and you don't want to use .env file but the environment variables of your system :

It's because of changes : https://symfony.com/blog/new-in-symfony-flex-1-2 : Please read Improved the handling of .env files on blog

In order :

  1. You have to push your .env on git, but only this .env file
  2. Developpers will create .env.local to override .env file for dev
  3. symfony/dotenv is in require-dev section of composer.json
  4. When you want to deploy :

    1. Get the git repo (without /vendors, of course)
    2. Run composer dump-env prod --empty to generate a .env.local.php file
    3. Install your dependencies via composer install --no-dev [Other prod args]

You can now enjoy your prod env without symfony/dotenv composant installed.

Upvotes: 1

Sebastian
Sebastian

Reputation: 70

I need a little bit more information to solve your problem.

First of all - just to be sure: did you add the dotenv dependency to your composer as hinted in your debug message: composer require symfony/dotenv

Also, what kind of symfony installation is it? Just a basic symfony/skeleton? In generall if you just installed a basic symfony/sekleton or website-skeleton changing the value of APP_ENV in .env should work! But we'll work that out.

Please verify that you installed dotenv and provide the information I am looking for.

Upvotes: 0

Related Questions