How to install composer?

What is composer?

Composer is a cross-platform dependency manager for PHP.  It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. Composer allows developers to specify project dependencies in a composer.json file and then Composer automatically handles the rest.

Suppose you are working on a project where you are suppose to work with number of different php libraries. If you are not using composer you may have to do following procedure:

  • Installing libraries manually
  • Update them manually by keeping an eye on each dependency and some of them depends on some other libraries.
  • This creates lots of trouble sometime

Composer will help you manage them easily:

  • It install them and lock their version so that you want update them by mistake.
  • You can always update them and it will also update other libraries that depends on it.
  • It finds out which versions of which packages can and need to be installed, and installs them
  • It helps you keep vendor directory out of the project using git so that you can only push necessary code in the repo and reduce the bundle size for your project

Installation

Depending on the operating system you are using it can be installed differently. In this tutorial we will address following operating systems:

  • Mac Os
  • Windows
  • Linux

Install composer on Mac OS

In order to install composer  on mac os follow the steps shown below:

Open your mac terminal window and run following commands:

# get the composer phar file
curl -sS https://getcomposer.org/installer | php

# now we move composer.phar file to a executable directory
sudo mv composer.phar /usr/local/bin/

# we need to let our computer to run composer command globally
# in order to do that we need to add entry in our bash_profile file
# open bash_profile file using nano editor
nano ~/.bash_profile

# add this line below to bash_profile and save using CMD + x + Enter
alias composer="php /usr/local/bin/composer.phar"

# once file is saved we need to run following command to activate our changes
source ~/.bash_profile

# finally, composer installed on mac os and you can verify using any of
# the following commands
composer --version
composer --V

Install composer on linux or ubuntu

Open linux terminal window and run following commands in order to install composer:

# update package manager first
sudo apt-get update

# make sure you have curl installed first
# if you do not have curl installed run following
# command to install curl on your linux operating system
sudo apt-get install curl

# install composer globally
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

# verify the composer installation
composer -V

Install composer on Windows

Download and run Composer-Setup.exe. It will install the latest Composer version and set up your PATH so that you can call composer from any directory in your command line.

How does composer work?

Once you install composer on your machine. You simple follow the steps below:

  • create a composer.json file
  • open terminal window and head towards your project root folder
  • run composer install command
  • this will generate vendor folder with all your project dependencies
  • include following line in your php script
  • require_once 'vendor/autoload.php'
  • now, your dependencies will be auto-loaded. You do not need to include them manually.

How to install/update a composer dependency?

To install a new php library or a package you need to know following commands:

# install a new library
composer require facebook/php-sdk

# update all packages except dev dependencies
composer update --no-dev

# install all packages except dev dependencies
composer install --no-dev

How to add author block in composer.json file?

To add author blocks in composer file you need to add following lines of code:

{
    "authors": [
        {
            "role": "Developer",
            "name": "Sandip Patel",
            "email": "sandip@learn2torials.com",
            "homepage": "https://www.learn2torials.com"
        },
        {
            "role": "Developer",
            "name": "Dhruti Patel",
            "email": "dhruti@learn2torials.com",
            "homepage": "https://www.learn2torials.com"
        }
    ]
}

How to add project support info in composer.json file?

To add project details or support info you need to add following block of code to your composer.json file:

{
    "support": {
        "email": "support@example.org",
        "irc": "ENTER_URL_HERE",
        "wiki": "ENTER_URL_HERE",
        "rss": "ENTER_URL_HERE",
        "chat": "ENTER_URL_HERE",
        "issues": "ENTER_URL_HERE",
        "docs": "ENTER_URL_HERE",
        "source": "ENTER_URL_HERE",
        "forum": "ENTER_URL_HERE"
    }
}

How to install a specific version of package using Composer?

Followings are some of the methods to install specific version of the package:

# Syntax - 1 
composer require vendor/package version

# Syntax - 2
composer require vendor/package:version

# Examples
composer require vendor/package:2.0
composer require vendor/package 0.1
composer require vendor/package "^0.1"
composer require "vendor/package":"^1.5.0"

How to remove unused dependencies from composer?

To remove unused dependencies from your existing project run following commands:

# Example - 1
composer remove vendor/package

# Example - 2
composer remove vendor/package --update-with-dependencies

How to add a package from a VCS repository?

To add a package from remote vcs repository or private repo you need to add following line of code block to your composer.json file:

{
    "require": {
        "user/repo": "dev-master"
    },
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/user/repo"
        }
    ]
}

How to add local package using composer?

To add local package using composer you need to include following code in your composer.json file:

{
    "require": {
        "my/package": "*"
    },
    "repositories": [
        {
            "type": "path",
            "url": "../../packages/my-package",
            "options": {
                "symlink": true // disabling mirroring
            }
        }
    ]
}

How to get list of all installed packages with version number?

To list all installed packages within a project run following commands:

# show globally installed packages
composer global show -i -t

# show packages installed in project
composer show -i -t

How to correctly deploy composer in prod environment?

Follow the steps below to correctly use composer in your production environment:

  • Install composer globally using sudo user
  • add vendor folder to your .gitignore file
  • pull your project on prod enviroment
  • run only: composer install --no-dev
  • never use sudo in front of install or update command
  • that is it.

How to

PSR4 Autoloading

PSR-4 is an accepted recommendation that outlines the standard for autoloading classes via filenames. To add psr-4 namespace in your existing project directory use following line of code in your composer.json file:

{
    "autoload": {
        "psr-4": {
            "App\\": "src/",
            "Monolog\\": ["src/", "lib/"]
        }
    }
}

Above code will make src directory to follow App as root namespace. Say you have following directory structure and replated psr4 namespace:

src
  |- Models
    |- User.php      -> namespace App/Models/User;
    |- Role.php      -> namespace App/Models/Role;

How to include global files using composer?

If you are required to run certain files in each request you can add following code to your composer.json file:

{
    "autoload": {
        "files": [
            "src/functions.php"
        ]
    }
}

I hope I covered most common scenarios that most developer come across. Thank you for reading this article.