直接调高 process-timeout 和换国内镜像源可解决90%的Composer下载超时问题;process-timeout控制git clone、unzip等子进程超时,默认300秒且不可通过composer.json修改,必须全局配置或设环境变量;推荐使用阿里云或中科大镜像,二者稳定可用,而phpcomposer.com和laravel-china.org已停用或归档。

直接调高 process-timeout 和换国内镜像源,基本能解决 90% 的 Composer 下载超时问题。单纯加 --timeout 参数或只改 http.timeout 很可能无效,因为真正卡住的不是 HTTP 连接,而是 git clone 或 unzip 这类子进程。
为什么卡在 Downloading https:// 却要调 process-timeout?
Composer 把“下载一个包”拆成多步:先发 HTTP 请求拉 packages.json,再根据结果决定是走 git clone 还是下 ZIP;如果是 ZIP,还要解压、校验、写入 vendor。其中 git clone 和 unzip 是独立子进程,受 process-timeout 控制——它默认仅 300 秒,且不读取 composer.json 里的配置,必须全局或环境变量设置。
常见错误现象:[RuntimeException] The process "git clone --no-checkout ..." exceeded the timeout 或日志停在 Downloading https://mirrors.aliyun.com/composer/... 后无响应超过 5 分钟。
-
process-timeout控制每个外部命令(如git、unzip、php脚本)最大执行时间,单位秒 - 设为
0表示不限时,但不推荐——万一 Git 仓库彻底不可达,会无限挂起 - 调试建议先设
600,确认流程能走通再逐步提高到1800
换镜像源比加 timeout 更治本
国内直连 packagist.org 失败率高,不是“慢”,而是 DNS 解析失败、TLS 握手被重置、中间链路丢包。换源后大部分请求走国内 CDN,延迟从 800ms+ 降到 30ms 内,自然就不容易触发超时。
当前稳定可用的镜像只有两个:
- 阿里云:
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ - 中科大:
composer config -g repo.packagist composer https://mirrors.ustc.edu.cn/composer/
注意:https://packagist.phpcomposer.com 已于 2022 年下线,https://packagist.laravel-china.org 已归档,不要再用。
验证是否生效:composer config -g repo.packagist 应输出对应 URL;若项目含私有 Git 包,镜像源不影响它们,仍需单独处理 Git 超时。
Git 包下载失败时,光调 Composer 没用
当依赖里有 "type": "git"(比如开发版分支、私有仓库),Composer 会调系统 git clone,而该命令本身没有内置超时。一旦卡在 DNS 或 SSH 握手,process-timeout 才真正起作用——但它只能“杀掉进程”,不能让 Git 自己更快响应。
更有效的做法:
- 强制走 HTTPS:
composer config -g github-protocols https,避开 SSH 认证失败 - 给 Git 加连接超时(Linux/macOS):
git config --global core.sshCommand "timeout 300 ssh" - 配 GitHub Token 避免 API 限流:
{"github-oauth": {"github.com": "xxx"}}写进~/.composer/auth.json - 禁用
--prefer-source,优先用--prefer-dist下 ZIP 包
Windows 用户还需注意:长路径 + 杀软可能拦截 unzip,导致看似“卡住”,实为权限拒绝。
别漏掉 PHP 层的 socket 超时
Composer 底层用 PHP 的 cURL 或 stream 发请求,而 http.timeout(或 --http-basic-timeout)最终依赖 PHP 的 default_socket_timeout。如果这个值设成 60,那哪怕你把 http.timeout 设成 1200,PHP 也会在 60 秒后主动断开连接。
检查并同步调整:
- 查当前值:
php -i | grep default_socket_timeout - 临时改:启动命令加
-d default_socket_timeout=600 - 永久改:编辑
php.ini,设default_socket_timeout = 600
这个值必须 ≥ 你设的 http.timeout,否则 Composer 的超时配置形同虚设。
最常被忽略的是:换源后仍卡在 GitHub API 或私有 Git,误以为镜像没生效;其实镜像只管 Packagist 元数据和 ZIP 包,不代理 Git 操作。这时候得单独处理 Git 超时、Token、协议切换,而不是继续加 process-timeout。











