What's the correct way to set ENV variables in Laravel 5?
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: The Correct Way to Set ENV Variables in Laravel 5 - A Comprehensive Guide
Introduction:
Laravel 5 has brought significant changes in how we deal with environment variables, making it much more flexible and efficient compared to previous versions. This blog post aims to provide a detailed explanation of these changes, as well as offer best practices for managing your application's environment variables.
Differences between Laravel 4 and 5:
In Laravel 4, the `detectEnvironment()` method was used to determine the current environment by providing an array with custom options like 'local,' which contained a list of hosts that would be considered local. In Laravel 5, however, the implementation has changed. The new `detectEnvironment()` function relies on a closure that first checks for the presence of an `APP_ENV` environment variable and uses its value if present. Otherwise, it assigns 'production' as the environment.
File Structure:
To make this more efficient, Laravel 5 removed `.env.*` from its .gitignore file, which allowed these files to be committed to their respective branches alongside your codebase. This is crucial for maintaining consistency and ensuring that each environment follows a standardized process.
The new structure consists of three main files:
1. .env - The primary environment variables file that can reside in the root folder.
2. .env.example - A sample configuration file, which can be renamed to .env for your local development or other environments as necessary.
3. Config/Envoy.php - The config class containing additional helper functions and settings used to manage environments.
Managing Multiple Environments:
If you have multiple environments and need to set different values based on the environment, you can create separate .env files for each environment within specific folders. For example:
- .env.local
- .env.alpha
- .env.test
- ...
Each file will contain the unique variables required for that particular environment. To load these variables into your application, use the `loadEnvironmentFrom` method in Config/Envoy.php, as demonstrated:
```php
$app->loadEnvironmentFrom(dirname(__DIR__).'/.env.'.$app['environment']);
```
What Happens without .env?:
If you have no .env file or if your application detects an invalid environment variable, Laravel will use 'local' as the default environment, ensuring that your app is still functional. However, it will print a warning message to the console stating that the environment has been set to 'local.'
Conclusion:
Managing and setting environment variables in Laravel 5 has become more efficient through new implementation methods and changes in file structure. By adopting best practices such as creating separate .env files for multiple environments or using the `loadEnvironmentFrom` method, you can ensure that your application consistently uses the correct settings regardless of its current running state.