Run Composer on Git Clone

I work on a couple of projects that use Composer to manage the dependencies. And a coworker asked if it was possible to automate running composer install after cloning a repo. It sounded like a good idea. As i sometimes forget to run the command myself. So i did some research. And it turned out to be quite easy.

To makee this work we will be using Git hooks together with Git templates.

So we start by creating a templates folder for git. Where does not matter.

$ mkdir -p [full path]/git-templates/hooks

When that’s done. we need to tell git to use the template folder on each clone / checkout. We can do this by setting a global alias for clone

$ git config --global alias.clone 'clone --template=[full path]/git-templates'

You can also do this by manually editing the .gitconfig file

clone = clone –template=[full path]/git-templates

So now we have a template folder that’s read on every clone / checkout. We only need to create the hook script that triggers composer.

$ vi [full path]/hooks/post-checkout
#! /bin/bash
# Check if a composer.json file is present
function composer_run() {
    REPO=$(readlink -m $(dirname .))
    if [ -f $REPO/composer.json ]; then
        composer install


Finally wer need to make it executable

$ chmod +x [full path]/hooks/post-checkout

Let’s test it by cloning a test repo

$ git clone foo

Cloning into foo…
remote: Counting objects: 21, done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 21 (delta 5), reused 14 (delta 1)
Receiving objects: 100% (21/21), done.
Resolving deltas: 100% (5/5), done.
Loading composer repositories with package information
Installing dependencies (including require-dev)
- Installing psr/log (1.0.0)
Loading from cache

- Installing monolog/monolog (1.12.0)
Loading from cache

comments powered by Disqus