24

I found a couple related posts, but those don't really answer my question. Let's say I want to install this package: https://github.com/pear/Net_Socket

an excerpt from my composer.json:

{
    "config": {
        "preferred-install": "dist"
    },
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/pear/Net_Socket.git"
        }
    ],
    "require": {
        "pear/net_socket": "*",
    }
}

So I need it to be installed without the .git directory so it is not seen as a submodule in my project. How do I make it download a "dist" version, like others said? Do I have to tag a commit?

5 Answers 5

20

If a package is seen as a git submodule, you haven't excluded the vendor folder from being committed to your own repository. It is recommended to add the vendor folder to .gitignore, and not commit these files, only commit composer.lock (and composer.json of course).

Apart from that, running composer install --prefer-dist should do the job. Note that Composer seems to not change the download method used first if you change your mind later. If Composer detects a cloned repo, it is faster to just update that instead of downloading a ZIP and unpacking it. If you want to change that, delete the whole vendor folder and run composer update --prefer-dist.

1
  • But what If I like to keep my vendor folder as submodule? I made it as submodule so I can see all changes which updates are making, to identify new bugs coming from updated code.
    – Black
    Apr 29, 2021 at 8:31
2

Using --prefer-dist is the only native solution, but there will be situations where there is simply no packaged version available, and in those cases Composer will still fall back to git clones.

The only workaround I know of is to run a cleanup script after composer install that removes Git directories. Maybe a command like this:

find . -type d | grep .git | xargs rm -rf

Be careful to run this in your vendor directory, not your root directory.

2
  • 5
    A little simpler, and also safer by specifying the "vendor" directory: find vendor -type d -name \.git -exec rm -rf \{\} \; Sep 19, 2017 at 1:07
  • 2
    DON'T USE THE COMMAND FROM THE ANSWER. The find grep xargs rm command is completely unsafe. First, it will delete "xgit", "bak.git", ".github", etc. directories (because it uses grep .git instead of something like grep '\/\.git$'). Second, it will not work correctly with a space containing directories and will delete "my" and "configs" directories when "my .git configs" directory is exist (because it uses xargs instead of -exec). Look at my answer below for a safe command.
    – Mihail H.
    Jun 2, 2022 at 15:06
2

composer update --prefer-dist works fine, but it might be simpler to just change the default download method for composer packages (composer.json):

{
"config": {
    "preferred-install": {
        "*": "dist"
    }
  }
}

https://getcomposer.org/doc/06-config.md#preferred-install

0
1

To safely delete only ".git" directories inside "vendor" directory, use:

find vendor -type d -name '.git' -exec rm -rf \{\} \+
0

May this works well even through the Q has been put up so far a long time. It's in composer.json

"scripts": {       
    "post-update-cmd": ["echo [WARNING] Delete all .git dirs", "rm -rf vendor/**/**/.git"],
    "post-install-cmd": ["echo [WARNING] Delete all .git dirs", "rm -rf vendor/**/**/.git"]
},

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.