Composer is a dependency manager tool for PHP. Using the composer we can manage the package dependency and version of the packages. Composer is different from npm (Node package manager) and apt-get because it only contain the dependency of project.
Composer creates a compose.json file to contain the information about the project and dependencies like packages, module etc. when we install the dependencies using the composer it creates a folder named as vendor and install dependencies there.
Let’s install the composer:
System Requirements
Composer requires PHP 5.3.2+ to run. Some additional seating are also required but composer will notify you when you use it.
Installation of composer in ubuntu and macOS
Before start make sure you have enabled the PHP CLI, so check php versions first in command line.
php -v
it should output like below:
if it’s now showing as above then you need to install the php first, you can read How to Install php, apache, mysql(Lamp) and phpmysadmin on Ubuntu.
Now, There is two way to install the composer
- Locally
- Globally
1. Installing Composer Locally in project
Go to the project location and open the terminal or First open the terminal and then go to path of project.
Type below command
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
above command will download composer-setup.php in current directory
php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Above Command will check the installer is not corrupted and verified.
php composer-setup.php
This will check the php.ini settings and warn you if its not correct. It will run above script to install the composer in directory.
php -r "unlink('composer-setup.php');"
delete the composer-setup file.
2. Installing Composer Globally
We already downloaded the composer.phar file in directory. If we want to use it globally then we need to move it into /usr/local/bin/ so it will be accessible globally.
sudo mv composer.phar /usr/local/bin/composer
There is few arguments which we can use
–install-dir
To install the composer in sepecific directory
Example:
php composer-setup.php --install-dir=bin
–filename
You can specify the filename (default: composer.phar) using the –filename option. Example:
php composer-setup.php --filename=composer
–version
Change the version of composer.
Example:
php composer-setup.php --version=1.0.0-alpha8
Install composer in windows
As we installed the composer using the command line in the same way we can install in windows but in windows we have more simple way to install it using window executable installer.
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.