彻底解决 Composer 太慢问题

| 选择喜欢的代码风格  

Composer 命令使用帮助


   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.9.1 2019-11-01 17:20:17

Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
      --no-cache                 Prevent use of the cache
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  about                Shows the short information about Composer.
  archive              Creates an archive of this composer package.
  browse               Opens the package's repository URL or homepage in your browser.
  check-platform-reqs  Check that platform requirements are satisfied.
  clear-cache          Clears composer's internal package cache.
  clearcache           Clears composer's internal package cache.
  config               Sets config options.
  create-project       Creates new project from a package into given directory.
  depends              Shows which packages cause the given package to be installed.
  diagnose             Diagnoses the system to identify common errors.
  dump-autoload        Dumps the autoloader.
  dumpautoload         Dumps the autoloader.
  exec                 Executes a vendored binary/script.
  global               Allows running commands in the global composer dir ($COMPOSER_HOME).
  help                 Displays help for a command
  home                 Opens the package's repository URL or homepage in your browser.
  i                    Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
  info                 Shows information about packages.
  init                 Creates a basic composer.json file in current directory.
  install              Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
  licenses             Shows information about licenses of dependencies.
  list                 Lists commands
  outdated             Shows a list of installed packages that have updates available, including their latest version.
  prohibits            Shows which packages prevent the given package from being installed.
  remove               Removes a package from the require or require-dev.
  require              Adds required packages to your composer.json and installs them.
  run                  Runs the scripts defined in composer.json.
  run-script           Runs the scripts defined in composer.json.
  search               Searches for packages.
  self-update          Updates composer.phar to the latest version.
  selfupdate           Updates composer.phar to the latest version.
  show                 Shows information about packages.
  status               Shows a list of locally modified packages, for packages installed from source.
  suggests             Shows package suggestions.
  u                    Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  update               Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  upgrade              Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  validate             Validates a composer.json and composer.lock.
  why                  Shows which packages cause the given package to be installed.
  why-not              Shows which packages prevent the given package from being installed.

Composer 命令执行后,无穷无尽的等待..


Loading composer repositories with package information
Updating dependencies (including require-dev)

composer 命令,建议带上 -vvv


建议在使用 composer 命令时带上 -vvv 参数,这个可以输出更多详细信息,好知道究竟卡在什么位置。

通过国内镜像,或使用代理,可以改善 composer 速度:
#比如 Composer 在安装 Laravel 的时候,加上 -vvv 知道问题所在:

$ composer -vvv global require laravel/installer                                                                                
Changed current directory to C:/Users/Admin/AppData/Roaming/Composer                                                            
Reading ./composer.json                                                                                                         
Loading config file ./composer.json                                                                                             
Checked CA file C:\Users\Admin\AppData\Local\Temp\ope435C.tmp: valid                                                            
Executing command (C:\Users\Admin\AppData\Roaming\Composer): git branch --no-color --no-abbrev -v                               
Executing command (C:\Users\Admin\AppData\Roaming\Composer): git describe --exact-match --tags                                  
Executing command (C:\Users\Admin\AppData\Roaming\Composer): git log --pretty="%H" -n1 HEAD                                     
Executing command (C:\Users\Admin\AppData\Roaming\Composer): hg branch                                                          
Executing command (C:\Users\Admin\AppData\Roaming\Composer): fossil branch list                                                 
Executing command (C:\Users\Admin\AppData\Roaming\Composer): fossil tag list                                                    
Executing command (C:\Users\Admin\AppData\Roaming\Composer): svn info --xml                                                     
Running 1.9.1 (2019-11-01 17:20:17) with PHP 7.4.0 on Windows NT / 10.0                                                         
Downloading https://repo.packagist.org/packages.json                                                                            
Writing C:/Users/Admin/AppData/Local/Composer/repo/https---repo.packagist.org/packages.json into cache                          
Reading C:/Users/Admin/AppData/Local/Composer/repo/https---repo.packagist.org/p-provider-2013.json from cache                   
Reading C:/Users/Admin/AppData/Local/Composer/repo/https---repo.packagist.org/p-provider-2014.json from cache                   

卡在这里不动了---->Downloading http://repo.packagist.org/p/provider-2015%247e1cd959b209e2e9508ccfa24641db7811eed0b60d7561a28bb0b485b69aa0f5.json
....

^C终止批处理操作吗(Y/N)? y
阿里云
阿里云官方已经推出了 Composer 全量镜像,使用说明:https://mirrors.aliyun.com/composer

# -g 表示全局,去除该参数则只对当前项目生效
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

#取消镜像:
composer config -g --unset repos.packagist

调试
# 追加参数 -vvv 可输出详细的信息
composer -vvv require alibabacloud/sdk

华为云
composer config -g repo.packagist composer https://mirrors.huaweicloud.com/repository/php/

腾讯云
composer config -g repos.packagist composer https://mirrors.cloud.tencent.com/composer/

cnpkg
composer config -g repos.packagist composer https://php.cnpkg.org](https://php.cnpkg.org

phpcomposer
Composer 官方镜像,速度较慢,国外服务器用的是这个:
composer config -g repo.packagist composer https://packagist.phpcomposer.com

composer 使用代理


使用代理前,将镜像切换成官方镜像,也就是上面最后一个,然后在你的代理程序上开一个本地代理端口(局域网代理也行),然后配置一下终端代理:

#Linux:
export http_proxy=http://127.0.0.1:1080
export https_proxy=http://127.0.0.1:1080

#Window命令行:
set http_proxy=http://127.0.0.1:1080
set https_proxy=http://127.0.0.1:1080

注意上面的操作只在当前 session 生效,新建或切换一个 tab 后会失效。如果想永久生效,记得修改 ~/.bashsrc


建议只是临时使用这种方式,强烈推荐方式一的全量镜像。

之后,就可以愉快的用 Laravel 创建项目了:

$ laravel new pizzahouse                                                               
Crafting application...                                                                
Loading composer repositories with package information                                 
Installing dependencies (including require-dev) from lock file                         
Package operations: 97 installs, 0 updates, 0 removals                                 
  - Installing doctrine/inflector (2.0.3): Downloading (100%)                          
  - Installing doctrine/lexer (1.2.1): Downloading (100%)                              
  - Installing dragonmantank/cron-expression (v2.3.0): Downloading (100%)              
  - Installing voku/portable-ascii (1.5.2): Downloading (100%)                         
  - Installing symfony/polyfill-ctype (v1.17.1): Loading from cache                    
  - Installing phpoption/phpoption (1.7.4): Downloading (100%)                         
  - Installing vlucas/phpdotenv (v4.1.7): Downloading (100%)                           
  - Installing symfony/css-selector (v5.1.2): Loading from cache                       
  - Installing tijsverkoyen/css-to-inline-styles (2.2.2): Downloading (100%)           
  - Installing symfony/polyfill-php80 (v1.17.1): Loading from cache                    
  - Installing symfony/polyfill-mbstring (v1.17.1): Loading from cache                 
  - Installing symfony/var-dumper (v5.1.2): Downloading (100%)                         
  - Installing symfony/deprecation-contracts (v2.1.2): Downloading (100%)              
  - Installing symfony/routing (v5.1.2): Downloading (100%)                            
  - Installing symfony/process (v5.1.2): Downloading (100%)                            
  - Installing symfony/polyfill-php73 (v1.17.1): Downloading (100%)                    
  - Installing symfony/polyfill-php72 (v1.17.0): Loading from cache                    
  - Installing symfony/polyfill-intl-idn (v1.17.1): Loading from cache                 
  - Installing symfony/mime (v5.1.2): Downloading (100%)                               
  - Installing symfony/http-foundation (v5.1.2): Downloading (100%)                    
  - Installing psr/event-dispatcher (1.0.0): Downloading (100%)                        
  - Installing symfony/event-dispatcher-contracts (v2.1.2): Downloading (100%)         
  - Installing symfony/event-dispatcher (v5.1.2): Downloading (100%)                   
  - Installing psr/log (1.1.3): Loading from cache

  ....

Composer 扩展阅读:




发表评论