composer install卡在“loading composer repositories”主因是未同时配置http-proxy和https-proxy,因packagist全量走https,缺一即静默直连超时;正确命令为composer config -g http-proxy http://127.0.0.1:7890与composer config -g https-proxy http://127.0.0.1:7890,且二者不可与镜像共存。

开发机上配 Composer 代理,90% 的问题不是代理没开,而是只配了 http-proxy 却漏掉 https-proxy——Packagist 全量走 HTTPS,缺一个就卡死。
为什么 composer install 卡在 “Loading composer repositories”
这不是 Composer 慢,是它直连 https://packagist.org 时被拦截或超时。现象包括:Resolving dependencies 长时间不动、cURL error 35、Failed to decode response,甚至无报错静默卡住。
根本原因:Composer 不读系统 HTTP_PROXY 环境变量;http-proxy 只管 HTTP 请求,对 HTTPS 完全无效;必须同时配置 http-proxy 和 https-proxy,且值都得是 http:// 协议开头(哪怕代理本身监听 TLS 端口)。
- 填成
https://127.0.0.1:8080或漏协议头(如127.0.0.1:8080)→ 静默 fallback 直连 → 最终超时 - 只执行一条
composer config -g http-proxy ...→ HTTPS 请求失败,但错误不明显 - 代理需支持 CONNECT 隧道(Clash、Squid 4.0+ 可用;普通 HTTP 代理不行)
全局代理配置的正确命令写法
别手改 ~/.composer/config.json,用 composer config 命令最安全。两条必须一起跑:
composer config -g http-proxy http://127.0.0.1:7890 composer config -g https-proxy http://127.0.0.1:7890
带认证?URL 写成 http://user:pass@127.0.0.1:7890;密码含 @、/、: 必须 URL 编码,例如 pa@ss/word → pa%40ss%2Fword,可用 php -r "echo rawurlencode('pa@ss/word');" 生成。
验证是否生效:composer config -g --list | grep -E "(http|https)-proxy",输出里必须两行都存在、协议和端口都对。
腾讯云/CVM 开发机更推荐换镜像,不是配代理
你在腾讯云 CVM 上跑 composer install,大概率不是网络不通,而是 DNS 解析慢或 TLS 握手卡在海外节点。配代理多一层故障点,还容易因证书、隧道、认证等问题反复排查。
直接切腾讯云官方镜像,快且稳定:
composer config -g repo.packagist composer https://mirrors.cloud.tencent.com/composer/ composer clear-cache
注意:https://mirrors.cloud.tencent.com/composer/ 末尾的 / 不能少,少了会报 Invalid repository type。
如果项目根目录 composer.json 里有 repositories 字段,它会覆盖全局配置,得先删掉或注释掉。
NTLM 代理(如公司域环境)怎么办
Composer 原生不支持 NTLM 认证,设了 http-proxy 也会返回 407 Proxy Authentication Required 或 Unable to connect。
不能靠改 Composer 配置解决,必须引入中转代理工具:
- Windows 推荐
cntlm或px,让它们监听127.0.0.1:3128并处理 NTLM,再让 Composer 连这个本地地址 - 临时验证:运行
curl -x http://127.0.0.1:3128 -I https://packagist.org/packages.json,如果curl也报407,说明中转层没配好 - 别指望
HTTP_PROXY环境变量能绕过——Composer 明确忽略它
真正容易被忽略的点是:镜像和代理不可共存;一旦 repo.packagist 被设为镜像,http-proxy 和 https-proxy 就完全失效——很多人配了代理又切了镜像,结果白忙活。











