[laravel] How to generate .env file for laravel?

From the documentation I see it's possible to create a laravel project via laravel installer:

$laravel new blog

or via composer:

$composer create-project laravel/laravel --prefer-dist

If I try the first way .env file is not created. How can I ask laravel,artisan or composer to create a .env file for me?

This question is related to laravel installation composer-php laravel-5 environment

The answer is


In windows OS, you can open command prompt, change directory to your project directory.
Example,
My project directory

E:\xampp\htdocs\my_project


You can type into your Command prompt like this (hit enter each line):

E:

cd xampp\htdocs\my_project

Then you can type,

copy .env.example .env

If you are using Linux, you can type cp, instead of copy


.env files are hidden by Netbeans. To show them do this:

Tools > Options > Miscellaneous > Files

Under Files Ignored be the IDE is Ignored Files Pattern:

The default is

^(CVS|SCCS|vssver.?\.scc|#.*#|%.*%|_svn)$|~$|^\.(?!(htaccess|git.+|hgignore)$).*$

Add env to the excluded-not-excluded bit

^(CVS|SCCS|vssver.?\.scc|#.*#|%.*%|_svn)$|~$|^\.(?!(env|htaccess|git.+|hgignore)$).*$

Files named .env now show.


create .env using command!

composer run post-root-package-install or sudo composer run post-root-package-install


There's another explanation for why .env doesn't exist, and it happens when you move all the Laravel files.

Take this workflow: in your project directory you do laravel new whatever, Laravel is installed in whatever, you do mv * .. to move all the files to your project folder, and you remove whatever. The problem is, mv doesn't move hidden files by default, so the .env files are left behind, and are removed!


This is an old thread, but as it still gets viewed and recently active "26" days ago as of this post, here is a quick solution.

There is no .env file initially, you must duplicate .env.example as .env.

In windows, you can open a command prompt aka the CLI and paste the exact code below while inside the root directory of the project. Must include the ( at the start line without space.

(
echo APP_NAME=Laravel
echo APP_ENV=local
echo APP_KEY=
echo APP_DEBUG=true
echo APP_URL=http://localhost
echo.
echo LOG_CHANNEL=stack
echo.
echo DB_CONNECTION=mysql
echo DB_HOST=127.0.0.1
echo DB_PORT=3306
echo DB_DATABASE=homestead
echo DB_USERNAME=homestead
echo DB_PASSWORD=secret
echo.
echo BROADCAST_DRIVER=log
echo CACHE_DRIVER=file
echo SESSION_DRIVER=file
echo SESSION_LIFETIME=120
echo QUEUE_DRIVER=sync
echo.
echo REDIS_HOST=127.0.0.1
echo REDIS_PASSWORD=null
echo REDIS_PORT=6379
echo.
echo MAIL_DRIVER=smtp
echo MAIL_HOST=smtp.mailtrap.io
echo MAIL_PORT=2525
echo MAIL_USERNAME=null
echo MAIL_PASSWORD=null
echo MAIL_ENCRYPTION=null
echo.
echo PUSHER_APP_ID=
echo PUSHER_APP_KEY=
echo PUSHER_APP_SECRET=
echo PUSHER_APP_CLUSTER=mt1
echo.
echo MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
echo MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
)>".env"

Just press enter to exit the prompt and you should have the .env file with the default settings created in the same directory you typed above CLI command.

Hope this helps.


Just tried both ways and in both ways I got generated .env file:

enter image description here

Composer should automatically create .env file. In the post-create-project-cmd section of the composer.json you can find:

"post-create-project-cmd": [
  "php -r \"copy('.env.example', '.env');\"",
  "php artisan key:generate"
]

Both ways use the same composer.json file, so there shoudn't be any difference.

I suggest you to update laravel/installer to the last version: 1.2 and try again:

composer global require "laravel/installer=~1.2"

You can always generate .env file manually by running:

cp .env.example .env
php artisan key:generate

If you have trouble viewing the .env file or it is not showing up in the project just do this: ls -a.
This allows to see the hidden files in Linux. You can also open the folder with Visual Studio Code and you will see the files and be able to modify them.


If the .env a file is missing, then there is another way to generate a .env file

You can download env.example, rename it to .env and edit it. Just set up correct DB credentials etc.

Don't forget to When you use the php artisan key:generate it will generate the new key to your .env file


in console (cmd), go to app root path and execute:

type .env.example > .env

Examples related to laravel

Parameter binding on left joins with array in Laravel Query Builder Laravel 4 with Sentry 2 add user to a group on Registration Target class controller does not exist - Laravel 8 Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel How to fix 'Unchecked runtime.lastError: The message port closed before a response was received' chrome issue? Post request in Laravel - Error - 419 Sorry, your session/ 419 your page has expired Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required How can I run specific migration in laravel Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory

Examples related to installation

You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) Conda version pip install -r requirements.txt --target ./lib How to avoid the "Windows Defender SmartScreen prevented an unrecognized app from starting warning" PackagesNotFoundError: The following packages are not available from current channels: Tensorflow import error: No module named 'tensorflow' Downgrade npm to an older version Create Setup/MSI installer in Visual Studio 2017 how to install tensorflow on anaconda python 3.6 Application Installation Failed in Android Studio How to install pip for Python 3.6 on Ubuntu 16.10?

Examples related to composer-php

Composer require runs out of memory. PHP Fatal error: Allowed memory size of 1610612736 bytes exhausted How to change PHP version used by composer PHP7 : install ext-dom issue PHP error: "The zip extension and unzip command are both missing, skipping." Composer: file_put_contents(./composer.json): failed to open stream: Permission denied require(vendor/autoload.php): failed to open stream How to install a specific version of package using Composer? Composer update memory limit Cannot create cache directory .. or directory is not writable. Proceeding without cache in Laravel To enable extensions, verify that they are enabled in those .ini files - Vagrant/Ubuntu/Magento 2.0.2

Examples related to laravel-5

Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory Can't install laravel installer via composer Including a css file in a blade template? No Application Encryption Key Has Been Specified How to Install Font Awesome in Laravel Mix Laravel Migration Error: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes Laravel 5.4 redirection to custom url after login How to set the default value of an attribute on a Laravel model laravel 5.3 new Auth::routes()

Examples related to environment

Using Pip to install packages to Anaconda Environment How to generate .env file for laravel? Set windows environment variables with a batch file Setting up Eclipse with JRE Path Changing default shell in Linux How to find the path of the local git repository when I am possibly in a subdirectory Windows 7 environment variable not working in path Error: Registry key 'Software\JavaSoft\Java Runtime Environment'\CurrentVersion'? Java system properties and environment variables Environment variables in Eclipse