Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Čamo's avatar
Level 3

Custom .env file

I need to have more than one .env files like .env, .env.local, .... and need to be able set up proper env file before application is created. I tried to do it via --env param but it seems it does not work with php artisan serve --env local because any request from client to localhost:8000 have original .env file. For other artisan commands this workd but not for artisan serve. There is a Application::environmentFile() method but I can call it at the very end of Application set up process.

Can somebody tell me please how to do it properly?

0 likes
23 replies
Snapey's avatar

You only need one .env per environment. You don't need to have multiple.

Your .env is created on the server you are deploying to so on a staging server, the .env will be different to the one on the production server.

The .env file should never be committed to your repository, but you can create an env.example which has placeholders to prompt what needs filling in on other servers or for other developers.

martinbean's avatar

@Čamo That completely defeats the point of .env files.

Their entire purpose is to set environment variables for an environment, where setting environment variables proper is difficult. So, locally, you would have an .env with values specific to your local environment. Other environments would then have their own .env files with values appropriate to that environment only.

Čamo's avatar
Level 3

I understand your point to have only one ev file and do not commit this file to git. BUT I really need it in Git so I would like to have more than one env file. For now I have this solution in bootstrap/app.php but I am not sure if the call of ->create()->loadEnvironmentFrom(getEnvFile()) is in the right order.

martinbean's avatar

BUT I really need it in Git

@Čamo No, you don’t. .env files are ignored by default for a reason.

Create an .env file in each of your environments with that environment’s specific values.

Snapey's avatar

If you REALLY need it, good luck. You must have very special and unique circumstances.

Tell me, how are you deciding which env file you need?

Čamo's avatar
Level 3

I understand you very well. BUT we are not talking here about common web application or API but about devices like Raspberry PI, Firefly, .... and every device could have its own specific environment which are related to specific version of the system. So it is not only about to copy .env from current repository but about specific env file for specific version......

martinbean's avatar

I understand you very well. BUT we are not talking here about common web application or API but about devices like Raspberry PI, Firefly, .... and every device could have its own specific environment which are related to specific version of the system. So it is not only about to copy .env from current repository but about specific env file for specific version......

@Čamo This is literally what environment variables are for.

Set the variables in those environments to the values you need, and it makes .env files completely redundant.

.env files only exist for scenarios where it’s difficult or impractical to set environment variables, such as your local machine where you may be running multiple projects.

martinbean's avatar

@Čamo Why do you think an .env file is called such? BECAUSE THEY’RE MEANT TO EMULATE ENVIRONMENT VARIABLES.

Čamo's avatar
Level 3

@martinbean Ok so tell me please how to do it for all variables in env file. Where should I put the code cause nor the ChatGPT does not know. And how to ensure that Laravel do not overide my variables.

martinbean's avatar

@Čamo You SSH into those machines and set environment variables. Laravel will read them if no .env file is present.

Čamo's avatar
Level 3

@Snapey i am just currious how to do it. Composer is one of the way I did not think about.

Now I have something like this in app.php and it seems to work.

$basePath = dirname(__DIR__);
$envFile = file_exists($basePath.'/.env.local') ? '.env.local' : '.env';

// 🚀 THIS HAS TO BE HERE BEFORE APPLICATION IS CREATED
Dotenv::createImmutable($basePath, $envFile)->safeLoad();

Čamo's avatar
Level 3

@martinbean there is hundrets of devices.... I really need to make it dynamically.

Čamo's avatar
Level 3

@martinbean I tried to use something like in app.php

$basePath = dirname(__DIR__);
$envFile = file_exists($basePath.'/.env.local') ? '.env.local' : '.env';

// 🚀 THIS HAS TO BE HERE BEFORE APPLICATION IS CREATED
Dotenv::createImmutable($basePath, $envFile)->safeLoad();

This works fine but the device has to be selected with better precission. It will check what kind of device is running and then choose right .env file.

martinbean's avatar

there is hundrets of devices

@Čamo So what? You gonna have “hundreds” of .env files in your repository…? 🙃

Configure your devices properly.

Čamo's avatar
Level 3

Can somebody explain to me why

->loadEnvironmentFrom(getEnvFile());

exists and how to use it properly? In most cases this works fine BUT with schedule:work it does not set choosen env file

Snapey's avatar

@Čamo How can it? How does the application know that its running on a raspberry Pi and therefore it needs .env.raspberry (for example)

If you HAVE to have different files in one distribution, have a step in your composer.json that copies the relevant env.various into .env as a one-off deployment step for that individual machine.

Go on, tell me... you don't use a deployment script, you FTP the files?

Čamo's avatar
Level 3

@Snapey Imagine hundreds of devices which have different motherboard different system versions .... I need to know what the application is running dynamically.

Snapey's avatar

@Čamo whats your point?

You say the device needs an env file that is just for that type of device.

What I an saying is that you only need to determine which file it needs once... when the application is deployed, not everytime it is booted.

Please or to participate in this conversation.