Composer – quick start guide on Linux and Mac

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.

Here is steps to install and getting started with composer on Linux and Mac.

  1. Get composer phar (php archive)
    $ curl -sS https://getcomposer.org/installer | php
    $ ls -l 
    total 3080
    -rwxr-xr-x  1 user1  staff  1576802 Mar  4 00:12 composer.phar
    
  2. List filenames in phar archive

    $ phar list -f composer.phar
    

    In case you want to browse files packed in conpose.phar, you can use phar extrat to extract all files in current dir. You may want to create a temporary directory before doing it.

    $ mkdir extracted; cd extracted
    $ phar list -f ../composer.phar
    $ ls -p
    LICENSE		bin/		res/		src/		vendor/
    
  3. Install composer globally (optional). You can always run composer from local copy using php composer.phar. But it is convenient to install it globally in /usr/local/bin

    ## You may need sudo if /usr/local/bin is not writable by user
    $ mv composer.phar /usr/local/bin/composer
    
  4. Assuming /usr/local/bin is in your PATH, you can run the following to print composer version.
    $ composer --version
    Composer version 1.0-dev (f2d606ae0c705907d8bfa1c6f884bced1255b827) 2016-03-03 12:44:49
    
  5. Now we’ll create a project config file containing dependencies.
    Create composer.json in desired project dir (say my_composer_project) file with the following content. Alternatively you can also use composer init to create initial composer.json.

    {
        "require": {
            "monolog/monolog": "*"
        }
    }
    
  6. Install packages using composer as per composer.json and create composer.lock. Note that re-running install will then read composer.lock and install stuff based on that. So if composer.json is modified, you need to run update.

    $ composer install
    $ ls 
    composer.json	composer.lock	vendor
    $ ls vendor/monolog/monolog/
    CHANGELOG.mdown		README.mdown		doc			src
    LICENSE			composer.json		phpunit.xml.dist	tests
    

    Note: Composer will display a Warning when executing an install command if composer.lock and composer.json are not synchronized

  7. To run composer update (in case composer.json is changed). This will update composer.lock based on composer.json.

    $ composer update
    
  8. To add required package (say mockery) to your composer.json and install it

    $ composer require mockery/mockery
    $ cat composer.json 
    {
        "require": {
            "monolog/monolog": "*",
            "mockery/mockery": "^0.9.4"
        }
    }
    
  9. List info about installed packages

    $ composer info
    monolog/monolog 1.18.0 Sends your logs to files, sockets, inboxes, databases and various web services
    psr/log         1.0.0  Common interface for logging libraries
    
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments