Fix "No application encryption key has been specified" in Laravel
No application encryption key has been specified.
- Illuminate\Encryption\MissingAppKeyException: No application encryption key has been specified.
- RuntimeException: No application encryption key has been specified.
- Unsupported cipher or incorrect key length. Supported ciphers are: aes-128-cbc, aes-256-cbc, aes-128-gcm, aes-256-gcm.
Laravel’s APP_KEY variable is empty or missing, so Laravel cannot encrypt cookies or sessions. Create the .env file if it is missing, run php artisan key:generate, then php artisan config:clear so the new key is picked up.
What this error means
Laravel encrypts cookies, the session and everything going through Crypt or encrypted casts with a secret key read from config('app.key'), which comes from the APP_KEY variable. The first time encryption is needed, often on the very first web request because of the session cookie, the encrypter is created. If the key is empty, Laravel throws MissingAppKeyException with this message. If the key exists but has the wrong length (32 bytes for aes-256-cbc, the default cipher) or lost its base64: prefix, you get "Unsupported cipher or incorrect key length". The key must be generated once per environment and never committed.
Common causes
- Project cloned without
.env: the.envfile is not versioned, andkey:generateonly runs automatically withcomposer create-project, not after agit clone. - Empty
APP_KEY:.envwas copied from.env.example, where the key is intentionally empty. - Cached configuration:
php artisan config:cacheran before the key existed, and Laravel ignores.envuntil the cache is cleared. - Variable missing in production or Docker: the platform (container, CI, Forge, Kubernetes) does not provide
APP_KEYin its environment variables. - Malformed key:
base64:prefix removed, extra spaces or quotes, or a key truncated by a copy-paste.
Step-by-step fix
-
Create the
.envfileAt the project root, copy the template if there is no
.envyet. Do not overwrite it if it already exists.# macOS / Linux cp -n .env.example .env # Windows (PowerShell) if (-not (Test-Path .env)) { Copy-Item .env.example .env } -
Generate the key
The command creates a random key and writes it to
APP_KEY=base64:...in.env. In production, it asks for confirmation: never regenerate a key that is already in use, or existing sessions, cookies and encrypted data become unreadable.php artisan key:generate -
Clear the config cache
If the configuration was cached, Laravel still reads the old empty value. Clear the cache and reload the page.
php artisan config:clear # or everything at once php artisan optimize:clear -
Production and Docker: provide the key through an environment variable
Generate a key without changing any file with
--show, then store it in your platform’s secrets (container, Forge or CI environment variables). Use the same key for every instance of the same application, then runphp artisan config:cacheagain.php artisan key:generate --show # base64:3q2+7w...= -> store it as the APP_KEY secret -
Check the key format
The line must start with
base64:, with no extra spaces or quotes. Foraes-256-cbc, the part afterbase64:decodes to 32 bytes.APP_KEY=base64:REPLACE_WITH_THE_GENERATED_VALUE APP_CIPHER=AES-256-CBC -
Tests: add a key to the test environment
If a
.env.testingfile exists, Laravel uses it instead of.envduring tests: it needs anAPP_KEYtoo. You can generate it directly for that environment.php artisan key:generate --env=testing
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Add
php artisan key:generateto the project setup script (README,composer.jsonor Makefile) for every new machine. - Store
APP_KEYin a secrets manager in production and never commit.env. - To rotate the key without breaking sessions, use
APP_PREVIOUS_KEYS(Laravel 11+), which lets Laravel decrypt with the old key during the transition.
Frequently asked questions
Can I regenerate APP_KEY in production?
Only if you accept logging out every user and losing data encrypted with the old key. Otherwise, put the old key in APP_PREVIOUS_KEYS before replacing it.
I ran key:generate but the error remains, why?
The configuration is probably cached: run php artisan config:clear. Also check that the web server reads the same .env (same folder, same container).
Should I commit APP_KEY to the repository?
No. The key lets anyone decrypt cookies and forge sessions. Each environment has its own key, kept out of the repository.
Useful tools for this error
More PHP & Laravel errors
- Fix "419 Page Expired" in Laravel (CSRF token mismatch)
- Fix "Allowed memory size of 134217728 bytes exhausted" in PHP
- Fix "Target class [X] does not exist" and "Class not found" in Laravel
- Fix "SQLSTATE[HY000] [2002] Connection refused" in Laravel
- Fix "Your requirements could not be resolved to an installable set of packages" (Composer)
- Fix "laravel.log could not be opened in append mode: Permission denied"
- Fix "Vite manifest not found at: public/build/manifest.json" in Laravel