How to Setup Multiple Environment for Laravel 5 Developers Way

Hello Guys, Today i will teach you on how to setup multiple environment in using Laravel 5 Framework.


Update Codes: April 02, 2015

  • I put laravel 5.1 version for loading the env file.
  • Dotenv::load(__DIR__ . '/../', '.' . getenv('APP_ENV') . '.env'); 

Update Codes: Feb 05, 2015

  • Please check the new code for laravel 5.2 for loading using DotEnv


If you just heard Laravel 5 for the first time, take a look on their website you will see the advantage of using it and be A Web Artisans.
Important benifits using Laravel 5.

  • Expressive, beautiful syntax.
  • Tailored for your team.
  •  Modern toolkit. Pinch of magic.
  • Love beautiful code
  • And Many More…

So lets get back to business, First thing to do is to checkout a new laravel 5 using composer.
You can take a look in the documentation here: Laravel 5 Installation.

After you setup everything, from installation of Homestead and Setting up new Laravel 5 virtual domain..

Follow this instructions:

1. Check if you have .env file (it should be automatically created by laravel)

  • REMOVE all the contents from here, and just put: local or production (whatever environment you want). This will be the one that will be changed when you want to switch to another environment. :D

2. Create new Environment file let say you have your local and production enviroment.

  • Create a file with the name of: .local.env
  • Create a file with the name of: .production.env

3. Add default environment value.

  • For Local Environment (.local.env file)
    • APP_ENV=local


  • For Production Environment (.production.env file)
    • APP_ENV=production

4. Create new php file and named it, environment.php, save it into this folder: app/bootstrap/environment.php
– Inside of this file we will do the magic. Insert this snippet code.


5.  This snippet code, use, putenv php function to set APP_ENV and then using the Dotenv package by vlucas. I’m loading the file that we created and base on the .env file we can determine which environment should we use.

6. Include your environment.php file in bootstrap file. Paste it inside your bootstrap/app.php file.

6. After that you can check which environment is being loaded using:

7. I output the current environment in my Laravel 5 Welcome Page, You can see my screenshots below:

How to Setup Multiple Environment for Laravel 5 Developers Way

How to Setup Multiple Environment for Laravel 5 Developers Way

How to Setup Multiple Environment for Laravel 5 Developers Way

How to Setup Multiple Environment for Laravel 5 Developers Way

75263 Total Views 93 Views Today
Anthony Pillos

About Anthony Pillos – We are Awesome. Tutorials in different programming languages like PHP,HTML5,JAVASCRIPT,CSS/CSS3,JQuery,Jquery Mobile, Responsive Design.

75264 Total Views 94 Views Today


  1. Hello,

    This does not work for me.
    I deleted the fichier.env and created two files. .local.env and .production.env

    I put the code provided in your page.

    He always shows me “production”.

    An idea?

    1. Hello, What version of php you’re using in your server now? Can you check from your phpinfo() if safe_mode_allowed_env_vars is enabled, if not you should enabled it. Also if safe_mode_allowed_env_vars is enabled and still not working.. try to use this apache_setenv or apache_getenv if you’re using apache. let me know if this help.

    1. hello ovidiu, the variable $app is coming from bootstrap/app.php, environment.php can access the variable $app inside of app.php because i put a require __DIR__.'/environment.php'; inside..

  2. How do I setup multiple dev environments on a box(server) (I’d first implement it on a local laptop then transfer it to a server machine), so multiple developers Just need an SDK, set some config, and start coding. When done, they can just commit the code. The databases for dev vs prod are already set in the config?. I need such a laravel dev environment setup. All they have to do is modify hosts file. All Help is appreciated.

    1. This tutorial can be used in your multiple dev environments.. if you have development and production you can easily separate them. just apply the current tutorial then the database will be separate for developement and for productions.

  3. Thanks for writing this tutorial, it works on my Laravel app. :)

    I had a couple of issues though, and I just want to share how I fixed them.

    I’m using Laravel version 5.2.39 and I placed the environment.php file under /bootstrap/environment.php instead of app/bootstrap/environment.php because app/ and bootstrap/ folders are on the same level.

    Then I had this error: “Fatal error: Call to a member function detectEnvironment() on a non-object in […]/bootstrap/environment.php on line 13″

    To fix it, I had to add the following code in app/bootstrap/environment.php, just above the line that calls detectEnvironment()

    $app = new Illuminate\Foundation\Application(

    1. Hello thank you catzie, hmm.. i think you dont need to do that.. why?. take a look bootstrap/app.php it has already

      $app = new Illuminate\Foundation\Application(

      i think you’re missing the required code inside app.php
      you should add this.. below of app.php code inside

      require __DIR__.'/environment.php';

      Thanks :D

  4. Why are people insisting on having more than one .env file? :-/
    IMO – There should only be one Environment Setup per environment.
    Staging server should be separate, and in that case just has a .env file with its own settings.
    Local server, separate again, where the .env file is its own copy, with its own settings.
    Each developer sets their own settings on their DEV machines. e.g. my DB username and password can be different to all other developers on my team. My own local environment settings will not affect the app.
    The staging server should _not_ contain a copy of the production server’s environment settings file. Nor should any of your repositories. Keep it on one server, secure, restricted access.
    … just my thoughts. ;-)

    1. Yes, you have a point.. well its really depend on each developer.. but remember we’re doing this to separate things.. for local and productions whereas the productions .env is inside of my .gitignore. so from production server it will not get pulled out from the repositories. and each developer will create their own .local.env files… this setup is most likely used in a Single Server setup.. :D coz some company or developer cant afford to have different environment server.. thats my opinion only..

  5. This idea works like magic. Thanks a lot, I am using this concept to do some kind of multi-tenancy and it works well. I am grateful

Leave a Reply to Anthony Pillos Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">