How to have environment specific .env files for dotenv (in Laravel 5)

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Environment-Specific .env Files for Laravel 5 and DotEnv

As developers, we constantly grapple with managing configuration across different deployment stages—local development, staging testing, and production. A common hurdle in using frameworks like Laravel is separating sensitive, environment-specific variables from general application settings. You’ve correctly identified the desire: wanting to commit your secrets while maintaining a clean separation for each environment.

This post dives into how you can achieve environment-specific .env file loading when working with older setups like Laravel 5 and the dotenv library, moving beyond the limitations of a single root .env file.

The Challenge: Single vs. Multi-Environment Configuration

When starting with Laravel, the standard practice involves setting an APP_ENV variable (e.g., APP_ENV=local) in the main .env file. This value dictates which configuration sets are loaded. While this works fine for simple setups, it forces all environment-specific secrets into one file, making version control less secure and potentially complicating deployment pipelines.

Your goal is to separate configurations like database credentials or API keys based on the active environment (local, staging, production). Simply having .env.local doesn't automatically tell Laravel to ignore .env and load only the specific file; it still relies on the core framework logic which often defaults back to reading the primary configuration sources.

The Solution: Customizing Environment Loading

Since the standard dotenv implementation primarily reads a single file, achieving true environment-specific loading requires us to either hook into Laravel’s bootstrap process or implement a custom loader that prioritizes specific files based on the detected APP_ENV.

For environments where you need granular control over configuration inheritance—a practice highly encouraged by modern framework philosophies found at https://laravelcompany.com—we must manually manage which file is loaded before the application services initialize.

Step 1: Structuring Your Environment Files

The first step is to structure your files logically. Instead of relying on implicit loading, we will explicitly define the environment context.

Create separate files for each environment, including a root file that defines the active environment:

/
├── .env              <-- Base settings (optional, or used only for APP_ENV)
├── .env.local        <-- Local development secrets
├── .env.staging      <-- Staging server secrets
└── .env.production   <-- Production server secrets

In your primary .env file, you would define the environment setting:

APP_ENV=staging

Step 2: Implementing Custom Loading Logic (The DotEnv Hook)

Since Laravel’s default loading mechanism is quite opinionated, direct modifications to how dotenv loads files are necessary. In a pure custom setup (especially relevant when dealing with older frameworks), you need to write a small bootstrap script that reads the active environment and explicitly loads the corresponding file.

Here is a conceptual example of how you might structure your application bootstrap to handle this:

<?php

// index.php or bootstrap.php

$dotenv = new Dotenv\Dotenv(__DIR__);

// 1. Load the base .env to get APP_ENV
$dotenv->load();

$appEnv = $_ENV['APP_ENV'] ?? 'local'; // Default if not set

// 2. Determine which specific file to load based on APP_ENV
$envFile = ".env.{$appEnv}";

if (file_exists($envFile)) {
    // Load the environment-specific file, overriding or adding variables
    $dotenv->load($envFile);
} else {
    // Fallback or error handling if the specific file is missing
    echo "Error: Environment file {$envFile} not found.";
    exit(1);
}

// Now, configuration values from .env.staging (or .env.local) are available.
// The rest of your application setup can proceed using these loaded variables.

Conclusion

Achieving true environment-specific .env management is less about changing the core functionality of dotenv and more about controlling when and how those files are loaded within your application's bootstrap process. By explicitly reading the APP_ENV variable and directing the dotenv loader to target the appropriate file (e.g., .env.staging), you gain full control over configuration isolation.

While modern Laravel often provides more abstracted ways to handle this, understanding this foundational level of file loading is crucial for mastering application deployment and configuration management, ensuring your secrets remain secure whether you are developing locally or deploying to production environments.