Managing PHP Dependencies with Composer
Composer is a multi-platform dependency management tool for PHP. It manages your project's dependencies and allows you to easily install and update them from Packagist, the main Composer repository where public PHP packages are aggregated in one location.
Composer logo
Composer will install the dependencies of your PHP project in the vendor
directory, allowing you to use them in your application.
Install Composer
You will need PHP already installed on your system. If you do not have PHP downloaded, you can download it from PHP.net.
Windows
To install Composer on windows, use their Windows installer. Simply run the Composer-Setup.exe
and it will install Composer for you.
macOS
You can install Composer using the Homebrew package manager.
BASHbrew install composer
Then to enable Composer globally, run:
BASHmv composer.phar /usr/local/bin/composer
Linux
You can install Composer in the command line by running:
BASHcurl -sS https://getcomposer.org/installer -o composer-setup.php
Then to enable Composer globally, run:
BASHmv composer.phar /usr/local/bin/composer
Initialize Composer
Before you can use Composer, ensure you installed it correctly. You can do this by checking the Composer version:
BASHcomposer --version
If that returns a number, you have successfully installed Composer.
To get started, initialize your project at the root directory:
BASHcomposer init
After the prompts, you will have a composer.json
file in your project root directory. This file contains the information about your project, including the project's dependencies and their versions.
Now that you have a composer.json
file, you can install your dependencies.
BASHcomposer install
This will download the dependencies from the Packagist repository and install them in your vendor
directory. This will also create a composer.lock
file. This file keeps track of the exact versions of the dependencies you have installed.
Inside your vendor
directory, you will find the dependencies you have installed and a file called autoload.php
, which is the auto-loader for your project.
To use the dependencies you have installed, you will need to include the autoload.php
file in your project.
PHPrequire_once 'vendor/autoload.php';
Your dependencies are now available in your project.
Adding a Dependency
You can add a dependency to your composer.json
file by adding a new "require"
key to the file or using the composer require
command.
BASHcomposer require vendor/package:version
For example:
BASHcomposer require symfony/console:6.0.0
This will add the symfony/console
package to your composer.json
file.
JSON{
"require": {
"symfony/console": "6.0.0"
}
}
Updating Dependency Versions
Composer can search for the latest version of your dependencies and update them if they are out of date. To do so, run:
BASHcomposer update
If a dependency has been updated, the new version will be shown in the terminal and the composer.lock
file will be updated accordingly.
You can also update a specific dependency by specifying the dependency's name and version:
BASHcomposer update symfony/console:6.0.0
This will only update the symfony/console
package to version 6.0.0 and leave the other dependencies untouched.
Removing Dependencies
With Composer, you can remove dependencies from your project that you no longer need or want.
BASHcomposer remove vendor/package
This will remove the vendor/package
package from your composer.json
file and the composer.lock
file.
Composer.lock File
The composer.lock
file is useful for specifying the exact versions of the dependencies you have installed. When another developer clones your project, they can use the composer.lock
file to know exactly what versions of the dependencies they should install. This is why any time you run a command that modifies your dependencies, Composer will automatically update the composer.lock
file.
Conclusion
Composer is a great tool for managing your PHP project's dependencies. It allows you to install and update your dependencies from a central repository, and it will keep track of the versions of the dependencies you have installed. It also allows you to remove dependencies that you no longer need.
Hopefully, you will find Composer useful for your PHP project. Thanks for reading!
Resources
- Getting Started with TypeScript
- Getting Started with Express
- Git Tutorial: Learn how to use Version Control
- How to Set Up Cron Jobs in Linux
- How to deploy a PHP app using Docker
- How to deploy a Deno app using Docker
- Getting Started with Deno
- How to deploy a MySQL Server using Docker
- Creating a Twitter bot with Node.js
- Using Push.js to Display Web Browser Notifications
- Building a Real-Time Note-Taking App with Vue and Firebase
- Setting Up Stylus CSS Preprocessor