Composer安装

hcwei 2020年06月10日 278次浏览

应开发需求安装Composer,Composer是PHP中用来管理依赖(dependency)关系的工具。可以在自己的项目中声明所依赖的外部工具库(libraries),Composer会帮你安装这些依赖的库文件。

1.获取composer

curl -sS https://getcomposer.org/installer | php

注意:如果是编译安装的php,此处需指定php文件位置,上面代码就需要变成 

curl -sS https://getcomposer.org/installer | /usr/local/php/bin/php

如果不指定使用默认php程序。

2.移动到启动目录

mv composer.phar /usr/local/bin/composer

3.验证安装

composer -v

错误1:

[root@localhost ~]# composer require topthink/think-swoole

Do not run Composer as root/super user! See https://getcomposer.org/root for details

解决1:
不可以用root用户启动,切换成非root用户启动即可

错误2:

[www@ ~]$ composer require topthink/think-swoole

PHP Warning:  PHP Startup: Invalid library (maybe not a PHP library) 'gettext.so' in Unknown on line 0


  [ErrorException]  
  file_put_contents(./composer.json): failed to open stream: Permission denied  


require [--dev] [--prefer-source] [--prefer-dist] [--no-progress] [--no-suggest] [--no-update] [--no-scripts] [--update-no-dev] [--update-with-dependencies] [--update-with-all-dependencies] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--] [<packages>]...

解决2:
权限问题,用户需要有对当前文件夹的修改权限

请求依赖正确执行结果:

  1. 更换国内镜像
composer config -g repo.packagist composer https://packagist.laravel-china.org 
  1. 执行
composer require topthink/think-swoole

注意:不更换国内镜像可能会下载失败。