What is Composer?

Composer is a tool for dependency management for PHP that help us to manage the packages. Composer is a package manager for PHP β€” it helps you install, update, and use code written by others in your own PHP project.

πŸ› οΈ What Does Composer Do?

  1. βœ… Installs packages (libraries/tools your project needs)
  2. πŸ“¦ Tracks dependencies (it knows which version of which library your app uses)
  3. πŸ”„ Updates libraries when new versions are available
  4. πŸ“ Creates autoloaders (makes it easy to include and use classes automatically)

πŸ“˜ Simple Example

Say you’re building a PHP app and want to send emails.
Instead of writing everything from scratch, you can use a package like PHPMailer.
With Composer, you just run:

composer require phpmailer/phpmailer

βœ… Step 1: Check if Composer is Installed

πŸ” Open your terminal or command prompt and run:

composer -V

or

composer --version

βœ… If installed, you’ll see something like:

Composer version 2.7.2 2024-05-15 16:45:23

❌ If not installed, you’ll get something like:

'composer' is not recognized as an internal or external command...

πŸ’Ύ Step 2: How to Install Composer (if not installed)

1. Go to https://getcomposer.org/
2. Download the Composer-Setup.exe
3. Run it and follow the instructions (make sure PHP is installed)


Common Composer Commands

Here’s a list of useful Composer CLI commands with short descriptions:

Command Description
aboutShows a short information about Composer
archiveCreates an archive of this composer package
auditChecks for security vulnerability advisories for installed packages
browse [home]Opens the package’s repository URL or homepage in your browser
bumpIncreases the lower limit of your composer.json requirements to the currently installed versions
check-platform-reqsCheck that platform requirements are satisfied
clear-cache [clearcache|cc]Clears Composer’s internal package cache
completionDump the shell completion script
configSets config options
create-projectCreates new project from a package into given directory
depends [why]Shows which packages cause the given package to be installed
diagnoseDiagnoses the system to identify common errors
dump-autoload [dumpautoload]Dumps the autoloader
execExecutes a vendored binary/script
fundDiscover how to help fund the maintenance of your dependencies
globalAllows running commands in the global composer dir ($COMPOSER_HOME)
helpDisplay help for a command
initCreates a basic composer.json file in current directory
install [i]Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json
licensesShows information about licenses of dependencies
listList commands
outdatedShows a list of installed packages that have updates available
prohibits [why-not]Shows which packages prevent the given package from being installed
reinstallUninstalls and reinstalls the given package names
remove [rm|uninstall]Removes a package from the require or require-dev
require [r]Adds required packages to your composer.json and installs them
run-script [run]Runs the scripts defined in composer.json
searchSearches for packages
self-update [selfupdate]Updates composer.phar to the latest version
show [info]Shows information about packages
statusShows a list of locally modified packages
suggestsShows package suggestions
update [u|upgrade]Updates your dependencies to the latest version according to composer.json
validateValidates a composer.json and composer.lock