执行php artisan key:generate时PHP内存耗尽报错求助
php artisan key:generate Hey there, let's get this memory issue sorted out for you! First off, 32MB of memory is way too low for Laravel to run its commands—Laravel's core dependencies alone need more than that to load properly. Tweaking the bootstrap/autoload.php file isn't the right fix here (and it'll get overwritten the next time you run Composer updates anyway), so let's focus on the actual solutions:
Quick Temporary Fix
If you just need to run the command once without changing any config files, add a memory limit flag directly to your command:
php -d memory_limit=512M artisan key:generate
This sets a 512MB limit just for this single command, which should be more than enough to generate the app key.
Permanent Global Fix
To make the change stick for all PHP commands and your Laravel app:
- Locate your
php.inifile. You can find its path by running:
Look for the "Loaded Configuration File" line in the output.php --ini - Open the file and search for the
memory_limitdirective. Update it to:memory_limit = 512M - Restart your web server (like Apache/Nginx) or PHP-FPM service if you're using it, so the changes take effect.
Composer-Specific Memory Adjustment
If you're running Artisan commands via Composer (like during deployment), you can also set a higher memory limit for Composer itself:
COMPOSER_MEMORY_LIMIT=-1 composer run-script post-install-cmd
The -1 means "unlimited" memory, which is safe for one-off commands like this.
Why You Shouldn't Edit bootstrap/autoload.php
That file is generated and managed by Laravel and Composer. Any changes you make here will be erased the next time you run composer install or composer update. Memory limits are a PHP configuration setting, not something that should be adjusted in framework files.
If you still run into issues after increasing the memory limit, double-check that you're editing the correct php.ini file (sometimes systems have separate ones for CLI and web PHP) or try updating your Laravel version to the latest stable release—older versions might have less efficient memory usage.
内容的提问来源于stack exchange,提问作者EgK

