6

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.

2
  • After deployment some more errors appeared. And Commented Dec 9, 2018 at 14:40
  • I deploed project and couldn't access it, i found, that symfony 4 needs apache2 bundle to run: symfony.com/doc/current/setup/web_server_configuration.html Commented Dec 9, 2018 at 14:43

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Happy to help, could you vote and confirm my anwser please
Ahmed, I woted your answer. I need 15 points reputation, to make it visible. Thanks, one more time.
@fama23 is --dev required? original question is about prod deployment!
Nah, not required, I gonna remove from the answer. Dotenv should be installed for all the nev. Thanks
1

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.

Comments

0

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.

1 Comment

It is Symfony4. I don't know how to add dotenv dependency to the composer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.