Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to generate a new Application Key in Laravel?

Since it automatically sets it for me in my .env file when I create the app, I'm not sure when I should run it.

In addition to that, if a second developer comes in, and clones the app, does he/she need to run php artisan key:generate ?

How do we know exactly when to run php artisan key:generate ?

like image 477
code-8 Avatar asked Oct 27 '15 14:10

code-8


People also ask

Why do we generate key in laravel?

Laravel Key Generate helps you to generate the application once it is developed with the help of the key that will be generated in the Laravel Key Generate. The application that you have developed needs to be set on a random string and Laravel Key Generate helps you with this.

What is Artisan key generate?

php artisan key:generate is a command that sets the APP_KEY value in your . env file. By default, this command is run following a composer create-project laravel/laravel command.

Where can you find the laravel APP key?

The application key is a random, 32-character string stored in the APP_KEY key in your . env file. The Laravel installer generates one for you, so you'll only notice it missing when you clone an existing app.

How do you fix no application encryption key has been specified laravel?

To solve this issue you will have to generate a APP_KEY which is located within your . env (environment) file. To generate the key you can run the Laravel Artisan key generate command. Once you have generated it you can try again and refresh your Laravel application in the browser to see if the page is running or not.


2 Answers

php artisan key:generate is a command that sets the APP_KEY value in your .env file. By default, this command is run following a composer create-project laravel/laravel command. If you use a version control system like git to manage your project for development, calling git push ... will push a copy of your Laravel project to wherever it is going, but will not include your .env file. Therefore, if someone clones your project using git clone ... they will have to manually enter php artisan key:generate for their app to function correctly.

So, TL:DR the only time you need to call php artisan key:generate is following a clone of a pre-created Laravel project.

Side note: If you try to run a Laravel project with your APP_KEY set to SomeRandomString (which is the default in your .env.example file, you will actually get an error:

No supported encrypter found. The cipher and / or key length are invalid.

like image 100
Tim Lewis Avatar answered Sep 20 '22 15:09

Tim Lewis


The most important thing to do when cloning a laravel project is to first run composer update then composer install. The composer install command installs any required dependencies for that laravel app.

The steps I took to clone a laravel project required the php artisan key:generate command. I can see in my .env file that there is an updated APP_KEY=base64:xxxxxxxxxxxxxxxxxxxx after running this command.

like image 42
Erich Meissner Avatar answered Sep 20 '22 15:09

Erich Meissner