Installing Composer on Mac OSX

Composer is a cross-platform dependency manager for PHP libraries.

In this guide, i'm installing Composer on a machine running Mac OSX with MAMP.

  1. Open a terminal and navigate to your user directory, ie cd /User/<USER_NAME>/
  2. Run this command shown below to download Composer. This will create a Phar (PHP Archive) file called composer.phar:
    curl -sS https://getcomposer.org/installer | php
    
  3. Now we move composer.phar file to a directory
    sudo mv composer.phar /usr/local/bin/
  4. We want to run Composer with having to be root al the time, so we need to change the permissions:
    sudo chmod 755 /usr/local/bin/composer.phar
    

     

  5. Next, we need to let Bash know where to execute Composer: 
    nano ~/.bash_profile
    

    Add this line below to bash_profile and save
     

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

    and then run this command:
     

    source ~/.bash_profile

     

  6. Finally, run: 
    composer -v
    

    If all goes well and it is working, you'll see this screen:
    Composer installed

 

Updated this post on 7 Dec 2020

Composer version 2 has been released for a few months now. Since i upgraded Drupal 9 to 9.1, it recommends to use Composer 2. So i thought it might as well be time to upgrade. The benefits it brings is that it is supposed to be much faster

If you have already installed Composer 1 and feel it is time to upgrade, the process is really simple. First, update to latest version. Type and run:

composer self-update

If you run composer self-update again, you will see this message:

Composer self-update

 To upgrade to Composer 2.x from older version, type and run:

composer self-update --2

While using Composer 2.x and you are experiencing some issues using it with your project, you can always downgrade back to 1.x version. To do this, type and run:

composer self-update --rollback

Note: When you try to upgrade Drupal 9 with composer 2. You will get this error:

Problem 1

    - oomphinc/composer-installers-extender[v1.1, ..., v1.1.2] require composer-plugin-api ^1.0 -> found composer-plugin-api[2.0.0] but it does not match the constraint.

    - Root composer.json requires oomphinc/composer-installers-extender ^1.1 -> satisfiable by oomphinc/composer-installers-extender[v1.1, v1.1.1, v1.1.2].

What you will need to do first is to update the plugin (composer installer extender). To do this, open your your composer.json file and change this line to use ^2.0 version:

"oomphinc/composer-installers-extender": "^2.0"

 

Now you can try and run:

composer update

 

Walter (not verified), 20 Mar 2020 - 2:09pm
Perfect! Thank you
Reynaldo (not verified), 21 Mar 2020 - 5:07pm
It worked perfectly! Thanks, man (or girl, idk).
James (not verified), 21 Mar 2020 - 6:30pm
That's brilliant. Clear, simple, easy to follow instructions. Thank you!
Damien (not verified), 23 Mar 2020 - 6:25pm
Wonderful !! Merci !
Arunjith R S (not verified), 24 Mar 2020 - 4:01pm
Perfect...Thanks alot
Robson Niedson (not verified), 24 Mar 2020 - 8:27pm
Obrigado. Funcionou bem!
Tércio (not verified), 30 Mar 2020 - 4:50am
Very good..
Daniel (not verified), 01 Apr 2020 - 8:18am
mv: rename composer.phar to /usr/local/bin/: No such file or directory
ERROR
Lemaro (not verified), 02 Apr 2020 - 12:33pm
You are the best! Thx God! Add it please to official docs, cause there not smart instructions for bots :\
Arif Budiman (not verified), 17 Apr 2020 - 1:31am
thank you bro
Phoenix2222 (not verified), 18 Apr 2020 - 8:33pm
Thanks Bro =)
Bryan from Smmile (not verified), 20 Apr 2020 - 4:56pm
Thanks, best tutorial!
Ace5193 (not verified), 25 Apr 2020 - 5:35pm
Thank you
Marco Castellani (not verified), 08 May 2020 - 9:56am
Thanks a lot!
Michael (not verified), 16 Jun 2020 - 8:44am
You can skip step 5 if you do this in step 3:

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

and then also lose the ".phar" in step 4
Aarti GONDHALEKAR (not verified), 15 Jul 2020 - 9:46pm
I have some code already written for anaconda in my .bash_profile file. so if i type alias composer="php /usr/local/bin/composer.phar", it throws an error. What do i do .Pls help.

"# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/opt/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/opt/anaconda3/etc/profile.d/conda.sh" ]; then
. "/opt/anaconda3/etc/profile.d/conda.sh"
else
export PATH="/opt/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<"
li xiaoyao (not verified), 16 Jul 2020 - 7:18am
Thankx
Dean (not verified), 09 Sep 2020 - 1:53pm
Thank you... just what I needed and straight to the point.
Christian (not verified), 14 Sep 2020 - 2:29pm
sudo mv composer.phar /usr/local/bin/composer is very simple
Marmor (not verified), 04 Dec 2020 - 9:37pm
Thank you very much!
Graeme (not verified), 31 Dec 2020 - 11:20am
Cheers, helped a lot - nice simple steps!
Beth (not verified), 07 Jan 2023 - 12:01pm
Thanks so much. For a command-line beginner like me, this helped immensely.
FYI for anyone working on a newer Mac OS, terminal prefers ZSH as its default shell (instead of Bash). That means that a couple of lines change because you write the alias to .zshrc instead of .bash_profile:

nano ~/.zshrc

and

source ~/.zshrc

And, pay attention to where the composer.phar file was saved. It might not be at /usr/local/bin...
jp oconnor (not verified), 13 Feb 2023 - 2:56pm
brilliant - i was struggling for days!
you saved much
total respect
The content of this field is kept private and will not be shown publicly.
Your email address will be kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.