102

I don't know if this question has been asked, because searching finds results mostly about moving the libraries installation directory.

I have a globally installed composer command. Is there a way to run, for example, composer install in a different directory than current, i.e. to specify the directory in which I would like tu run the command?

E.g. being in /home/someuser, I would like to acquire the same result as in running composer install it inside /home/someuser/myproject. Of course, one way would be to simply change the current directory, run composer and go back.

6 Answers 6

200

Try composer install -h. There you'll find an option --working-dir (or -d). And that's what you're looking for.

Then run:

composer install --working-dir=/home/someuser/myproject

You can find more in composer docs.


Depending on your operating system, the = might need to be removed:

composer install --working-dir /home/someuser/myproject
5
  • 6
    -d=/home/someuser/myproject actually throws an error for me. Current version of composer seems to accept -d /home/user/myproject/ instead. Aug 6, 2018 at 23:09
  • 3
    AFAIK it depens on the operating system (Windows, Linux, OS X). Thank you for your comment, I've updated the answer to make the OS differences more clear. Aug 7, 2018 at 6:33
  • 1
    using = made my build fail a million times, thanks for adding this detail :)
    – Levi
    Dec 5, 2019 at 14:38
  • 1
    It was not working on linux. You need to create composer.json file with curly brackets: {} in it. And then all was ok.
    – liutis
    Dec 9, 2021 at 12:31
  • 1
    works excellent for me May 8, 2022 at 14:53
17

In addition to the above answer from Tomáš Votruba i had to append the = charachter on OSX. So the full command would be:

composer install -d=/home/someuser/myproject

My first post on SO so was unable to simply add this as a comment.

0
4

This works for me, PHP 7.3 on ubuntu 18.04

Install

composer install --working-dir=/your_composer_dir

Update

composer update --working-dir=/your_composer_dir

0
1

I am using a Windows machine with PHPStorm (terminal) and this worked for me.

composer install --working-dir /home/someuser/myproject

My Linux OS machines require me to use

composer install --working-dir=/home/someuser/myproject

Note: You may be able to substitute ~/ for /home/someuser/ if your path is super long.

0

I tried what others said, but it was giving me: Invalid working directory specified 'PATH' does not exist. Although it was my working dir that contained composer.json!

I don't know why anyway, but this worked for me (only for gnu/linux users):

composer --working-dir=$(pwd)

And by the way, if you had run composer -h, it would've told you the solution:

-d, --working-dir=WORKING-DIR If specified, use the given directory as working directory.

-6

Run:

cd /home/mysites/google.com

Then run:

composer require facebook/graph-sdk

Above steps will open up the directory named (google.com) and install facebook Graph SDK there.

1
  • 2
    I don't think that changing the directory is wanted. Additionally, if the requirement is to run composer install, why did you introduce some package to be installed?
    – Nico Haase
    Apr 11, 2019 at 12:21

Your Answer

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.