答案是SSH配置或权限问题导致,需检查SSH密钥是否生成并添加到GitHub/GitLab,测试ssh -T git@github.com连接;确保Composer使用正确协议,可改为HTTPS或配置全局协议;确认Git能手动克隆仓库;若用HTTPS需配置个人访问令牌(PAT)并设置composer github-oauth。

当你在使用 Composer 安装依赖时遇到 "Could not read from remote repository" 错误,通常是因为 Git 无法通过 SSH 访问远程仓库(比如 GitHub、GitLab 等),这多数是 SSH 配置或权限问题导致的。下面是一些常见原因和解决方法。
Composer 在拉取私有仓库时默认使用 SSH 协议(如 git@github.com:username/repo.git),需要本地有对应的 SSH 密钥,并且已添加到代码平台(如 GitHub)。
ssh-keygen -t ed25519 -C "your_email@example.com"
~/.ssh/id_ed25519.pub)内容复制并添加到你的 GitHub/GitLab 账户的 SSH Keys 设置中。
• 测试连接:
ssh -T git@github.com
Hi username! You've successfully authenticated... 说明 SSH 配置正常。
有时 Composer 会尝试用 SSH 拉取本应公开的包,但如果你没有权限或未登录,就会失败。你可以强制 Composer 使用 HTTPS 协议代替 SSH。
• 修改composer.json 中的仓库地址,从:
"url": "git@github.com:username/private-repo.git"
"url": "https://github.com/username/private-repo.git"
composer config --global github-protocols https
git config --global url."https://github.com/".insteadOf "git@github.com:"
这样所有 SSH 地址都会自动转为 HTTPS,适合公司代理或无密钥环境。
手动测试一下 Git 是否能克隆该仓库:
git clone git@github.com:username/private-repo.git
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
如果你改用 HTTPS 方式访问私有仓库,GitHub 已不再支持密码认证,需使用 Personal Access Token (PAT)。
• 创建 PAT:进入 GitHub → Settings → Developer settings → Personal access tokens → Tokens (classic) → 生成一个带repo 权限的 token。
• 使用时将 token 当作密码:
https://TOKEN@github.com/username/private-repo.git
composer config --global github-oauth.github.com YOUR-TOKEN-HERE
这是 Composer 推荐的安全方式,避免频繁输入凭证。
基本上就这些。重点排查 SSH 密钥、Git 协议、远程权限和 Composer 配置。只要 Git 能手动克隆,Composer 一般就没问题。
以上就是composer 提示 "Could not read from remote repository" Git 权限问题的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号