My thoughts on development, software, and more.

Symfony environment variables

May 13, 2017

This post may be outdated due to the release of the Dotenv component. See this blog post for more information.

Environment variables are increasingly popular to manage your application configuration. They are one of the main concepts of the twelve-factor app methodology. Their main advantages are that they can be changed between deploys without changing any code and that they don’t need to be checked into the code repository.

Since Symfony 3.2 there is full support for environment variables and in Symfony 3.3 the old SYMFONY__ variables are deprecated and will not be treated special anymore by Symfony in 4.0. The old SYMFONY__ variables behavior was simple, when the application container was built, the values of those environment variables were dumped into the compiled container. Therefore, if those variables changed during the application execution, the updated values were ignored.

Today we are going to change the Symfony Standard Edition and add environment variables to them. Lets start by downloading a new Symfony Standard Edition via the Symfony Installer (The Symfony Installer is a tool to easily install a new symfony codebase within seconds. see the documentation for more information.):

$ symfony new demo Downloading Symfony... 5.5 MiB/5.5 MiB ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ 100% Preparing project... ✔ Symfony 3.3 was successfully installed. Now you can: * Change your current directory to /Users/Ricardo/Symfony * Configure your application in app/config/parameters.yml file. * Run your application: 1. Execute the php bin/console server:start command. 2. Browse to the http://localhost:8000 URL. * Read the documentation at []

Directly go in to the newly created directory and execute the following commands:

$ cd demo $ rm -f app/config/parameter*

Don’t be scared by removing these parameter files, we are going to use the environment variables these files are not longer needed. Do not forget to remove /app/config/parameters.yml from .gitignore.

Next up we need to add several environment variables to our development machine. The best location for environment variables in development is in ~/.bash_profile. For production I advise you to use /etc/environment so that even deployment scripts can access these variables if they are used with another user on that production server.

Add the following lines and adjust them if needed:

export SYMFONY_ENV="dev" export SYMFONY_SECRET="2134bd98ff78a6950de9cb02d3fb958c4ae450d2" export SYMFONY_LOCALE="en" export SYMFONY_DB_HOST="" export SYMFONY_DB_PORT="3306" export SYMFONY_DB_NAME="symfony" export SYMFONY_DB_USER="root" export SYMFONY_DB_PASS="password"

Close your terminal completly and re-open it to activate your variables. You can test if they work by issuing:


Then we need to edit our app/config/config.yml file. First we need to remove the import of the parameter file. Lets edit the values of all the others. For environment variables, you need to use '%env(PARAMETER)%' to grab and use the environment variables in Symfony. For example, to activate the doctrine hostname variable we need to change host: '%database_host%' to host: '%env(SYMFONY_DB_PORT)%'. Change all the variables accordingly to their environment variable name.

Now we need to edit our composer.json file to remove some command involving the parameter files. Look at line 42 and remove that line. Also remove line 66, 67 and 68. When you removed those lines, it is time to remove a composer package from the installation. This package takes care of the parameter files but since we removed them, the package is obsolete for our project:

$ composer remove incenteev/composer-parameter-handler

If anything went well, you did not recieve any error. Congratulations, you are now using environment variables instead of a parameter file. See you next time.

Want to leave a comment? Visit this page on GitHub (You'll need a GitHub account)