Profile pic of Tommy KuTommy Ku's Blog

Reading and thinking

Multiple .env configurations in Laravel 5 using symbolic link

Posted on  by Tommy Ku

This post is more than 2 years old, it may contain outdated information

In this post I am going to introduce an ingenius approach for having multiple environmental configurations in Laravel 5 without the need for another artisan command. Credit goes to @Tiquortoo on reddit who nicely pointed that out.

Tl;dr;

$ ln -s ./.local.env .env

Bear with me if you don’t know what the hack that command does.

ln creates hard links or symbolic link (if you add the -s option) in Unix-like systems.

A symbolic links like a pointer to a file itself. When you access a symbolic link you will be redirected to the file. Deleting the file which the symbolic link is pointing will cause the link to be broken.

A hard link, however, points directly to the inode of a file, so you can see it as a file existings in both folders. If you modify a file from it’s hard link the original file content get changed. If you delete one hard link, the file itself will not be removed until all the hard links are removed.

Very likely that you have a couple of .env lying around (.local.env, .testing.env, .staging1.env etc), one for each environment. One of the easiest way to change the environement is to simply overwrite the .env with the config file of your target environment like:

$ cat .testing.env > .env

That doesn’t look nice because you will then have 2 separate .testing.env to manage. What if you want to make a change? (1) change one and run the cat again, or (2) change both. Both ways sound dumb.

Now what if we use symbolic link:

$ ln -s ./.testing.env .env

Changing either of .env or .testing.env will affect both files. For whatever change you want to do, you will only need to do it once. Having multiple environmental configuration is particularily useful for teams having multiple deploys, so I hope this article helps.

You could also look at...

This post first appeared on . Permalink:

About the author

Profile pic of Tommy Ku

Tommy Ku, a Hong Kong-based Software Engineer experienced developing PHP and Java-based web solutions and passionate in Web technology.

Also a hobbyist digital and film photographer.