linux environment
download
curl -sS https://getcomposer.org/installer | php
Set environment variables after downloading
mv composer.phar /usr/local/bin/composer
Modify permissions
mv composer.phar /usr/local/bin/composer
view version
composer -v
Windows environment download and install
Download and install
Please make sure that PHP is installed correctly before installation. Open the command line window and execute php -v to check whether the version number is output correctly. PHP version 7.3.4 or above is recommended.
php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
Friends who find it troublesome can also directly click the installation package below to download and install, the effect is the same
view version
Run the following command in the command line window, if the version number is displayed normally, the installation is successful
composer --version
Modify Composer image source
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
Single project configuration
Open a command line window (windows users) or a console (Linux, Mac users), enter the root directory of your project (that is, the directory where the composer.json file is located), and execute the following command:
composer config repo.packagist composer https://packagist.phpcomposer.com
The above command will automatically add the mirror configuration information at the end of the composer.json file in the current project (you can also add it manually):
"repositories": {
"packagist": {
"type": "composer",
"url": "https://packagist.phpcomposer.com"
}
}
Taking the composer.json configuration file of the laravel project as an example, after executing the above command, it will look like this (note the last few lines):
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.2.*"
},
"config": {
"preferred-install": "dist"
},
"repositories": {
"packagist": {
"type": "composer",
"url": "https://packagist.phpcomposer.com"
}
}
}
Cancel Composer image source modification
composer config -g --unset repos.packagist
Composer common instructions
Upgrade the Composer version (you may need to restore the mirror source to a foreign mirror source first)
composer self-update
View global configuration
composer config -gl
install command
composer install
-
composer install reads third-party components and their versions from composer.lock, and installs them into the vendor directory.
If the composer.lock file does not exist, read the composer.json third-party component and its version, and then install it in the vendor directory. -
Update is to directly obtain third-party components and their versions from composer.json, and then update the composer.lock file.
If there is a version update for the dependent third-party components, update will install the latest version to the vendor directory and update composer.lock. -
In the production environment, it is risky to use the composer update command directly, because there may be a version of the third-party component library used in the testing phase, and the composer update command was executed during the online packaging, resulting in untested third-party component updates The issue of direct release arises.
require command
In addition to using the install command, we can also use the require command to quickly install a dependency without manually adding dependency information in composer.json:
composer require monolog/monolog
Composer will first find the appropriate version, then update the composer.json file, add the relevant information of the monolog/monolog package in require, then download the relevant dependencies for installation, and finally update the composer.lock file and generate an automatic loading file for php .
update command
The update command is used to update all packages in the project, or some specified packages:
# 更新所有依赖
$ composer update
# 更新指定的包
$ composer update monolog/monolog
# 更新指定的多个包
$ composer update monolog/monolog symfony/dependency-injection
# 还可以通过通配符匹配包
$ composer update monolog/monolog symfony/*
It should be noted that the version that the package can be upgraded will be constrained by the version constraint, and the package will not be upgraded to the range of the version beyond the constraint. For example if the version constraint in composer.json is ^1.10 and the latest version is 2.0. Then the update command cannot upgrade the package to version 2.0, but can only upgrade to version 1.x at most.
remove command
The remove command is used to remove a package and its dependencies (if the dependencies are not used by other packages), if the dependencies are used by other packages, they cannot be removed:
$ composer remove monolog/monolog
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 0 installs, 0 updates, 2 removals
- Removing psr/log (1.0.2)
- Removing monolog/monolog (1.23.0)
Generating autoload files
search command
The search command can search for packages:
composer search monolog
This command will output the package and its description information. If you only want to output the package name, you can use the --only-name parameter:
composer search --only-name monolog
show command
The show command can list information about the packages used by the current project:
# 列出所有已经安装的包
$ composer show
# 可以通过通配符进行筛选
$ composer show monolog/*
# 显示具体某个包的信息
$ composer show monolog/monolog