Search code examples
symfonycomposer-phpenvironment-variablessymfony-console

Composer scripts disregards Symfony console environment variable


I am facing an issue where the build process of my Symfony application fails, as explained in many different forums (none recent). For instance:

When doing composer install with --no-dev only the production packages are installed (as it should be). However when composer launches the symfony scripts they are launched in development modes and thus start throwing errors when AppKernel.php can not find packages needed for development. These scripts should also be executed in production mode!

Quote source

The above Github issue is closed, as it apparently can be fixed by setting the SYMFONY_ENV env variable to the environment of your choice: export SYMFONY_ENV=prod.

I have done that, and calling printenv correctly prints the variable. I can even call the Symfony console php bin/console manually. and it will launch in prod environment.

Success? Seemingly, but no... Calling composer install still forces all scripts to launch in dev environment. What gives?


Solution

  • The root cause appears to be the environment variable not being set globally, so whatever user the commands ended up running as, www-data via my deploy script, or root(as I called sudo composer?) from the console, did not have the environment variable set that I created with my default user.

    Adding SYMFONY_ENV=prod on a new line in /etc/environment, and then restarting fixed the issue, and the scripts are now running in prod.