Home > Development Tools > composer > The most comprehensive and detailed Composer installation and configuration tutorial (win/linux)

The most comprehensive and detailed Composer installation and configuration tutorial (win/linux)

藏色散人
Release: 2021-10-27 16:01:07
forward
12883 people have browsed it

Installation and configuration of Composer under Windows and Linux

Introduction: Composer is a tool used by PHP to manage dependency relationships. You can declare the external libraries you depend on in your project, and Composer will install these dependent library files for you.

(1) Window installation

1.1 Download Composer

Composer official website download Composer.exe application (https ://getcomposer.org/)

1.2 Install Composer

The downloaded version is the latest Composer version and the PATH environment variable will be automatically set, so that you You can call Composer from any directory

  • Select the version according to the local php configuration, and then install it in the next step

  • ##Global configuration
①Search for “path”—>Open “Edit System Environment Variables”—“Environment Variables”

②Configure Administrator user variables


##1.3 Installation successful
# win+R 输入cmd 进入C:\Users\Administrator>composer
 ______
 / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
 /_/
Composer version 2.1.9 2021-10-05 09:47:38
Copy after login

(2)Linux installation

2.1 Download Composer
**# 进入根目录下载** [root@VM-12-9-centos /] curl -sS https://getcomposer.org/installer | php
Composer (version 2.1.9) successfully installed to: //composer.phar
Use it: php composer.phar #下载成功#友情提示:下载因为是外网可能会出现请求失败的情况,耐心多尝试下就成功了
Copy after login

If
    the download fails frequently
  • , you can use the Alibaba Cloud image download
#This image is synchronized with Packagist official in real time, it is recommended to use the latest Composer version
[root@VM-12-9-centos /] curl -sS mirrors.aliyun.com/composer/compos...


2.2 Global configuration
# 复制composer.phar到/usr/local/bin/目录下生成composer的目录,方便全局使用[root@VM-12-9-centos /] mv composer.phar /usr/local/bin/composer
Copy after login

2.3 Replacement Mirror source

Because foreign websites will be very slow in domestic requests, we can replace the domestic source to speed up the download of the source. There are many domestic mirror sources. Here are some commonly used ones. You can choose one of them according to your needs.

    Alibaba Cloud Composer Image
  • [Recommendation]
    composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
    Copy after login
  • 国产Cloud Composer Image
  • composer config -g repo.packagist composer https://packagist.phpcomposer.com
    Copy after login
  • Tencent Cloud Composer Image
  • composer config -g repo.packagist composer https://mirrors.cloud.tencent.com/composer/
    Copy after login
  • Huawei Cloud Composer Image
  • composer config -g repo.packagist composer https://repo.huaweicloud.com/repository/php/
    Copy after login
  • 安长Cloud Composer Image
  • composer config -g repo.packagist composer https://php.cnpkg.org/
    Copy after login

    2.4 Installation successful
    [root@VM-12-9-centos etc]# composer
     ______
    / ____/___  ____ ___  ____  ____  ________  _____
    / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
    / /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
    \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                      /_/
    Composer version 2.1.9 2021-10-05 09:47:38
    Copy after login

##(3) Composer command

##3.1 View version

composer --version# 查看版本会看到当前版本以及更新时间[root@VM-12-9-centos /]# composer --versionComposer version 2.1.9 2021-10-05 09:47:38
Copy after login

3.2 Version upgrade

# 升级将默认升到最新版本composer self-update# **友情提示:** 版本升级命令将会从官方版本去下载composer,phar镜像,速度将受限!强烈建议全局配置加速镜像
Copy after login

3.3 Execute diagnostic command

composer diagnose
Copy after login

3.4 Clear Caching

composer clear
Copy after login

3.5 Project update composer.lock

composer update --lock
Copy after login

3.6 Debugging

# **Composer调试只需要在命令前加上-vvv就可以打印出错信息** # 例如下载laravel命名为blog的项目composer -vvv create-project laravel/laravel blog
Copy after login

3.7 Installation Dependent package

# 该命令仅限在项目根目录操作
composer install
Copy after login

3.8 Update dependent package

# 该命令仅限在项目根目录操作

### 更新所有依赖包
composer update

### 更新指定依赖包
composer update monolog/monolog

### 更新多个指定依赖包
composer update monolog/monolog symfony/dependency-injection

### 根据通配符匹配更新依赖包
composer update monolog/monolog symfony/*
Copy after login

3.9 Remove dependent package

# 移除指定依赖包
composer remove monolog/monolog

# 根据通配符匹配移除依赖包
composer remove monolog/monolog symfony/*
Copy after login

3.10 Search for dependent packages

# 搜索依赖包:输出包及其描述信息
composer search monolog

# 只搜索包:--only-name
composer search --only-name monolog

# 根据通配符搜索依赖包
composer search mono*
Copy after login

3.11 View dependent packages

# **该命令仅限项目根目录使用**# 查看**当前项目**已安装的依赖包
composer show

# 根据通配符**当前项目**进行筛选
composer show laravel*# 查看**当前项目**指定依赖包
composer show laravel/tinker
Copy after login

(4) Project usage

in In projects, we often use composer to download and update packages or libraries, but we often encounter various problems during use. Next, we'll address common issues.

4.1 宝塔面板安装Composer

composer 的安装需要php开启 putenv 函数 ,否则会安装失败!由于 putenv 函数可以设置特定的环境变量而被认为是一个潜在的安全漏洞,因此php在安装之初是默认禁止它的!

  • 删除禁用函数

宝塔面板出于对于安全的考虑,自动禁用了一些函数;因为安装Composer也会用到相应的函数,我们应该先删除在安装 Composer 。          安装 Composer 需要删除 的 PHP 禁用函数:putenv()          友情提示: 在使用 Composer 的时候需要根据项目的php版本去做操作(安装多少个PHP版本就需要相对应删除),下图示范是根据 php-8.0 来操作的。

  • 非宝塔面板管理操作
# 进入php.ini配置文件编辑--->搜索(输入 / 进入搜索模式,搜索disable_functions)# ---->依次删除putenv**### 第一步:查询php.ini路径** [root@VM-12-9-centos 74]# find / -name "php.ini"/www/server/php/56/etc/php.ini
/www/server/php/80/etc/php.ini
/www/server/php/74/etc/php.ini

**### 第二步:依次编辑php.ini文件** : 删除函数putenvvim /www/server/php/56/etc/php.ini

**### 第三步:搜索“disable_functions”** # ESC + :+ / + disable_functions 将会自动定位到指定位置disable_functions = passthru,exec,system,**putenv** ,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv

**### 第四步:保存文件并退出** ESC + :wq!
Copy after login
  • 友情提醒

宝塔是支持php多版本的,如果在不同版本的php中使用 composer ,只要开启php中的 putenv 函数即可!

4.2 PHP项目使用

  • composer.lock已经存在

使用场景: 由于下载代码或者git代码或者部署项目时会存在 composer.lock , 首先应该删除composer.lock

# 项目中composer.lock已经存在,需要进行删除,再重新安装,安装成功后会重新生成composer.lock文件# 案例示范:**### 第一步:进入项目blog根目录** cd www\wwwroot\blog

**### 第二步:删除composer.lock文件** # 删除请核对文件名后谨慎操作rm -rf composer.lock

**### 第三步:重新composer安装项目所需包** composer install
Copy after login

(五)结尾

提示: 不要忘了经常执行 composer selfupdate 以保持 Composer 一直是最新版本哦!   

原文地址:https://learnku.com/articles/62076                               

The above is the detailed content of The most comprehensive and detailed Composer installation and configuration tutorial (win/linux). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template