核心问题是composer默认连接境外packagist.org受网络影响,换阿里云镜像可显著提速;需执行composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/(末尾斜杠不可省)、清缓存并验证生效,卡在“installing dependencies”多因github api限流,应配github_token或改用git克隆。

国内部署 Laravel 时下载慢,核心问题不在 Laravel 本身,而是 Composer 默认连接境外 packagist.org,受网络波动和 DNS 解析影响严重。换阿里云镜像能明显提速,但必须按步骤操作到位,否则可能报错或无效。
全局配置阿里云镜像(推荐,一劳永逸)
这条命令让所有后续 Composer 操作自动走国内源:
- 执行:composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/(注意末尾斜杠 / 不可省,漏掉会触发 Could not parse version constraint 错误)
- 清缓存(非强制但强烈建议):composer clear-cache
- 验证是否生效:composer config -g repo.packagist,输出应为包含
"url": "https://mirrors.aliyun.com/composer/"的 JSON 对象
创建项目时进一步提速
即使已配镜像,加参数仍可优化下载体验:
- 用 --prefer-dist 优先下载压缩包而非 Git 克隆:composer create-project --prefer-dist laravel/laravel myapp
- 如需指定 Laravel 版本(例如 11.x),显式加版本约束:composer create-project --prefer-dist laravel/laravel:^11.0 myapp
遇到卡在 “Installing dependencies” 或 “Cloning Laravel repository”?别急着换镜像
这大概率不是镜像问题,而是 GitHub API 限流导致——尤其未配置 GITHUB_TOKEN 时:
- 给 Composer 配 token:composer config -g github-oauth.github.com your_actual_token_here
- 或跳过 installer,改用 Git 直接克隆(更稳定):git clone --depth=1 https://github.com/laravel/laravel.git myapp,再进目录运行 composer install
- 顺便检查
api.github.com是否解析异常,可临时加 hosts:140.82.112.4 api.github.com
换源后还是慢?排查中间层干扰
阿里云镜像本身响应极快,若仍卡顿,可能是本地网络环境干扰:
- 测试镜像连通性:curl -I https://mirrors.aliyun.com/composer/packages.json,应秒回 HTTP 200
- 检查是否启用了 HTTPS 代理、公司防火墙或杀毒软件劫持 HTTPS 流量
- 若在 CI/CD(如 GitHub Actions)中使用,建议用临时参数方式,避免污染全局配置











