68
votes

From within the directory holding my composer.phar file, I can't execute any composer commands.

I can see Composer is running when I execute

php composer.phar 

But any direct composer statements fail.

Not sure if it matters but Composer was included within a cloned repository.

I just want to install a single Oauth library, then likely not touch Composer again for several months, so I don't need to run it globally. I'm just confused why I can't run Composer from within this directory.

7
Windows, Mac, or Linux machine? - MiDri
What is a 'direct composer statement'? - code-kobold
php composer.phar require foo/bar - Sammitch
We need more information to be any help here. Do you have an error message? What command are you trying to run that's failing? - HPierce
an example statement would be "composer -v". I get a Command Not Found error. I'm on a Mac - Brandon Buster

7 Answers

182
votes

This problem arises when you have composer installed locally. To make it globally executable,run the below command in terminal

sudo mv composer.phar /usr/local/bin/composer

For CentOS 7 the command is

sudo mv composer.phar /usr/bin/composer
59
votes

I am using CentOS and had same problem.

I changed /usr/local/bin/composer to /usr/bin/composer and it worked.

Run below command :

curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/bin/composer

Verify Composer is installed or not

composer --version

50
votes

Your composer.phar command lacks the flag for executable, or it is not inside the path.

The first problem can be fixed with chmod +x composer.phar, the second by calling it as ./composer.phar -v.

You have to prefix executables that are not in the path with an explicit reference to the current path in Unix, in order to avoid going into a directory that has an executable file with an innocent name that looks like a regular command, but is not. Just think of a cat in the current directory that does not list files, but deletes them.

The alternative, and better, fix for the second problem would be to put the composer.phar file into a location that is mentioned in the path

32
votes

MacOS: composer is available on brew now (Tested on Php7+):

brew install composer

Install instructions on the Composer Docs page are quite to the point otherwise.

27
votes

This is for mac or ubuntu user, try this on terminal

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

or mac brew can try

brew install composer
8
votes

Step 1 : Open Your terminal

Step 2 : Run bellow command

          curl -sS https://getcomposer.org/installer | php

Step 3 : After installation run bellow command

          sudo mv composer.phar /usr/local/bin/

Step 4 : Open bash_profile file create alias follow bellow steps

           vim ~/.bash_profile

Step 5 : Add bellow line in bash_profile file

          alias composer="php /usr/local/bin/composer.phar"

Step 6 : Close your terminal and reopen your terminal and run bellow command composer

8
votes

First I did alias setup on bash / zsh profile.

alias composer="php /usr/local/bin/composer.phar"

Then I moved composer.phar to /usr/local/bin/

cd /usr/local/bin
mv composer.phar composer

Then made composer executable by running

sudo chmod +x composer