Composer is a cross-platform dependency manager for PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. Composer allows developers to specify project dependencies in a composer.json file and then Composer automatically handles the rest.
Installation
Depending on the operating system you are using it can be installed differently. In this post i will address following operating systems:
- Mac OS
- Window OS
- Linux OS
Install composer on Mac
Open your mac terminal window and run following commands:
$ curl -sS https://getcomposer.org/installer | php
now we move composer.phar file to a executable directory
$ sudo mv composer.phar /usr/local/bin/
open bash_profile file using nano editor
$ nano ~/.bash_profile
add this line below to bash_profile and save using CMD + x + Enter
alias composer="php /usr/local/bin/composer.phar"
once file is saved we need to run following command to activate our changes
$ source ~/.bash_profile
Now, composer installed on mac os and you can verify using
$ composer --V
Install composer on linux or ubuntu
Open linux terminal window and run following commands
$ sudo apt-get update
make sure you have curl installed first , if you do not have curl installed run below command to install curl on your linux operating system
$ sudo apt-get install curl
Now install composer
$ curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
verify the composer installation
$ composer -V
Install composer on Windows
Download and run Composer-Setup.Exe. It will install the latest Composer version and set up your PATH so that you can call composer from any directory in your command line.
How does composer work?
Once you install composer on your machine. You can use the below steps
- create a composer.json file
- open terminal window and head towards your project root folder
- run composer install command
- this will generate vendor folder with all your project dependencies
- include following line in your php script
- require_once ‘vendor/autoload.php’
- now, your dependencies will be auto-loaded. You do not need to include them manually.
How to install/update a composer dependency?
$ composer require facebook/php-sdk