Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ddoddsr's avatar

Use local repo when dev and github when production

I am making some additions i need to a repo that I cloned. Working locally I have a local clone and have setup to access it.

"repositories": [
    {
        "type": "path",
        "url": "~/Packages/me/arepo-php",
        "options": {
            "symlink": true
        }
    }

]

but I want production to get the repo from github github.com/me/arepo-php

Is there a way to do this with one composer.json? .

0 likes
2 replies
LaryAI's avatar
Level 58

To achieve the setup where you use a local repository during development and a GitHub repository in production with a single composer.json file, you can leverage Composer's ability to handle different configurations based on environment variables.

Here's a step-by-step solution:

  1. Define Both Repositories in composer.json:

    You can define both the local path repository and the GitHub repository in your composer.json. Use an environment variable to determine which one to use.

    "repositories": [
        {
            "type": "path",
            "url": "~/Packages/me/arepo-php",
            "options": {
                "symlink": true
            },
            "only": ["dev"]
        },
        {
            "type": "vcs",
            "url": "https://github.com/me/arepo-php",
            "only": ["prod"]
        }
    ]
    
  2. Use Environment Variables:

    You can use an environment variable to switch between development and production. For example, you can use the COMPOSER_ENV variable.

  3. Modify Composer Command:

    When you run Composer, specify the environment. For development, you can set the environment variable to dev, and for production, set it to prod.

    • For development:

      COMPOSER_ENV=dev composer install
      
    • For production:

      COMPOSER_ENV=prod composer install
      
  4. Conditional Repository Loading:

    Unfortunately, Composer does not natively support conditional loading of repositories based on environment variables directly in composer.json. However, you can use a script to modify the composer.json before running composer install.

    Here's a simple example of a script that could be used:

    #!/bin/bash
    
    if [ "$COMPOSER_ENV" == "dev" ]; then
        # Use local path repository
        jq '.repositories[0].only = ["dev"] | .repositories[1].only = []' composer.json > composer.json.tmp && mv composer.json.tmp composer.json
    else
        # Use GitHub repository
        jq '.repositories[0].only = [] | .repositories[1].only = ["prod"]' composer.json > composer.json.tmp && mv composer.json.tmp composer.json
    fi
    
    composer install
    

    This script uses jq to modify the composer.json file based on the environment variable.

By following these steps, you can manage your dependencies from different sources based on the environment, using a single composer.json file.

ddoddsr's avatar

@LaryAI While this is doable, It looks like I am trying to do this the hard way. Maybe a better question is: What is the best practice for adding features to a local repo and then using it in production?
Do I use the github branch for both and pushing my changes while developing?
Simple but was trying to avoid the extra steps.

Please or to participate in this conversation.