Composer配置国内镜像及版本更新

Composer在Windows上的安装还是很简单的,exe一键执行,但在版本更新以及插件下载上由于国内网络的现状,十有八九不是无法访问就是超时失败。我是很早前为了安装Laravel下载的Composer,结果在最近为了给Laravel安装PRedis插件时提示版本太旧,update更新时一直超时,后来发现国内有好心人搭了镜像,速度很快,基本和源站是同步的。

修改方法

Composer的配置文件包括系统全局配置具体项目配置

修改项目配置:

直接定位到项目目录,右键User Composer here或者cmd定位到项目目录,然后执行

  1. composer config repo.packagist composer https://packagist.phpcomposer.com

或者也可以直接打开项目目录下的composer.json文件,找到repositories,将这段配置整个替换为

  1. "repositories": {
  2. "packagist": {
  3. "type": "composer",
  4. "url": "https://packagist.phpcomposer.com"
  5. }
  6. }

修改全局配置

cmd执行命令

  1. composer config -g repo.packagist composer https://packagist.phpcomposer.com

或者找到Composer安装目录下的composer.json文件修改,如果忘记了Composer的安装目录,可以cmd执行

  1. composer config -l -g

表示列出(list)全局(global)配置(config)信息,大致如下

  1. C:WINDOWSsystem32>composer config -l -g
  2. You are running composer with xdebug enabled. This has a major impact on runtime
  3. performance. See https://getcomposer.org/xdebug
  4. [repositories.packagist.type] composer
  5. [repositories.packagist.url] https://packagist.phpcomposer.com
  6. [process-timeout] 300
  7. [use-include-path] false
  8. [preferred-install] auto
  9. [notify-on-install] true
  10. [github-protocols] [https, ssh, git]
  11. [vendor-dir] vendor (C:WINDOWSsystem32/vendor)
  12. [bin-dir] {$vendor-dir}/bin (C:WINDOWSsystem32/vendor/bin)
  13. [cache-dir] C:/Users/Administrator/AppData/Local/Composer
  14. [data-dir] C:/Users/Administrator/AppData/Roaming/Composer
  15. [cache-files-dir] {$cache-dir}/files (C:/Users/Administrator/AppData/Local/Compo
  16. ser/files)
  17. [cache-repo-dir] {$cache-dir}/repo (C:/Users/Administrator/AppData/Local/Compose
  18. r/repo)
  19. [cache-vcs-dir] {$cache-dir}/vcs (C:/Users/Administrator/AppData/Local/Composer/
  20. vcs)
  21. [cache-ttl] 15552000
  22. [cache-files-ttl] 15552000
  23. [cache-files-maxsize] 300MiB (314572800)
  24. [bin-compat] auto
  25. [discard-changes] false
  26. [autoloader-suffix]
  27. [sort-packages] false
  28. [optimize-autoloader] false
  29. [classmap-authoritative] false
  30. [prepend-autoloader] true
  31. [github-domains] [github.com]
  32. [bitbucket-expose-hostname] true
  33. [disable-tls] false
  34. [secure-http] false
  35. [cafile]
  36. [capath]
  37. [github-expose-hostname] true
  38. [gitlab-domains] [gitlab.com]
  39. [store-auths] prompt
  40. [archive-format] tar
  41. [archive-dir] .
  42. [home] C:/Users/Administrator/AppData/Roaming/Composer

其中最后一行就是Composer的安装位置。

最后执行composer selfupdate更新到最新版本1.2.1

  1. F:wwwelittle-master>composer selfupdate
  2. Your version of PHP, 5.4.17, is affected by CVE-2013-6420 and cannot safely perform certificate validation, we strongly suggest you upgrade.
  3. You are running composer with xdebug enabled. This has a major impact on runtime performance. See https://getcomposer.org/xdebug
  4. You are already using composer version 1.2.1 (stable channel).

备注

  1. 如果已经使用了Composer安装时的配置创建了项目,那么如果仅修改全局配置文件,项目中的composer.json优先级是更高的,所以要全部修改。
  2. 之前国内镜像是只支持http协议,所以还需要修改配置文件中的secure-http为false,现在不需要了,已经支持了https。
  3. Composer中文在线文档