86

I followed composer manual (global installation of composer (manual)) to install composer on Ubuntu.

$ ll /usr/local/bin/
total 4760
drwxr-xr-x  2 root root    4096 2012-03-29 08:29 ./
drwxr-xr-x 10 root root    4096 2011-04-26 00:50 ../
-rwxr-xr-x  1 root root  410324 2012-03-29 08:28 composer.phar

Other scripts from /usr/local/bin/ works, but composer gives:

$php composer.phar update Could not open input file: composer.phar

It works only if I enter absolute path to composer.phar. How to fix this?

muru
  • 193,181
  • 53
  • 473
  • 722
umpirsky
  • 4,020
  • 17
  • 58
  • 85

8 Answers8

200

TLDR;

As described on the Composer website:

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

Manual Method

I found an easier way to globally install composer than the manual proscribed in the github readme.md. It's actually on the getcomposer.org website:

curl -s http://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/

And if you're even more lazy, like me, you can create an alias:

alias composer='/usr/local/bin/composer.phar'

This way you can invoke composer with just composer

chrisjlee
  • 10,716
  • 14
  • 45
  • 51
  • 12
    Nice. The alias is a sweet touch for obsessive compulsives too. Typing in file extensions? Pfeh! That's for chumps – eggonlegs Aug 15 '12 at 14:00
  • 5
    Laziness, mother of creativity. Last update is the best ! – Pierre de LESPINAY Oct 21 '13 at 07:27
  • when I run `$ sudo mv composer.phar /usr/local/bin/composer` and then try to type `$ composer` I get the error: `bash: /usr/local/bin/composer: Permission denied` How can I change these permissions? – Connor Leech Jan 13 '14 at 09:19
  • @ConnorLeech You can run `chmod +x /usr/local/bin/composer` – Chibueze Opata Mar 20 '14 at 22:23
  • Since the last update is the best, I suggest removing everything before that update. What do you think, @chrisjlee? – Kim Stacks Mar 28 '14 at 02:46
  • 1
    @KimStacks I think it makes sense to keep it there context. – chrisjlee Mar 28 '14 at 03:39
  • At least you move the best method to the top. That's good! – Kim Stacks Mar 28 '14 at 04:07
  • this is my adaptation of your wonderful answer. https://gist.github.com/simkimsia/0c218cadbb3055d53417 Only difference is I move it to /usr/share and made it accessible by all users – Kim Stacks May 05 '14 at 07:10
  • when I put `curl -sS https://getcomposer.org/installer | php` it says `curl: (6) Couldn't resolve host 'getcomposer.org'` whats wrong with that?? – Thusitha Sumanadasa Dec 08 '14 at 12:15
  • @ThusithaSumanadasa LMGTFY: https://www.google.com/search?q=curl%3A+(6)+Couldn%27t+resolve+host&oq=curl%3A+(6)+Couldn%27t+resolve+host&aqs=chrome..69i57j69i58.296j0j1&sourceid=chrome&es_sm=122&ie=UTF-8 http://stackoverflow.com/questions/24221642/following-url-curl-couldnt-resolve-host-get :) – chrisjlee Dec 09 '14 at 05:28
  • Also, don't forget that aliases will be forgotten after you close the terminal session. You have to place that line to your .bashrc or .bash_profile file. – totymedli Apr 28 '15 at 15:04
29

Don't add php in the beginning. Just call composer.phar.

Eliah Kagan
  • 116,445
  • 54
  • 318
  • 493
garakkio
  • 726
  • 9
  • 20
  • It works, thanks. Explanation would be great, I'm curious. Thanks again. – umpirsky Jun 19 '12 at 19:18
  • 2
    It works because composer is already an executable (it has u+x rights by default). So, no need to execute php, because shell will do it anyway. – netvision73 Jan 09 '14 at 10:36
  • 3
    And there is a special code on the first line of composer.phar, `#!/usr/bin/env php` which tells ubuntu that the file is a php file and should be run with the php program. – T0xicCode Feb 08 '14 at 19:36
28

Actually, getcomposer.org now recommends a simpler method:

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

Now you can just use composer without bothering with an alias or a separate sh script.

BenjaminRH
  • 465
  • 5
  • 12
7

Another alternative to get a nice composer command instead of composer.phar:

$ curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin
$ ln -s /usr/local/bin/composer.phar /usr/local/bin/composer
Michael Härtl
  • 490
  • 1
  • 4
  • 17
5

for easier execution I created /usr/local/bin/composer with content:

#!/bin/sh
exec /usr/local/bin/composer.phar "$@"

dont forget about sudo chmod +x /usr/local/bin/composer.phar

Eliah Kagan
  • 116,445
  • 54
  • 318
  • 493
bazilio91
  • 51
  • 1
  • 1
3

Below are the steps to be followed to install composer globally: 1. Before installing Composer, make sure our server has all dependencies installed.

1st, update the package manager cache by running:

$ sudo apt-get update

2. Now, let's install the dependencies. We'll need

  • curl => to download Composer
  • php5-cli => installing and running it
  • git => used by Composer for downloading project dependencies

Everything can be installed with the following command:

$ sudo apt-get install curl php5-cli git

3. Composer installation with a single command:

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

4. This will download and install Composer as a system-wide command named composer, under /usr/local/bin. The output should look like this:

Output:

\#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: /usr/local/bin/composer

Use it: php /usr/local/bin/composer

5. To test your installation, run:

$ composer -V

Output:

Composer version 1.1.3 2016-06-26 15:42:08
  1. Installation Successful
Nick Weinberg
  • 4,555
  • 4
  • 24
  • 30
Ravistm
  • 131
  • 4
0

I have made a simple bash script that automatically installs composer for current user/all users, you can one of the following commands in your terminal to use bash script.

Current User

wget -O - https://gist.github.com/EmpireWorld/1dd5f59566e186907f99dc16badc382a/raw/install-composer-local.sh | bash

All Users

wget -O - https://gist.github.com/EmpireWorld/1dd5f59566e186907f99dc16badc382a/raw/install-composer-global.sh | bash

Check out the Gist

Hasan Bayat
  • 107
  • 3
-1

This might work for you. Note that sudo is used two times here.

sudo curl -sS https://getcomposer.org/installer | sudo php
grooveplex
  • 2,486
  • 3
  • 25
  • 35
do_do_do
  • 99
  • 3
  • 1
    Hi! Welcome to Ask Ubuntu. As it stands, it's not clear why the commands might solve the issue the OP has. Can you please [edit](https://askubuntu.com/posts/788118/edit) your question and elaborate on your answer? – grooveplex Jun 17 '16 at 10:45