To add a package using composer require, run the command in your project root to install a PHP package and update configuration files. 1. Execute composer require vendor/package-name, such as composer require guzzlehttp/guzzle, to download the package, update composer.json and composer.lock, and install files into vendor/. 2. Optionally specify a version like composer require monolog/monolog:^2.0, using constraints such as ^ for compatible updates or ~ for patch-level changes; otherwise, Composer selects the latest stable version matching your environment. 3. Use the --dev flag, like composer require phpunit/phpunit --dev, to add packages only for development, which are placed in require-dev. 4. Behind the scenes, Composer checks availability, resolves dependencies, updates composer.json, writes exact versions to composer.lock, and downloads files to vendor/. Always commit both composer.json and composer.lock to version control, and use composer install when setting up a cloned project to ensure consistent dependency installation based on the lock file.
To add a package using composer require
, you're telling Composer to download and install a specific PHP package into your project and automatically update your composer.json
and composer.lock
files.

Here’s how to do it:
1. Run composer require
with vendor/package name
Open your terminal, navigate to your project’s root directory (where your composer.json
file is), and run:

composer require vendor/package-name
For example, to install the popular Guzzle HTTP client:
composer require guzzlehttp/guzzle
This will:

- Download the package and its dependencies.
- Add the package to the
require
section of yourcomposer.json
. - Update the
composer.lock
file. - Install the files into the
vendor/
directory.
2. Specify a version (optional)
You can also require a specific version, range, or stability:
composer require monolog/monolog:~1.0
Or:
composer require monolog/monolog:^2.0
Common version constraints:
^2.0
→ allows updates that don’t break compatibility (e.g., 2.0.0 to 2.9.9)~1.5
→ allows patch-level changes (e.g., 1.5.0 to 1.5.9)
If you don’t specify a version, Composer will choose the latest stable version that matches your project’s PHP version and other dependencies.
3. Require dev dependencies
To add a package only for development (like PHPUnit or PHPStan), use the --dev
flag:
composer require phpunit/phpunit --dev
This adds the package to the require-dev
section instead of require
.
4. What happens behind the scenes?
When you run composer require
:
- Composer checks package availability and version constraints.
- It resolves dependencies to avoid conflicts.
- It updates
composer.json
automatically. - It writes a snapshot of the exact versions installed to
composer.lock
. - It downloads packages into
vendor/
.
Bonus Tips
- Always commit
composer.json
andcomposer.lock
to version control (unless it's a package meant to be reused). - After cloning a project, use
composer install
(notrequire
) to install dependencies from the lock file.
That’s it — composer require
is the fastest way to add a new package to your PHP project. Just remember the vendor and package name from Packagist.org, and you're good to go.
The above is the detailed content of How to add a package with composer require?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

When using Composer in a production environment, you need to pay attention to safety, stability and performance. 1. Use composerinstall-no-dev to reduce unnecessary development dependencies and reduce online environment risks; 2. Always submit and rely on composer.lock files to ensure version consistency, and avoid using updates during deployment; 3. Optional configuration platform-check=false ignores platform differences warnings, which is suitable for building packaging scenarios; 4. Enable APCU to accelerate automatic loading to improve performance, especially suitable for high concurrency services, while paying attention to namespace uniqueness to avoid cache conflicts.

Installing Composer takes only a few steps and is suitable for Windows, macOS, and Linux. Windows users should download Composer-Setup.exe and run it to ensure that PHP is installed or XAMPP is used; macOS users need to execute download, verification, and global installation commands through the terminal; Linux users operate similarly to macOS, and then use the corresponding package manager to install PHP and download and move the Composer file to the global directory.

To check whether Composer is installed correctly, first run the composer--version command to view the version information. If the version number is displayed, it means that it is installed. Secondly, use the composerdiagnose command to detect configuration problems and ensure that the environment variables and permissions are normal. Finally, try to verify the functional integrity through the composerrequiremonolog/monolog installation package. If the vendor directory is successfully created and the dependency is downloaded, it means that Composer is fully available. If the above steps fail, you may need to check whether PHP has been installed globally or adjusted system path settings.

Tomaintainapplicationsecurity,keepdependenciesupdatedusingautomatedtoolslikeDependabot,Renovate,orSnyktotrackandapplyupdates.1)UsethesetoolstoautomatedependencychecksandintegratewithCI/CDpipelinesforreal-timealertsandmergerestrictions.2)Regularlyscan

To install the Composer plug-in, please first confirm that Composer is installed and the composer.json file exists, and then follow the following steps: 1. Make sure that Composer has been installed and created composer.json; 2. Search and copy the required plug-in name on Packagist; 3. Use the composerrequirequire command to install the plug-in, such as composerrequiredealerdirect/phpcodesniffer-composer-installer; 4. Verify whether the plug-in is effective and check compatibility and configuration. Follow these steps to correctly install the Composer plug-in.

Stabilityflagslike@dev,@alpha,@beta,and@stableindicatesoftwarematurity.1)@devmeansearlydevelopmentwithpossiblebreakingchanges.2)@alphaisanunstableearlytestingphase.3)@betaindicatesimprovedfunctionalitybutwithpotentialbugs.4)@stablemeansproduction-rea

You can make Composer use a specific PHP version by specifying PHP binary files, using version management tools, or configuring composer.json. 1. In Unix-like systems, you can set an alias or export the COMPOSER_PHP environment variable to directly specify the PHP binary file; 2. Use tools such as phpenv or brew to switch the PHP version to achieve global or project-level version control; 3. Configure the platform.php field in composer.json to declare the PHP version required for the project to ensure that the dependency check is correct; 4. Windows users can call the specified PHP version by modifying the PATH environment variable or creating a batch script to call the specified PHP version.

To add a custom repository to the Composer configuration, edit the composer.json file in the project and specify the repository information under the "repositories" key. The specific steps are as follows: 1. Determine the repository type, such as VCS (Git, SVN, etc.), Composer, PEAR or Package; 2. Add the "repositories" block in composer.json and fill in the repository type and URL. For example, when using a VCS-type Git repository, the format is {"type":"vcs","url":"https
