InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search
  • Home
  • > Tutorials
  • > PHP cookbook

Composer – quick start guide on Linux and Mac

on Mar 4, 2016

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
    

Suggested posts:

  1. HTML import
  2. Git – how to undo last commit
  3. jQuery – find total number of DOM elements
  4. LXC (Linux Containers) – quick start tutorial on Ubuntu
  5. How to use CasperJS to automate website testing
  6. Android chrome – create website bookmark on home screen
  7. Centos – save and restore iptables
  8. Embed youtube video with javascript on-click lazy loading approach
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Linux, Linux/Unix Command Line, Mac, PHP cookbook, Tutorials, Ubuntu Linux
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress