All Products
Search
Document Center

Alibaba Cloud SDK:Install and use Composer

Last Updated:Aug 19, 2025

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.

    Check if OpenSSL is enabled

    Run the following command. If the output contains openssl, the extension is enabled.

    • Windows

      php -m | find "openssl"
    • Linux

      php -m | grep openssl

    If the openssl extension is not enabled, you can enable it by performing the following steps:

    1. Find the PHP configuration file, php.ini. This file is usually located in the PHP installation folder.

    2. Open the php.ini file and search for the extension line that contains openssl, such as extension=openssl, extension=php_openssl.dll, or extension=openssl.so.

    3. If the line is preceded by a semicolon (;), delete the semicolon to uncomment it.

    4. Save the file and check again to verify that the change has taken effect.

Install Composer

Follow these steps to install Composer for your operating system.

Windows

  1. Download the Composer installer.

    Go to the official Composer website. In the Windows Installer section, click Composer-Setup.exe to download the installer.

    image

  2. Run the installer and follow the on-screen instructions.

  3. Verify the installation.

    Press Win+R, enter cmd, and press Enter to open Command Prompt. In Command Prompt, enter composer -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

  1. Run the following command to install Composer:

    • Alibaba Cloud Linux/CentOS

      sudo yum install -y composer
    • Ubuntu/Debian

      sudo apt install -y composer
  2. Run the composer --version command 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

  1. 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/composer
  2. Run the composer --version command 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-20170525
    

    Composer 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/*
Note

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/monolog

View a list of installed packages

composer show --installed

Version 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.1 installs a version greater than or equal to 4.1.0.

  • composer require alibabacloud/dysmsapi-20170525 >=4.1 || <4.2 installs 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.