Reputation: 37
I'm trying to deploy a symfony app to heroku but after the command "git push heroku master" i got this error
*remote: Script cache:clear returned with error code 255
remote: !! 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 /tmp/build_c283120b0d4ffbcbe11960331dfe8069/bin/console:20
remote: !! Stack trace:
remote: !! #0 {main} remote: !! thrown in /tmp/build_c283120b0d4ffbcbe11960331dfe8069/bin/console on line 20 remote: !! remote: Script @auto-scripts was called via post-install-cmd remote: ! WARNING: The APP_ENV environment variable is missing remote: ! Run 'heroku config:set APP_ENV=prod' to set it.
remote: remote: ! ERROR: Dependency installation failed!
remote: ! remote: ! The 'composer install' process failed with an error. The cause remote: ! may be the download or installation of packages, or a pre- or remote: ! post-install hook (e.g. a 'post-install-cmd' item in 'scripts') remote: ! in your 'composer.json'.
remote: ! remote: ! Typical error cases are out-of-date or missing parts of code, remote: ! timeouts when making external connections, or memory limits.
remote: ! remote: ! Check the above error output closely to determine the cause of remote: ! the problem, ensure the code you're pushing is functioning remote: ! properly, and that all local changes are committed correctly.
remote: ! remote: ! For more information on builds for PHP on Heroku, refer to remote: !
https://devcenter.heroku.com/articles/php-supportremote: ! remote: ! REMINDER: the following warnings were emitted during the build; remote: ! check the details above, as they may be related to this error: remote: ! - The APP_ENV environment variable is missing
remote: remote: ! Push rejected, failed to compile PHP app.
remote: remote: ! Push failed remote: Verifying deploy...*
I changed the line APP_ENV=dev to APP_ENV=prod on .env file but nothing changed... any ideas? How do I add "symfony/dotenv" as a Composer dependency?
Upvotes: 1
Views: 4512
Reputation: 635
Just to add another perspective... Heroku specifically recommends NOT using symfony/dotenv
... so the right answer to this question is simple: you don't.
Practically, using a .env
file reduces your flexibility, since putting your environment variables in your code means you need to push a new build if you want to change them. And don't forget the fact that you're putting things that are usually supposed to be secrt in your codebase, which is a pretty big no-no from a security perspective (here's one of many articles on the topic).
Here is Heroku's latest article on the topic, Deploying Symfony 4 Apps on Heroku. The note at the end of the Environment Variables section says:
It is recommended that you do not use the
symfony/dotenv
package in production, but instead set all required environment variables explicitly usingheroku config
. Movingsymfony/dotenv
from require torequire-dev
in yourcomposer.json
will ensure that.env
files from your application root aren’t picked up in the first place. This corresponds to option #2 in post-deploy step B of the Symfony documentation.
For reference, the linked Symfony docs say this:
Most Symfony applications read their configuration from environment variables. While developing locally, you'll usually store these in .env and .env.local (for local overrides). On production, you have two options:
- Create "real" environment variables. How you set environment variables, depends on your setup: they can be set at the command line, in your Nginx configuration, or via other methods provided by your hosting service.
- Or, create a
.env.local
file just like your local development.
On Heroku, the way to implement #1 is to use Heroku-managed environment variables (either using the CLI or their web interface).
Upvotes: 1
Reputation: 52483
The error message is pretty clear about this:
You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.
It ain't enough to just create a .env
file! You must also add symfony/dotenv
as a composer dependency for symfony to actually use the environment variables defined in the .env
file.
Run the following command and re-deploy your application to fix your issue:
composer require symfony/dotenv
Another way to fix the issue is to configure Heroku to provide the APP_ENV
environment variable when running your application.
heroku config:set APP_ENV=prod
Upvotes: 1