composer 连不上私有 git 仓库是因为默认走 https 而私有仓库需 ssh 认证,需确保 ssh 密钥已加载、git clone 手动成功,并配置 git url.insteadof 将 https 自动转为 ssh。

Composer 为什么连不上私有 Git 仓库?
因为默认走 HTTPS,而私有仓库(比如 GitHub/GitLab 私有项目、自建 Git 服务)往往要求 SSH 认证。Composer 在执行 composer install 或 composer update 时,若 composer.json 中的 repositories 指向 git@xxx:org/repo.git 这类 SSH URL,就会尝试用系统 SSH 配置拉取代码——但很多人没配好密钥或没启用 agent 转发,结果卡在 Cloning failed using an ssh command 或直接超时。
确保 SSH 密钥已加载且能手动克隆
这是最常被跳过的验证步骤。Composer 不会报错说“密钥没加”,它只是静默失败。
- 运行
ssh -T git@github.com(或你的 Git 服务商域名),确认输出类似Hi username! You've successfully authenticated... - 如果提示
Permission denied (publickey),说明密钥没加载:ssh-add -l查看是否列出密钥;没列出就运行ssh-add ~/.ssh/id_rsa(路径按实际调整) - 检查
~/.ssh/config是否为该主机配置了正确的IdentityFile和User,例如:Host github.com User git IdentityFile ~/.ssh/github_ed25519
- Git 必须能手动
git clone git@github.com:org/private-repo.git成功,Composer 才可能成功
Composer 不要改 URL,要改 Git 全局配置
别在 composer.json 里把 SSH 地址硬改成 HTTPS 来绕过问题——这会丢失对私有包的访问权限,也破坏了语义。正确做法是让 Git 自动把 HTTPS 请求重写成 SSH:
工作流任务拆解与模型编排技能。使用场景:(1) 收到复杂工作任务需要拆解为可执行步骤,(2) 需要为不同步骤选择最合适的模型,(3) 需要跟踪工作流进度和模型使用情况,(4) 长时间任务卡住需要问题诊断和解决方案。
- 运行
git config --global url."git@github.com:".insteadOf "https://github.com/" - 对 GitLab:运行
git config --global url."git@gitlab.com:".insteadOf "https://gitlab.com/" - 这样即使
composer.json写的是"https://github.com/org/private-repo",Git 也会自动转成git@github.com:org/private-repo.git并走 SSH 认证 - 注意:这个配置影响所有 Git 操作,不是 Composer 独有;如需局部控制,可用
--local替代--global
CI/CD 环境下容易漏掉的关键点
本地能跑不等于 CI 能跑。很多团队在 GitHub Actions / GitLab CI 里遇到 “Host key verification failed” 或 “Could not open a connection to your authentication agent”。
-
ssh-keyscan必须提前运行:例如在 GitHub Actions 中加一步ssh-keyscan github.com >> ~/.ssh/known_hosts,否则首次连接会卡住 - SSH agent 必须显式启用:GitHub Actions 需用
ssh-agent bash -c 'ssh-add /path/to/key; composer install',不能只靠ssh-add - 私钥文件权限必须是
0600,否则ssh-add会拒绝加载;CI 中从 secrets 注入的密钥要先chmod 600 $KEY_PATH - 不要在
composer.json的repositories里写ssh://前缀——Composer 不识别,应统一用git@host:path.git格式
SSH 密钥认证本身不复杂,但 Composer 是借 Git 的壳做事,所以真正要调通的,是 Git + SSH + 系统环境三者的配合。任何一环没对齐,就会表现为 Composer “莫名其妙拉不下包”。










