Laravel: No application encryption key has been specified
Laravel's encrypter has no key, because the APP_KEY environment variable is empty on the server. Generate one with php artisan key:generate --show, set it as APP_KEY where the app runs, clear any cached configuration, and keep the same key from then on.
By Livops team, the people who build and run LivopsUpdated
The exception, as Laravel shows it on every page that uses cookies or the session:
Illuminate\Encryption\MissingAppKeyException
No application encryption key has been specified.
This page explains which key is missing, lists the causes from the most common, shows how to check the key your app really loads, and says what Livops does for Laravel projects.
What does the error mean?
Laravel encrypts cookies, including the session cookie, with the key in the key option of config/app.php, and that option is driven by the APP_KEY environment variable[1]. The key is normally generated during Laravel’s installation[1], into the .env file. .env is not committed, so a fresh server starts with no key, and every request that touches encryption fails with this exception.
Causes, most common first
1. APP_KEY is not set on the server
Generate a key once, with the command Laravel recommends, because it uses PHP’s secure random generator[1]. --show prints it instead of writing it into .env:
php artisan key:generate --show
# prints base64: followed by 44 characters
Set the printed value as APP_KEY in the server’s environment, and keep it. Changing the key logs every user out, and values encrypted with the old key can no longer be decrypted; if you must rotate, list the old ones in APP_PREVIOUS_KEYS[1].
2. The configuration was cached without the key
php artisan config:cache belongs in a production deployment[2][3]. Once the configuration is cached, .env is not loaded, and env() returns only real system environment variables[2]. If the cache was made during a build, or before APP_KEY was set, it holds an empty key. Clear it, and cache it again where the key exists:
php artisan config:clear
php artisan config:cache
3. Code calls env() outside the config files
After config:cache, every env() call for a .env variable returns null; only calls inside the configuration files are safe[3]. A package or your own code that reads env('APP_KEY') directly breaks the moment the config is cached. Read config('app.key') instead.
4. The value is empty or malformed
APP_KEY= copied from .env.example, quotes around it, or a key without the base64: prefix. An empty value gives this error; a wrong length gives Unsupported cipher or incorrect key length. Generate a fresh one as in cause 1.
How to confirm the fix locally
Check the key the application really loads, not the one in a file:
php artisan config:clear
APP_KEY="base64:..." php artisan tinker --execute="var_dump(strlen(config('app.key')) > 0);"
bool(true) means the key is there. To reproduce the server, remove APP_KEY from .env and open any page: you get the exception; put it back and it is gone.
On Livops
- The key is generated when the project is created. When Livops detects a Laravel project (
artisanbesidecomposer.json, built without your own Dockerfile) and creates it, it generatesAPP_KEYthe waykey:generatedoes —base64:and 32 random bytes — and saves it as a secret variable, unless you gave a value. Projects created from a stack get one too. - The config is cached where the variables are. The start command runs
php artisan optimizeas the app starts, with the project’s variables in place, so cause 2 does not happen with Livops’ own start command. - So on Livops this error usually means one of three things: the project builds from your own Dockerfile (no key is generated for it),
APP_KEYwas removed or emptied in Variables, or your Dockerfile caches the config during the build. SetAPP_KEYunder Variables, mark it secret, and deploy. - The deploy log warns you. After a deploy goes live, Livops opens the home page. If it answers with a server error, the deploy log says the app is running but its home page answers 500, with the app’s last log lines under it.
- The assistant explains, it does not invent keys. It can read the log and point you to Variables. It sets a variable only to a value you wrote in the same message, and only after you press its card.
More on missing variables: A variable is not set.
Questions
Why does it work locally?
Laravel's installer wrote APP_KEY into your local .env file. That file is not committed, so the server never got the key.
Can I generate a new key on every deploy?
No. Changing the key logs out every user and makes values encrypted with the old key unreadable. Generate it once, keep it in the server's environment, and list old keys in APP_PREVIOUS_KEYS if you ever rotate.
I set APP_KEY and still see the error. Why?
The configuration was probably cached before the key existed. A cached config does not read .env, so run php artisan config:clear, or cache it again in an environment that has the key.
What format does APP_KEY have?
base64: followed by 32 random bytes in base64, the way php artisan key:generate writes it. A key without the prefix or of the wrong length gives a different error, Unsupported cipher or incorrect key length.
Does Livops set APP_KEY?
Yes, when it detects a Laravel project and creates it, unless you gave a value. It generates the key the way key:generate does and saves it as a secret variable.
Sources
- Laravel docs — Encryption (laravel.com). Checked .
- Laravel docs — Configuration, configuration caching (laravel.com). Checked .
- Laravel docs — Deployment (laravel.com). Checked .
Put your project online today
Connect GitHub or drop a folder. No card is asked for.
Deploy your Laravel appRelated
- App not listening on $PORT: why the health check failed after deployThe platform started your app, tried the port it gave it, and nothing answered in time. Almost always the app listens on its own fixed port instead of the PORT variable, listens on localhost (127.0.0.1) instead of 0.0.0.0, or crashed or was still starting when the check gave up. Listen on 0.0.0.0 and the port in PORT, and it passes.
- npm ERR! code 1: why the build failed with exit code 1npm ERR! code 1 (npm error code 1 in newer npm) only says that a script npm ran exited with code 1. The real error is printed above it: usually a compile or type error, a lockfile out of sync with package.json, or a dependency whose install script failed. Find the first error line above code 1, fix that, and the build passes.
- exec format error in Docker: the image was built for another CPUThe kernel refused to run the container's program because it is not an executable for this machine: almost always a binary or image built for arm64 (an Apple Silicon Mac) running on an amd64 server, or the reverse. Build for the server's platform with --platform linux/amd64, or as a multi-platform image; if the file is a script, give it a #! line.