16

I am new to Laravel and want a simple explanation of the .env and .env.example files, why we need them and the difference between them.

I know that .env is used to specify the app's database connection, for example but I would like to understand it deeper.

8 Answers 8

12

.env file, as its name suggest, is a local where you put all your environment setup, such as database credentials, cache drivers and etc. Everything that is about the server that the project is running, and may have different values for different servers, are setup here.

Per example, your local dev environment has different database credentials than production environment. Also your colleague dev environment has different than yours. So each one has a .env with different informations.

And because of these, this file can't be versioned, so .env.example is the file that has every constants setups that .env has but with no values, and only this one is versioned. .env.example works as a guide for creating a .env file with the needed informations that is needs to have the application running.

As you are working with Laravel, you can find more informations here: environment-configuration

7

The .env file stores configuration variables for your application and .env.example is simply an example of what might be in the .env file! You can easily rename .env.example to .env to get started.

What are configuration variables? From The Twelve-Factor App

An app’s config is everything that is likely to vary between deploys (staging, production, developer environments, etc). This includes:

  • Resource handles to the database, Memcached, and other backing services
  • Credentials to external services such as Amazon S3 or Twitter
  • Per-deploy values such as the canonical hostname for the deploy

In Laravel the .env file also contains your app key which is used for encryption in your app. Because of this, and because you will likely store other private keys in this file, ensure you do not commit .env to your source control or share it publicly!

I recommend you read the link above for an explanation of why you should separate configuration from your application and for Laravel-specific information you can look here

1
  • It's worth noting that on production you would want to actually set environment variables for the app instead of reading them from .env. Also, if you work in a team setting you will likely want to copy the .env.example file to .env (instead of rename) so that others on your team can do the same whenever they clone the project (and fill in their own details inside their own .env). Keeping the example file up-to-date with the variables actually used by your app is also a good idea. Commented Mar 24, 2022 at 7:30
6

.env is simply used to store all sensitive files like password API key, database,and so on as environment variables to be used in your code later this sensitive files are not included in the code base and will not be there when been pushed to git. .env.example This is a file that tells other programmer what is meant to be in there code when your code is cloned or been used by another user.

Example .env API_KEY="hwhhwhshs6585gahwhgwuwjwusuhs"

.env.example API_KEY="YOUR API KEY GOES HERE"

4

.env file contains various settings, one row – one KEY=VALUE pair. And then, within your Laravel project code you can get those environment variables with function env(‘KEY’).

The rule is that .env file is not committed to the repository, so it is really convenient, cause then people on your team can change their variables locally without committing them to the repository.

Now, .env.example file, on the contrary, is included in the repository – it is used as an example file for you to know what KEY=VALUE pairs you need for your project. Most often it is used to copy it to .env file and then change the values.

You can also read about it in the official Laravel documentation.

2

The .env.example file is just an example of the .env file. It is not used by the app. It's used to serve as a base for you to edit and rename.

The .env file contains constants that are specific to that application to those environments. What this means is for example if I want to deploy my app in multiple places with the same code, I'll just have to change some settings on this file to run on each environment and we are all set, no code changes needed.

These settings can be database connection settings but they can be used for other things too like the APP_KEY that should be different for each application and used my many functions.

Be careful, this .env file should not be shared anywhere as it contains private information about that specific deploy.

You can read more here: https://laravel.com/docs/5.6/configuration#environment-configuration

0

.env or environment variables are files that store some sensitive information like your API key. They are only visible to you on your PC/local system. Not to anyone else, if you push your project to GitHub or some other platform.

0

.env is a file that store information about your website such as API or database password. these information are visible for you in local host. when you publish website on a host this file not visible for other people.actually env's file improve security website on the net.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jan 6, 2022 at 3:02
0

.env.example act as kind of a template for .env

Not the answer you're looking for? Browse other questions tagged or ask your own question.