Fix "No application encryption key has been specified." in Laravel
How to fix the “No application encryption key has been specified.” error message in Laravel
“No application encryption key has been specified.” is quite common in Laravel, and it means exactly what it says: there’s no encryption key specified for your application.
To fix “No application encryption key has been specified.”, run the command php artisan key:generate
.
Understanding the “No application encryption key has been specified.” error message in Laravel
Laravel uses an encryption key to secure sessions, cookies, serialized data, password hashes, and other encrypted data.
If this key is not set, Laravel cannot guarantee the security of these things, hence the error message.
This is a big problem, especially in production where sensitive data must be encrypted. Here’s how to fix the issue:
-
Generate an application key: Open a terminal, navigate to your project directory, and run the command
php artisan key:generate
. This command will generate a new random key for your application. To be safe, runphp artisan config:clear
just in case you previously cached the config values. -
Create the eventually missing .env file: The above command sets the generated key to your
APP_KEY
environment variable in your .env file. Laravel should automatically have created it based on .env.example at the root of your project. If you get thefile_get_contents(/path/to/project/.env): Failed to open stream: No such file or directory
error message, it means it didn’t. You must create it yourself by runningcp .env.example .env
for instance.
Remember, it’s important to keep your APP_KEY
secret and not to commit your .env file to version control systems.
Learn more on Laravel’s documentation.
Bonus: fix the “No application encryption key has been specified.” error message in Laravel with one click
As I said, the “No application encryption key has been specified.” error message is extremely frequent in Laravel.
So much that Laravel offers you to fix it with just a single click!
Did you notice the button? Try it, it’s so convenient! 👍