Composer is a dependency management tool for PHP. It defines and manages project dependencies in a composer.json file, supports an autoloading feature to simplify class loading, and lets you run custom scripts. It is an essential tool for modern PHP development.
Prerequisites
You have installed PHP 5.6 or a later version. If you have not, see the Install PHP document.
The OpenSSL extension for PHP is enabled.
Install Composer
Follow these steps to install Composer for your operating system.
Windows
Download the Composer installer.
Go to the official Composer website. In the Windows Installer section, click Composer-Setup.exe to download the installer.

Run the installer and follow the on-screen instructions.
Verify the installation.
Press
Win+R, entercmd, and press Enter to open Command Prompt. In Command Prompt, entercomposer -V. If the output is similar to the following, the installation is successful.Composer version 2.7.7 2024-06-10 22:11:12 PHP version 7.4.33 Run the "diagnose" command to get more detailed diagnostics output.
Linux
Run the following command to install Composer:
Alibaba Cloud Linux/CentOS
sudo yum install -y composerUbuntu/Debian
sudo apt install -y composer
Run the
composer --versioncommand to verify the installation. If the output is similar to the following, the installation is successful.Composer version 1.10.27 2023-09-29 10:50:23
macOS
Run the following commands to install Composer:
curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer chmod +x /usr/local/bin/composerRun the
composer --versioncommand to verify the installation.
Use Composer
Install dependencies
`install` command
Create a `composer.json` file in your project folder. This file describes the project dependencies and uses the following format:
{ "require": { "alibabacloud/dysmsapi-20170525": "4.1.*" } }The preceding file specifies that Composer can download any version of `alibabacloud/dysmsapi-20170525` from 4.1.0 onwards. Run the following command to install the dependency package:
composer install`require` command
You can also use the `require` command to install a dependency without manually editing the `composer.json` file:
composer require alibabacloud/dysmsapi-20170525Composer finds a suitable version and updates the `composer.json` file with the package information. It then downloads and installs the dependencies, updates the `composer.lock` file, and generates the PHP autoload file.
Update dependencies
The `update` command updates all packages in a project or only specific packages:
# Update all dependencies
composer update
# Update a specific package
composer update alibabacloud/dysmsapi-20170525
# Update multiple specific packages
composer update alibabacloud/dysmsapi-20170525 alibabacloud/credentials
# You can also match packages using a wildcard character
composer update alibabacloud/*The package versions that can be upgraded are subject to version constraints. Packages are not upgraded beyond the version range specified by the constraints.
Remove dependencies
The `remove` command removes a package and its dependencies. If another package uses a dependency, the dependency is not removed:
composer remove monolog/monologView a list of installed packages
composer show --installedVersion constraints
Exact version
You can specify the exact version of a Composer dependency. For example, run the command composer require alibabacloud/dysmsapi-20170525 4.1.2 to install version 4.1.2.
Range
You can specify a range of package versions using comparison operators, such as >, >=, <, <=, and !=. You can also define multiple ranges. The logical AND (space or comma) and logical OR (||) operators are supported between ranges. The logical AND operator has a higher priority than the logical OR operator.
composer require alibabacloud/dysmsapi-20170525 >=4.1installs a version greater than or equal to 4.1.0.composer require alibabacloud/dysmsapi-20170525 >=4.1 || <4.2installs any version in the 4.1.x series.
Wildcard character (*)
The wildcard character (*) allows updates to minor or patch versions, but not to major versions. For example, run the command composer require alibabacloud/dysmsapi-20170525 4.1.* to install any 4.1.x version, which corresponds to the range >=4.1.0 <4.2.0.
Tilde (~)
The tilde (~) allows updates to patch and minor versions from the specified version, but not to major versions. For example, run the command composer require alibabacloud/dysmsapi-20170525 ~4.1.3 to install a version in the range >=4.1.3 and <4.2.0.
Caret (^)
The caret (^) allows all non-breaking updates, including minor and patch levels, but prohibits major version upgrades. For example, run the command composer require alibabacloud/dysmsapi-20170525 ^4.1 to install a version in the range >=4.1.0 and <5.0.0.