410

I'm trying to use the Artisan command like this:

php artisan serve

It displays:

Laravel development server started: http://127.0.0.1:8000

However, it won't automatically launch and when I manually enter http://127.0.0.1:8000 it shows this error:

RuntimeException No application encryption key has been specified.

What's the cause of this problem, and how can it be fixed?

I'm using Laravel framework 5.5-dev.

starball
  • 20,030
  • 7
  • 43
  • 238
Carlos F
  • 4,621
  • 3
  • 12
  • 19
  • or if your .env file needs to be changed ref: https://stackoverflow.com/questions/36276767/laravel-install-getting-key-generate-error-in-ubunto-os/36279613 – Nameishi Aug 12 '17 at 23:20
  • 2
    The answers below are correct: however, if you first run "php artisan serve" and have an active web server session you will need to restart the server (control-c in the terminal to stop) to get the change to take effect after you add the key to your .env file. – Chris Adams Oct 24 '17 at 19:51

27 Answers27

672

From Encryption - Laravel - The PHP Framework For Web Artisans:

"Before using Laravel's encrypter, you must set a key option in your config/app.php configuration file. You should use the php artisan key:generate command to generate this key"

From Encryption - Laravel - The PHP Framework For Web Artisans:

"Before using Laravel's encrypter, you must set a key option in your config/app.php configuration file. You should use the php artisan key:generate command to generate this key"

I found it using this query in google.com: "laravel add encrption key" (Yes, it worked even with the typo!)

Note that if the .env file contains the key but you are still getting an application key error, then run php artisan config:cache to clear and reset the config.

Amarnasan
  • 14,939
  • 5
  • 33
  • 37
  • Im sorry so are you putting php artisan key:generate in the terminal? because I get an ErrorException – Nameishi Aug 12 '17 at 22:57
  • 2
    For anyone else that ran into my issue input this into the terminal cp .env.example .env php artisan key:generate – Nameishi Aug 12 '17 at 23:15
  • 12
    `php artisan key:generate` does not even work for me. Gives me the same error **No application encryption key has been specified**, please any solution? – Oniya Daniel Nov 04 '17 at 08:39
  • 7
    This worked for me, but I first had to rename the file .env.example to .env for this to work, otherwise I was getting ".env: no such file" error. – Webomatik Nov 27 '17 at 15:11
  • 3
    Works, but first you need to create an empty .env file, and then copy the contents of .env.example, to your .env file. The .env.exampe file has an 'APP_KEY=' line, where 'APP_KEY' is the key, and nothing is specified as the value. If you don't have the the 'APP_KEY=' line, it seems it will not add the generated key to the .env file. I also then had to run 'php artisan config:cache' and then restart the server, for it to work. – Sami El Maameri Jun 17 '18 at 06:12
  • Don't forget 'php artisan config:cache' after you generate your key. You need this especially after moving from another server, or whenever making changes to config or the .env on a working app – James Bailey Sep 27 '18 at 13:32
  • If you are using `php artisan serve` you need to restart the server after changing the `.env` file. – Daniel Oliveira Oct 24 '18 at 00:50
221

In my case, I also needed to reset the cached config files:

php artisan key:generate
php artisan config:cache
Leonid Dashko
  • 3,657
  • 1
  • 18
  • 26
  • 1
    What is `php artisan config:cache` for? Why is it useful? – Pathros Feb 17 '19 at 18:57
  • 2
    @Pathros to create/regenerate the cached config files – Leonid Dashko Feb 18 '19 at 19:29
  • 5
    In addition to this, first, ensure that your "`.env`" file at least contains the empty key-option, like: `APP_KEY=` – Top-Master Sep 03 '19 at 04:53
  • 2
    Or, for local development use `php artisan config:clear`, and it won't generate a new cached version. – Daniel Dewhurst Sep 06 '19 at 12:43
  • `php artisan config:cache` is what I missed. I've typed it in and the very same moment this page loaded :) – sr9yar Dec 10 '20 at 19:00
  • If Laravel has cached a configuration value, updating the `.env` file won't have an immediate effect. You need to refresh the configuration cache using `php artisan config:cache` or clear the old cache with `php artisan config:clear` for the changes to take effect. – Shakil Alam Sep 01 '23 at 10:09
65

Open command prompt in the root folder of your project and run below command:

php artisan key:generate

It will generate Application Key for your application.

You can find the generated application key(APP_KEY) in .env file.

skm
  • 1,192
  • 1
  • 11
  • 23
33
  1. Copy .env.example to .env:

    cp -a .env.example .env

  2. Generate a key:

    php artisan key:generate

  3. Only then run:

    php artisan serve

Adam Pery
  • 1,924
  • 22
  • 21
  • 1
    Whoops. This was the critical step! A fresh Laravel project needs a .env file or nothing will work. Thanks, Adam. – Eric L. Apr 13 '19 at 04:38
26

Simply run this command:

php artisan key:generate
Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
Didinya Johnson
  • 472
  • 6
  • 4
18

cp .env.example .env if there is no .env file present. php artisan key:generate command works for me. It generates the encryption key

Kamran Syed
  • 439
  • 3
  • 7
Sabyasachi Ghosh
  • 1,402
  • 14
  • 18
15

Open command prompt in the root folder of your project and run

php artisan key:generate

Then

php artisan config:cache

and Then

If you're getting the same error after having key-value, then just copy the APP_KEY value from .env file and paste it to config/app.php with 'key' => 'YOUR KEY',

and then again run

php artisan config:cache
Hashmat
  • 149
  • 1
  • 11
kaushik
  • 903
  • 7
  • 16
13

I actually had to add a .env file to my project and then copy the contents of .env.example so that the key:generate would work. Not sure why a .env file was not created when I started the project.

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Craig Kollross
  • 139
  • 1
  • 2
  • 1
    It might depend on how you installed Laravel ... "_In a fresh Laravel installation, the root directory of your application will contain a `.env.example` file. If you install Laravel via Composer, this file will automatically be renamed to `.env`. Otherwise, you should rename the file manually._" -[#Environment Configuration](https://laravel.com/docs/5.5/configuration#environment-configuration) – Sᴀᴍ Onᴇᴌᴀ Sep 11 '17 at 16:01
  • also, if you cloned the project from a repo where the .env file was likely in .gitignore, then you would neeed to create it as well. From the Laravel docs: 'Your .env file should not be committed to your application's source control, since each developer / server using your application could require a different environment configuration' – Sami El Maameri Jun 17 '18 at 06:08
  • I also found that if you download it from the cloud (I'm using windows) i.e. google-drive, sky-drive etc. the .env, .env.example files are named _env, _env.example etc. In that case I renamed the files accordingly and then just ran 'php artisan key:generate'. – Tim Kruger Jun 18 '18 at 07:49
12

In 3 steps:

Generate new key php artisan key:generate

Clear the config php artisan config:clear

Update cache php artisan config:cache

Darlan Dieterich
  • 2,369
  • 1
  • 27
  • 37
11
php artisan key:generate
php artisan config:cache

worked for me, but it had to be done in a command prompt on Windows.

Doing it inside the terminal in PHPStorm didn't worked.

José Maria
  • 149
  • 1
  • 4
10

A common issue you might experience when working on a Laravel application is the exception:

RuntimeException No application encryption key has been specified.

You'll often run into this when you pull down an existing Laravel application, where you copy the .env.example file to .env but don't set a value for the APP_KEY variable.

At the command line, issue the following Artisan command to generate a key:

php artisan key:generate

This will generate a random key for APP_KEY, After completion of .env edit please enter this command in your terminal for clear cache:php artisan config:cache

Also, If you are using the PHP's default web server (eg. php artisan serve) you need to restart the server changing your .env file values. now you will not get to see this error message.

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
7

Follow this steps:

  1. php artisan key:generate
  2. php artisan config:cache
  3. php artisan serve
Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
  • 1
    even after generating key in .env file i was getting error. When i ran "php artisaan config:cache" it cleared cache and then only my problem solved. Thanks a lot. – Pankaj Apr 14 '19 at 10:39
  • Remember to point to the database and close the .env file. If the .env file is open the error will continue. – Renato Lazaro Sep 04 '21 at 15:56
7

Simply run command php artisan key:generate.. Still issue exist then run one more command php artisan config:cache and php artisan cache:clear ..

Now run php artisan serve

5

Okay, I'll write another instruction, because didn't find the clear answer here. So if you faced such problems, follow this:

  1. Rename or copy/rename .env.example file in the root of your project to .env.

You should not just create empty .env file, but fill it with content of .env.example.

  1. In the terminal go to the project root directory(not public folder) and run

php artisan key:generate

  1. If everything is okay, the response in the terminal should look like this

Application key [base64:wbvPP9pBOwifnwu84BeKAVzmwM4TLvcVFowLcPAi6nA=] set successfully.

  1. Now just copy key itself and paste it in your .env file as the value to APP_KEY. Result line should look like this:

APP_KEY=base64:wbvPP9pBOwifnwu84BeKAVzmwM4TLvcVFowLcPAi6nA=

  1. In terminal run

php artisan config:cache

That's it.

Prakash Pazhanisamy
  • 997
  • 1
  • 15
  • 25
5

I found that most answers are incomplete here. In case anyone else is still looking for this:

  1. Check if you have APP_KEY= in your .env, if not just add it without a value.
  2. Run this command: php artisan key:generate. This will fill in the value to the APP_KEY in your .env file.
  3. Finally, run php artisan config:cache in order to clear your config cache and recache your config with the new APP_KEY value.
Diligence Vagere
  • 178
  • 1
  • 3
  • 9
3

You can generate Application Encryption Key using this command:

php artisan key:generate

Then, create a cache file for faster configuration loading using this command:

php artisan config:cache

Or, serve the application on the PHP development server using this command:

php artisan serve

That's it!

Prakash Pazhanisamy
  • 997
  • 1
  • 15
  • 25
3

If you git clone some project then this kind of issue may usually occur.

  1. make sure there is .env file
  2. run php artisan key:generate and then it should generate APP_KEY in .env
  3. finally run php artisan serve and it should be working.
Top-Master
  • 7,611
  • 5
  • 39
  • 71
Jasbin Karki
  • 574
  • 7
  • 15
3

If you don't have a .env file then run the below command, else skip this

cp .env.example .env

Then run the below artisan command and it will generate an application key for your project:

php artisan key:generate

Note: Your APP_KEY is inside your .env file.

Hedayatullah Sarwary
  • 2,664
  • 3
  • 24
  • 38
2

Facing the Same Issue in Laravel v8.49.0 (PHP v8.0.6) Solution
genrate app key

  1. Click

Genrate app key

succesfully genrate key

  1. Click on Refresh now
Sarthak Raval
  • 1,001
  • 1
  • 10
  • 23
2

If after running php artisan key:generate issue is not resolved, check your .env file.

Search for APP_KEY=.

If it doesn't exist, manually add it to .env file and run php artisan key:generate again.

After this you will see generated key in .env file.

Copy that key and paste it in /config/app.php (search for APP_KEY there as well). You should end up with something like this in app.php file

'key' => env('APP_KEY', 'base64:...'),

Then run php artisan serve (You might have to run php artisan config:cache at some point. Not 100% sure when)

temo
  • 612
  • 1
  • 9
  • 25
1

I ran into this issue when I manually copied the contents of my Laravel project (say sites/oldname) into a new directory on my Mac (say, sites/newname). Since I was manually dragging and droppping, it didn't grab the hidden files, namely, '.env'. When I looked more closely at sites/oldname I saw .editorconfig, .env, .env.example, .gitatrributes, .styleci.yml, etc.

The error went away once I copied the hidden files to the new directory.

So, "No Application Encryption Key Has Been Specified" is Laravel speak for "your .env file is missing."

JJ Rohrer
  • 2,671
  • 5
  • 29
  • 37
1

Sometimes If everything Fails Use this:

Goto: laravelProject/config/app.php

Find the line: 'key' => and check to what it refers,

It can either be one of two:

Case 1: env('APP_KEY') Case 2: "somekeystring"

For Case 1: Goto your .env file after you have run cp -a .env.example .env Enter a random string like 10101010101010101010101010101010

Now, run php artisan key:generate

Your key will be updated automatically.

For Case 2: set a random string like for value of Key 10101010101010101010101010101010

Now, run php artisan key:generate

Your key will be updated automatically.

imshashi17
  • 177
  • 1
  • 3
  • 12
1

Run below command to set app key

php artisan key:generate

By running the above command, it sets the key value in .env of your project directory which is referenced in the app config

// .env
APP_KEY=base64:XkrFWC+TGnySY2LsldPXAxuHpyjh8UuoPMt6yy2gJ8U=


// config/app.php
'key' => env(APP_KEY);

Restart your server, in order to reflect the above change.

Ankit Jindal
  • 3,672
  • 3
  • 25
  • 37
1

Among many other reasons which I have faced during my years of working with Laravel, most of them are already mentioned here in answers, one strange thing I got this issue in in server was because I forgot to run the command:

php artisan storage:link

Hope this helps someone else, but try it after using other solutions as they address the most common causes.

Abhishek
  • 3,900
  • 21
  • 42
0

simply run

php artisan key:generate

its worked for me

Dimuthu
  • 417
  • 4
  • 12
0

Try setting correct file permissions

chmod -R 777 storage/
chmod 777 bootstrap/cache/
Danon
  • 2,771
  • 27
  • 37
-1

I had to restart my queue worker using php artisan queue:restart after running php artisan key:generate to get jobs working.

Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100