Composer Basics

PHP is a pretty cool language, yeah. It’s super easy to create dynamic websites with a very small learning curve. Well, it’s easy until your site gets big. Or you need to move to a new computer. What if you lose a file and your entire app crashes? What if dependencies you use clash with each other and cause unforeseen errors well into production? How can we combat this? Enter Composer.


Composer is the remedy for PHP’s nightmarish dependency management situation. Composer is a command-line tool that works similar to NPM. You can easily add or remove dependencies. Plus, if you host your code in version control, you can upload your code without having to include the dependencies themselves. Others can just composer install and have a working copy in minutes.


###Mac/Linux: With CURL: > curl -sS | php

Otherwise: > php -r “readfile(‘’);” | php

To move Composer into your /bin directory and install it globally: > mv composer.phar /usr/local/bin/composer


Nobody really likes us on Windows. We’re weird, and installing programs is unnecessarily complicated. Regardless, click this link to download the newest official Composer Windows installer. This basically just installs Composer to your AppData directory and lets you put the tool in your PATH.


Now, we can easily use composer just by running this command in the shell: > composer

If you installed it locally only: > php composer.phar

You will mostly use 4 composer commands: install, update, require, remove and self-update. Let’s talk about each one a bit more in-depth.


composer install

This command is almost identical to npm install. This command assumes there is a composer.json file present in the working directory (more on composer.json later). Composer will enumerate through the dependencies listed in composer.json and install them all to the /vendor directory. Composer is awesome in that it resolves all conflicts and installs the most suitable version of every dependency you need.

After this, all you need to do to have access to every single dependency in your app is include this code at the beginning of your main source file:


// Use an autoload.php path relative to your file.
// Here, I assumed that your *.php file is located in a directory that
// contains a subdirectory named vendor/.


// Your code here...


composer update

This command does exactly what you would imagine - it updates your project to work with new dependencies, and also updates all dependencies to the highest version available that will not create a conflict with any other dependency. This also assumes you have a composer.json file.


composer require (vendor)/(package)

This command requires the given package by adding it to composer.json. It does not require a composer.json file. One will be made if not present.


composer remove (vendor)/(package)

This command removes the given package from composer.json and uninstalls it from vendor/.


composer self-update

This command simply updates Composer itself.

###composer init and composer.json

composer init

This command begins a series of prompts that ask you questions about your project and generates a composer.json file in the working directory.


Similar to a package.json file, composer.json describes your project and lists its dependencies. Full documentation can be found here. Read it. Now.

##Conclusion The composer documentation can be found here. Thanks for reading this article. Good luck with your PHP development!