Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Creating Project Templates With Composer

If you've ever looked at the instructions to install symfony, you will have noticed a command like this:

composer create-project symfony/website-skeleton my-project

Haven't you ever wanted to be able to do this with your own templates for projects? It's actually really easy, and this tutorial will show you how.

There's also a major benefit compared to performing a git clone operation. Your template's .git directory will not be pulled down, so you don't have to remember to manually delete that directory before running git init etc.

You can do this for any type of project, not just PHP based ones. E.g. set this up for your pure JS based webapps etc.

Steps

Create Repository

The first thing you need to do is create a repository that will act as your template. This will need to be accessible by Packagist. I used Github for my Web App Template.

Create The Composer.json File

At the top level, create a composer.json file which will specify the license information etc. Below is an example:

{
    "name": "organization/name-of-template",
    "type": "template",
    "description": "A project template to make creating new (type goes here) projects easier.",
    "keywords": ["template"],
    "homepage": "https://url-of.project.repository",
    "license": "MIT",
    "authors": [
        {
            "name": "Joe Bloggs",
            "email": "joe.bloggs@gmail.com",
            "homepage": "https://blog.joe-bloggs.com",
            "role": "Developer"
        }
    ]
}

Tag Your Project

When you are happy with your template, tag it a version number (preferably 1.0.0 or higher) and push the tag. Without this, you will get an error message later about not finding a stable version when running the composer create-project command.

Packagist - Submit

After having pushed the composer.json file to your repository, go to packagist.org's submit page and enter the URL of your template's repository.

I'm assuming you have an account and are logged in, if not then create your account or log in.

Packagist will show you a message stating the name that will appear for the project (which is based on the name you put in the composer.json file). This will be the name that users will have to use in the composer create-project command. Check you are happy with the name and submit it.

Test

You should now be able to install a fresh project from your template using the composer command. E.g.

composer create-project organization/name-of-template name-for-new-project

Webhook

By default, packagist does not automatically update when your repository does. Don't forget to set up a webhook to do this for you.

Last updated: 28th February 2022
First published: 30th December 2018