How to Install Composer 1 and Composer 2 Side by Side on macOS

Máximo Martinez Soria

Dec 6, 2021 | 2 min to read |

Tooling

While most projects will likely use Composer 2, there are times when you need Composer 1 to make changes to an older project that was originally built using Composer 1.

The most common way to handle this is by switching versions using an update. To switch to Composer 1: composer self-update --1.

To switch to Composer 2: composer self-update --2.

However, this is not very practical. If you need to run these commands several times a day, it quickly becomes a waste of time.

A much better alternative is to install both versions and use different commands for each one. In my case, I like to use composer for Composer 2 and composer1 for Composer 1.

This way, I never need to downgrade.

Setting this up is quite simple. Let’s see how to do it…

  1. Install Composer using Homebrew. This will install version 2.

    brew install composer
  2. Rename the composer binary to composer2 so the versions can be distinguished.

    mv /usr/local/bin/composer /usr/local/bin/composer2
  3. Move into the installation directory.

    cd /usr/local/bin/
  4. Download Composer 1.

    wget https://getcomposer.org/download/1.10.17/composer.phar
  5. Change permissions so the file can be executed.

    chmod 755 composer.phar
  6. Rename the binary to composer1.

    mv /usr/local/bin/composer.phar /usr/local/bin/composer1
  7. Create a symlink to define which command points to each version. Important: run only one of the following commands. The “default” version is the one executed with the composer command. The other version will be executed using composerX, where X is the version number.

    # Composer 2 as default
    # Commands: composer (Composer 2) and composer1
    ln -s /usr/local/bin/composer2
    
    # Composer 1 as default
    # Commands: composer (Composer 1) and composer2
    ln -s /usr/local/bin/composer1

Done! It’s that simple to have both versions of Composer installed and use one or the other whenever needed.

Author

Máximo Martinez Soria