25

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
3

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 Comments

A little simpler, and also safer by specifying the "vendor" directory: find vendor -type d -name \.git -exec rm -rf \{\} \;
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.
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

Comments

2

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"]
},

Comments

1

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.