Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kennybjr87's avatar

RuntimeException: No application encryption key has been specified 5.8

I downloaded a brand new install of laravel 5.8 before even doing any coding at all running phpunit on the ExampleTest is failing with a RuntimeException: No application encryption key has been specified.

and yes there is a key in my .env it generated when I did the new install. I even tried to remove the key and generated a new one. It's still failing...

0 likes
19 replies
Snapey's avatar

try clearing the config cache? php artisan config:clear

Snapey's avatar

If thats no good, check the key with php artisan tinker and then config('app.key')

And if thats no good, check you have the right php encryption libaries

rdelorier's avatar

Kinda interesting, I can reproduce this is I use the globally installed version of phpunit but it works fine using the local version

global producing error

/tmp/test$ phpunit
PHPUnit 7.5.6 by Sebastian Bergmann and contributors.

.E                                                                  2 / 2 (100%)

Time: 169 ms, Memory: 18.00 MB

There was 1 error:

1) Tests\Feature\ExampleTest::testBasicTest
RuntimeException: No application encryption key has been specified.

/private/tmp/test/vendor/laravel/framework/src/Illuminate/Encryption/EncryptionServiceProvider.php:44
/Users/rdelorier/.composer/vendor/illuminate/support/helpers.php:1027
/private/tmp/test/vendor/laravel/framework/src/Illuminate/Encryption/EncryptionServiceProvider.php:48
/private/tmp/test/vendor/laravel/framework/src/Illuminate/Encryption/EncryptionServiceProvider.php:24
/private/tmp/test/vendor/laravel/framework/src/Illuminate/Container/Container.php:791
/private/tmp/test/vendor/laravel/framework/src/Illuminate/Container/Container.php:671
/private/tmp/test/vendor/laravel/framework/src/Illuminate/Container/Container.php:619
/private/tmp/test/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:757
/private/tmp/test/vendor/laravel/framework/src/Illuminate/Container/Container.php:929
/private/tmp/test/vendor/laravel/framework/src/Illuminate/Container/Container.php:857
/private/tmp/test/vendor/laravel/framework/src/Illuminate/Container/Container.php:822
/private/tmp/test/vendor/laravel/framework/src/Illuminate/Container/Container.php:671
/private/tmp/test/vendor/laravel/framework/src/Illuminate/Container/Container.php:619
/private/tmp/test/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:757
/private/tmp/test/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:215
/private/tmp/test/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:189
/private/tmp/test/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:354
/private/tmp/test/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:170
/private/tmp/test/tests/Feature/ExampleTest.php:17
/Users/rdelorier/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:208
/Users/rdelorier/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:164

ERRORS!
Tests: 2, Assertions: 1, Errors: 1.

But local version is fine:

/tmp/test$ ./vendor/bin/phpunit
PHPUnit 7.5.6 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 154 ms, Memory: 16.00 MB

OK (2 tests, 2 assertions)
kennybjr87's avatar

@RDELORIER - I never even thought to use a local version. It works for me as well. If you happen to get global working could you please let me know?

rdelorier's avatar
Level 9

@KENNYBJR87 - I was able to get global phpunit working by upgrading laravel/valet globally

4jZW7jVSdS4U6PC's avatar

Do you have

    <php>
        <env name="APP_ENV" value="testing"/>
    </php>

In your phpunit.xml file? If yes, check your .env.testing or .env for APP_KEY

1 like
Vehikl's avatar

For some reason on a recent project php artisan key:generate didn't actually insert the key to the .env automatically. So I did the following:

php artisan key:generate --show

then copied the displayed key over to APP_KEY=... in the .env. Voila, all good :)

tenh's avatar

@snapey for me your answer is the best but unfortunately I am not allowed to click hit best anwser button for you.

worgor's avatar

I had this issue completly random - from time to time the log sai "No application encryption Key .... " ... this error was annoying since the user was logged out or warped to the login page. So i what i found out was that the .env file was, for some reason, the issue of the problem. Either it was not available at the time the infos where needed or it was something else - i did what Vehikl did - created a new Key, copied it into the .env file and the i copied the key into the app.php as a default value for the key 'key' => env('APP_KEY','base64:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'). Until now it works perfect and since this is only a hack or a solving of symptoms but not the root cause, i really wonder why this issues was triggered - this never happend when i was using the local web server (php artisan serve) but with wamp maybe things are different?

rocketvalley's avatar

I think I may have a similar issue to @worgor - In 99.9% of cases, everything works fine, however, on a page which has many requests (e.g. a lot of images), 1 or 2 are failing sometimes with a MissingAppKeyException.

Really strange, I also saw in my logs that it was no only missing API key, it seemed to be other vars from env that were not being found. Again... only sometimes.

I wonder if it is the number of requests that is the issue.

ElMarkesDeSade's avatar

Using PHPunit and having phpunit.xml you can specify a key for testing purposes there :

 <php>
        <env name="APP_ENV" value="testing"/>
        <env name="APP_KEY" value="12345678901234567890123456789012"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="QUEUE_CONNECTION" value="sync"/>
        <env name="ENV_FILE" value=".env.pipeline" />
    </php>
2 likes
kennedyjr's avatar

if you added the key, but it still doesn't work - clear caches

php artisan optimize:clear

Please or to participate in this conversation.