Fix "No application encryption key has been specified." in Laravel

Fix "No application encryption key has been specified." in Laravel

Modified
Aug 12, 2023
Written by
Benjamin Crozat
0
comments
2 minutes
read

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:

  1. 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, run php artisan config:clear just in case you previously cached the config values.
  2. 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 the file_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 running cp .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! 👍

No application encryption key has been specified.

About Benjamin Crozat
Benjamin Crozat

Hi! I’m from the South of France and I’ve been a self-taught web developer since 2006. When I started learning PHP and JavaScript, PHP 4 was still widely used, Internet Explorer 6 ruled the world, and we used DHTML to add falling snow on websites.

Being able to educate myself for free on the web changed my life for the better. Giving back to the community was a natural direction in my career and I truly enjoy it.

Therefore, I decided to take action:

  1. I launched this blog in September 2022 with the goal to be in everyone’s Google search. I get more than tens of thousands of monthly clicks from it and even more visits overall (my analytics dashboard is public by the way).
  2. I also started growing my X (formerly Twitter) account at the same time, which has now over 7,000 followers.
  3. All the content I write is free thanks to my sponsors.

I also want to be completely free with my time and make a living with my own products. In April 2024, I launched Nobinge, a tool to summarize and chat with your content, including YouTube videos.

Believe me, I’m just getting started!

0 comments

You need to be signed in to comment this post.
Sign in with GitHub