Ultimate PHP Composer Package Manager Tutorial
What is PHP Composer?
The PHP Composer is an application level dependency manager tool in PHP programming language. For any PHP or Laravel project, you need to download and install a few third-party packages/dependencies.
PHP Composer is also known as the package manager tool which easily manages software packages used in your application.
And, Laravel framework uses composer to manage all its dependencies. So, make sure that the composer is installed before you start installing Laravel framework on your machine.
Who developed PHP Composer?
The PHP Composer tool was developed and maintained by Nils Adermann and Jordi Boggiano. The development was started around April 2011 and first released in March 2012.
If you are a beginner to Laravel MVC, then we recommend you to read the following topics:
- Laravel Framework Overview
- Laravel Request Lifecycle
- Top 20 Laravel Artisan Commands
- Understanding Laravel Middleware
- Laravel Routing Tutorial
- Understanding Laravel Blade Template
- Laravel Interview Questions
- Laravel Authentication
- Laravel Validation Tutorial
How PHP Composer Works Under the Hood?
- The PHP composer tool provides the standardized format of managing dependencies in PHP and Laravel projects.
- It runs from the command-line interface and downloads and installs the project dependencies as specified in the configuration file.
- Composer downloads the dependencies or packages from the common packages repository Packagist.
- The main advantage is composer automatically handles the dependency resolution.
- It means that if you install Laravel using composer, then composer will automatically download and install the required dependencies.
Install PHP Composer on Windows
Okay. So far, we have grasped the basic concept behind the PHP composer tool.
Now, Let’s see how to install composer on Windows?
Before installing the composer, you check if it is already installed on your machine?
Open the Windows Command prompt and run the following command
D:\My Work\Laravel Books>composer
If the composer is already installed it will show the composer version information. If not, then install composer using following commands:
Step1 – Go to https://getcomposer.org/download/ page.
Step2 – Download Composer Installer file
Composer download Page – https://getcomposer.org/Composer-Setup.exe
Step3 – Run the Composer Installer (Composer-Setup.exe)
Now, follow the Installer screen steps.
Step4 – Verify Composer Installation and Check Version.
D:\My Work\Laravel Books>composer –version
Output – Composer version 1.8.4 2019-02-11 10:52:10
Note – Make sure to append composer’s system-wide vendor-bin directory into your system’s $PATH environment variable so the laravel executable can be located by your system.
Install Laravel Using Composer
As we have learned that Laravel uses Composer to manage its dependencies. Instead, We can install Laravel framework using the PHP Composer tool.
composer create-project –prefer-dist laravel/laravel myLaravelProject
Now, comes the interesting part composer.JSON file.
Understanding Composer JSON file – composer.json
The composer.json is the dependency configuration file for the composer. It is the lookup list for the composer to download and install the required packages.
So, when you run the composer, it will install only those required dependencies which are specified in this file. Its the heart of composer tool and used as a core component. This file describes the dependencies of your project and may contain other metadata as well.
If you have never executed this command before then there is also no composer.lock and composer.json files present.
Sample composer.php file
“description”: “The Laravel Framework.”,
“keywords”: [“framework”, “laravel”],
“dompdf/dompdf”: “0.7.0 as 0.6.2”,
You can also refer official deteiled documentation at https://getcomposer.org/doc/01-basic-usage.md
PHP Composer Commands List
Composer offers a great set of commands to be used for various purposes while managing package dependencies.
Composer usage syntax:
command [options] [arguments]
-h, –help Display this help message
-q, –quiet Do not output any message
-V, –version Display this application version
–ansi Force ANSI output
–no-ansi Disable ANSI output
-n, –no-interaction Do not ask any interactive question
–profile Display timing and memory usage information
–no-plugins Whether to disable plugins.
-d, –working-dir=WORKING-DIR If specified, use the given directory as the working directory.
-v|vv|vvv, –verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Below is the deep list of most common PHP composer commands:
1. composer require:
This command adds the library in parameter to the file composer.json, and install it.
2. composer install:
Installs all libraries from composer.json. It’s the command to use to download all PHP repository dependencies.
3. composer update:
Update all libraries from composer.json, according to the allowed versions mentioned into it.
4. composer remove: uninstall a library and remove it from composer.json.
5. composer list or composer – display the list of available composer commands
6. composer init – This command creates the composer.json file in the current directory.
7. composer <command> –help
The –help combined with any composer command is used to get help on that command.
8. composer about – Shows the short information about Composer.
9. composer archive – Creates an archive of this composer package.
10. composer browse – Opens the package’s repository URL or homepage in your browser.
11. composer check-platform-reqs – Check that platform requirements are satisfied.
12. composer clear-cache – Clears composer’s internal package cache.
13. composer clearcache – Clears composer’s internal package cache.
14. composer config – Sets config options.
15. composer create-project – Creates a new project from a package into the given directory.
16. composer depends – Shows which packages cause the given package to be installed.
17. composer diagnose – Diagnoses the system to identify common errors.
18. composer dump-autoload – Dumps the autoloader.
19. composer dump-autoload – Dumps the autoloader.
20. composer exec – Executes a vendor binary/script.
21. composer global – Allows running commands in the global composer dir ($COMPOSER_HOME).
22. composer help – Displays help for a command
23. composer home – Opens the package’s repository URL or homepage in your browser.
24. composer i – Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
25. composer info – Shows information about packages.
26. composer init – Creates a basic composer.json file in the current directory.
27. composer install – Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
28. composer licenses – Shows information about licenses of dependencies.
29. composer list – Lists commands
30. composer outdated – Shows a list of installed packages that have updates available, including their latest version.
31. composer prohibits – Shows which packages prevent the given package from being installed.
32. composer remove -Removes a package from the require or require-dev.
33. composer search – Searches for packages.
34. composer self-update – Updates composer.phar to the latest version.
35. composer validate – Validates a composer.json and composer.lock.
For complete list o commands with examples – https://getcomposer.org/doc/03-cli.md
Platforms Supported by PHP Composer
Currently, PHP Composer supports a set of packages where the composer can be used:
- Symfony version 2 and later
- Laravel version 4 and later
- Drupal version 8 and later
- Silex (web framework)
- Magento version 2.0 later
- CodeIgniter version 3.0 and later
- CakePHP version 3.0 and later
- FuelPHP version 2.0 and later
- TYPO3 version 6.2 and later
- SilverStripe version 3.0 later
- Yii version 1.1 and later
- Zend Framework version 1 and later
What is Packagist?
Packagist is the main Composer repository for PHP packages. It aggregates public PHP packages into an installable with Composer.
Great! Today, you have learned all about the composer and its important commands. Laravel Composer helps developers in managing third-party packages and project dependencies. I am sure now PHP composer will become your best friend to avoid any overhead of manually downloading and installing the packages and its further dependencies.
Have any question? Please do ask, we will try to answer best.