0%

Linux安装composer遇到的坑

正常操作

1、下载composer

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

2、将composer.phar文件移动到bin目录以便全局使用composer命令 (重点)

1
mv composer.phar /usr/local/php/bin/composer    // 一定要找到对应的php版本的执行目录bin下面

3、切换国内源

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

避坑

1、执行命令:curl -sS https://getcomposer.org/installer | php

报错如下:

1
2
Some settings on your machine make Composer unable to work properly.Make sure that you fix the issues listed below and run this script again:
The openssl extension is missing, which means that secure HTTPS transfers are impossible.If possible you should enable it or recompile php with --with-openssl

没有openssl扩展
cd php7.0/ext/openssl // 进入到openssl文件,注意自己的php版本
phpize // 运行phpize

2、编译和安装

1
./configure --with-openssl --with-php-config=/usr/local/php/bin/php-config    // 编译和安装

遇到报错:

1
configure: error: Cannot find OpenSSL's <evp.h>

缺少openssl相关的库,直接安装即可

1
yum install openssl openssl-devel

遇到报错:

1
configure: error: mcrypt.h not found. Please reinstall libmcrypt

提示比较明确,缺少libmcrypt,直接安装即可

1
yum install libmcrypt

3、最后执行 make && make install

进入最后提示的目录 cp openssl.so /usr/local/php/include/php/ext
找到php.ini,在最后面添加如下内容: extension=openssl.so
重启 后查看扩展: php -m

4、最后

1
mv composer.phar /usr/local/php/bin/composer   //一定要找到对应的php版本的执行目录bin下面

5、设置别名

为防止多个php版本的存在,有设置别名的必要

1
2
3
4
5
vim ~/.zshrc

alias composer74="php /usr/local/Cellar/php@7.4/7.4.21_1/bin/composer.phar"

source ~/.zshrc