What is composer?

Composer is version management for php libraries.

How to install composer?

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

How to use composer?

Run this command to see help screen:

composer

Composer works with 2 files and 1 directory:

  • composer.json - list of your project dependencies
  • composer.lock - cached composer.json, points to exact commits of the used libraries
  • vendor - folder with all libraries

Start working on the project

You need to install modules that are defined in project? Install them!

composer install

Adding and updating modules

You want to update module version?

composer require phpunit/phpunit 3.7.*

This command will change 2 files: composer.json and composer.lock. And add new files to vendor directory.

Development and production

  • It is not recommended to run composer commands on production system.
  • It is recommended to save composer.lock in your repository to use the same version on every environment.
  • You can ignore vendor folder in your git repo.

Versions

You can use this ways to specify versions:

  • dev-master - latest commit in branch master
  • * - latest commit in main branch (in most cases it is master branch)
  • 3.7.* - any version starts with 3.7
  • ~3.7 - the same as 3.7.* [RECOMMENDED FOR USE]
  • 3.7.1 - exact version

Composer is using Semantic Versioning - major.minor.patches The idea is that patches should never broke functionality and only major updates can broke backward compability.

Example

Here is a real example how to install magento via composer

Reading list

Feel informed? I hope so.

Like this article? Share it on Facebook, Twitter, or Linkedin, send a smoke signal or even try some morse code.

I will be glad to answer your questions or get some feedback from you here in comments below.