Laravel can encrypt data in database fields such as passwords. However, you must have configured the encryption key in the APP_KEY environment variable. If the variable is empty or the key has an invalid format, you will get an error.
In this short article, I will explain how to fix the “your app key is missing or no application encryption key has been specified” error in Laravel.
How to Fix APP_KEY is Missing
You will not see this error when you create a new Laravel project using composer, because it already has the correct .env file with the key specified. But you can get it when you deploy your project from the Git repository. There you have only the .env.example file and APP_KEY is empty in it. If you copy the .env.example file into .env and then open project in the browser you will see the following error page:
Laravel also suggests how you can fix it in the green area. Run key:generate command to create a new key:
php artisan key:generate
Don’t forget to clear the config cache:
php artisan config:clear
However, it will only work when the APP_KEY variable is empty. If you have any text in it, for example, null, a new key will be inserted before this text.
And you will get the following error: “Unsupported cipher or incorrect key length: Supported ciphers are: aes-128-cbc, aes-256-cbc, aes-128-gcm, aes-256-gcm”. For example:
So, if you have generated the encryption key and it still is not working, check the APP_KEY variable and ensure that the key starts from base64: and ends on =. If there is anything else, you can make the variable empty and regenerate the key or remove extra characters manually. For example, on the screenshot above the null will cause the error:
So, you have to delete it.
In this article, I have explained how to fix no application encryption key has been specified in Laravel. As you can see it is easy. You can generate a new key or copy a key from another project into the APP_KEY variable and it will work.