For any one who does web development with PHP, the composer will be a very useful dependency manager. In this article we will see what is composer and how to install it on Windows and Linux machines.
Composer is a dependency manager not a package manager. Composer will install the required dependencies to Vendor directory of your project on which you are working.
Install composer on Windows10 using windows installer
Composer can be easily installed on windows system just by downloading and installing a single installer (.exe) file Composer-Setup.exe. This will install the composer and also add this to system PATH. Now we can use it just by accessing with command 'composer' from any path of the system as below.
C:\User\Home>composer {-options}
Install composer on Ubuntu 20.04 using Command Line
Installion of composer to current directory (project alone)Composer can be installed through command line also. This will install composer in the current directory.
$ sudo apt update
$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php -r "if (hash_file('sha384', 'composer-setup.php') === 'e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
$ php composer-setup.php
$ php -r "unlink('composer-setup.php');"
Read this: Install PHP on Windows10, Ubuntu machines.
sudo apt update will update the source repositories. Now we will see what the following lines of commands actually do. First line will download the composer-setup.php file from https://getcomposer.org website to current directory. Second line will verify the downloaded installer file using SHA-384 and gives the "installer verified" message. After verification the third step will run the installer, here composer-setup.php will check for proper php.in settings and then downloads latest composer.phar (.phar means PHP archive). In last step the installer composer-setup.php will be removed. This way will install composer to a particular directory/path only and it cannot be accessed globally.
There are ways to install composer to our required location, with our required filename and required version.
- Installation to required location
php composer-setup.php --install-dir=bin
php composer-setup.php --filename=composer
php composer-setup.php --version=1.10.5
Now we can run composer using the following command at command prompt.
$ php composer.phar {-options}
Installion of composer globally
To install composer with globally accessible on Ubuntu 20.04 you have to install it to /usr/local/bin location.
$ sudo apt update
$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php -r "if (hash_file('sha384', 'composer-setup.php') === 'e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
$ sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
$ php -r "unlink('composer-setup.php');"
Now we can run composer using the following command at command prompt. Please note that "php" interpreter is not required before "composer".
$ composer {-options}
Once it is installed you can see the message "Composer (version x.xx) is successfully installed" at the command prompt. You can check it's installation by verifying its version using the following command.
$ composer -v
Please find below the screenshot of the above command output.

Hope you to got this installed correctly by now. Happy coding.
When ever you install a composer dependency the dependency details will be updated to composer.json file present in that particular project folder and their versions are maintained by creating a composer.lock file. This is very important for version control of any application where many developers involve in a project. When ever this project is made live on a system all the dependencies present in composer.json will be automatically installed with versions present on composer.lock file.