What is composer?
Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. Composer deals with packages or libraries and manages them per-project basis by installi it in directory (e.g Vendor).
You can install it locally or globally depending upon the requirements. By default it doesn’t install anything globally.
Composer is strongly inspired by node’s npm and ruby’s bundler.
- Enables you to declare the libraries you depend on.
- Finds out which versions of which packages can and need to be installed, and installs them (meaning it downloads them into your project).
Composer requires PHP 5.3.2+ to run. A few sensitive php settings and compile flags are also required, but when using the installer you will be warned about any incompatibilities.
To install packages from sources instead of simple zip archives, you will need git, svn, fossil or hg depending on how the package is version-controlled.
Composer is multi-platform and run equally well on Windows, Linux and OSX.
Installing composer on Linux/MacOs
Most PHP developers use either their Macs or Linux-based operating systems for development, making this section relevant to the majority of readers. The process is very similar on both OS. You have to run the same commands on your system’s terminal in order to get Composer.
There are two options for Linux/Mac users when installing Composer:
Not the most common option, but you can choose to install Composer locally. This means that your operating system will not be able to run Composer from anywhere; you will have to specify the path from where it is installed. To do this, follow these steps:
- Run the following commands
The first command will grab the Composer installer as a PHAR (PHP Archive) file, while the second ensures that the installer is free from any errors or corruption.After executing these commands, you will have the latest build of Composer’s installer on your drive.
- Install composer via command
You can change the installation directory and filename through the –install-dir and –filename parameters. Here you can see that the directory has been named bin, and the executable is installed by the name composer.
Now that you’ve installed Composer, you can run it by typing the following command:
After the local installation, we recommend making Composer accessible globally. This will ensure that Composer can be executed from anywhere on your system, regardless of the path. We do this by adding Composer to your PATH variable.
To do this, you have to:
- Follow the instructions given in Local Installation.
- Run the following command afterwards.
- Now you can boot Composer independent of your current path in the terminal.
- To run composer, execute the command.