96

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.

Brandon Buster
  • 1,195
  • 1
  • 9
  • 12

10 Answers10

211

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
miken32
  • 42,008
  • 16
  • 111
  • 154
drowsyTicker
  • 2,279
  • 2
  • 10
  • 17
  • 1
    This worked perfectly for composer 1.3.1 on OSX 10.10, thankyou – Davey Jan 21 '17 at 18:13
  • "-- sudo mv composer.phar /usr/local/bin/composer" or "sudo mv composer.phar /usr/local/bin/composer" it helps if you actually use a code block – JGallardo Feb 14 '17 at 19:38
  • 2
    For me, the command has worked after opening a new console (to take effect) – khaled_webdev Jan 26 '18 at 10:42
  • 2
    Thanks, worked. Do you know, that in 12 years since I am doing stuff on unix/debian it NEVER happened that one of the commands I found worked in the first place? there is ALWAYS an error, and I always have to GOOGLE and fix it..bla.bla.. – bodomalo May 26 '20 at 15:11
  • i am getting the error mv: rename composer.phar to /usr/local/bin/composer: No such file or directory – user9437856 Nov 27 '22 at 15:39
  • Note that when moved, the ".phar" extension is removed from the file name. – Troy Niemeier Jul 27 '23 at 15:32
86

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

BSB
  • 2,270
  • 17
  • 26
59

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

Sven
  • 69,403
  • 10
  • 107
  • 109
  • Prefixing a reference to the current directory worked, ./composer.phar -v. Thanks for the answer, thank you also for the explaination! – Brandon Buster Dec 02 '15 at 02:02
44

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
Nishant Shah
  • 2,022
  • 22
  • 20
37

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.

Nitin Nain
  • 5,113
  • 1
  • 37
  • 51
12

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

Nanhe Kumar
  • 15,498
  • 5
  • 79
  • 71
  • on run this command sudo mv composer.phar /usr/local/bin/ get bolow issue sudo: /etc/sudo.conf is world writable sudo: /usr/bin/sudo must be owned by uid 0 and have the setuid bit set – yagnik devani Jun 03 '20 at 11:54
  • yes its work for me alias composer="php /usr/local/bin/composer" – matinict Nov 22 '22 at 20:36
12

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
Ankur Kedia
  • 3,453
  • 1
  • 13
  • 15
-1

https://getcomposer.org/download/ it might help

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '906a84df04cea2aa72f40b5f787e49f22d4c2f19492ac310e8cba5b96ac8b64115ac402c8cd292b8a03482574915d1a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php

this code for ubuntu and wsl2.

Krunal Akbari
  • 316
  • 1
  • 17
-1

Open terminal and run the following command,

sudo ln -s /usr/bin/php71 /usr/bin/php
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
-1

Works in RHEL 8, php 7.4

sudo mv composer.phar /usr/bin/composer