How to create .env file with command line in Laravel?

If you are familiar with command line tools, you may find it easier to create .env file with command line.

You can always create a new .env file, if it is missing or gets corrupted. There are many ways to generate it. However, many applications won’t have an .env file initially.

So, you will need to duplicate the env.example file and rename it to .env. You can then set it up with your current database credentials.

You can open command line, navigate to your project directory or change the current directory as your Laravel application one. In Windows Operating System, you can do it with the command below.


Or navigate to your project using cd, as given below.

cd xampp\htdocs\laravel_project

Once in your project directory, you can pass the below commands in a CLI to create and generate a .env file.

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

Running the the php artisan key:generate will help you create a new key for your .env file.

Most of the time, .env may not exist in your Laravel application. If by chance, it is also present. Creating a new one, using your CLI and with your desired credentials and environment variables, will overwrites the old file. This ensures that the right information is entered when you create .env file with command line.

Leave a Comment