99

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.

Przemek
  • 6,300
  • 12
  • 44
  • 61

6 Answers6

197

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
Tomas Votruba
  • 23,240
  • 9
  • 79
  • 115
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.

Eric Mulder
  • 171
  • 1
  • 3
5

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

M_R_K
  • 5,929
  • 1
  • 39
  • 40
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.

wrg20
  • 21
  • 1
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.

X 47 48 - IR
  • 1,250
  • 1
  • 15
  • 28
-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.

henlly
  • 1
  • 1
  • 4
  • 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 '19 at 12:21