临时设置:在命令前添加环境变量,如 Linux/macOS 下使用 COMPOSER_MEMORY_LIMIT=-1 composer install;2. 永久设置:Linux/macOS 在 ~/.bashrc 等文件中添加 export COMPOSER_MEMORY_LIMIT=-1,Windows 在系统环境变量中添加变量名 COMPOSER_MEMORY_LIMIT 和值 -1;3. 可用值包括 -1(无限制)、512M、2G 等,但不能超过 php.ini 中 memory_limit 的设定。

要设置 Composer 的内存限制环境变量 COMPOSER_MEMORY_LIMIT,你可以通过在运行 Composer 命令前临时设置,或在系统/用户环境中永久设置。
1. 临时设置(当前终端会话)
在执行 Composer 命令时,直接在命令前加上环境变量:Windows(CMD):
set COMPOSER_MEMORY_LIMIT=-1 && composer installWindows(PowerShell):
$env:COMPOSER_MEMORY_LIMIT="-1"; composer installLinux / macOS:
2. 永久设置环境变量
如果你想一劳永逸地避免内存限制,可以将环境变量写入系统或用户的环境配置中。Linux / macOS:
编辑 shell 配置文件(如 ~/.bashrc、~/.zshrc 或 ~/.profile),添加: export COMPOSER_MEMORY_LIMIT=-1 保存后运行 source ~/.bashrc(或对应文件)使其生效。Windows:
打开“系统属性” → “高级” → “环境变量”,在用户或系统变量中添加:- 变量名:COMPOSER_MEMORY_LIMIT
- 变量值:-1(表示无限制)或 2G、4G 等具体值
3. 可用值说明
COMPOSER_MEMORY_LIMIT 支持以下格式:- -1:禁用内存限制(推荐开发环境使用)
- 512M:限制为 512MB
- 2G:限制为 2GB
Composer 默认使用 PHP 的 memory_limit,设置 COMPOSER_MEMORY_LIMIT 可覆盖该限制。遇到 "Allowed memory size exhausted" 错误时,优先考虑此变量。
基本上就这些。










