Let’s install Composer in less than 2 minutes

L

Do you need to get Composer installed quickly? If so, you’ve come to the right place.

Prerequisites

Before we get started with the installation, this guide assumes that:

  • You have PHP installed on your machine
  • You’ve got your user permissions in order
  • wget is installed on your machine
  • You have SSH or terminal access to your machine
  • You’re running this installation on a machine that’s powerful enough to run Composer. (Raspberry Pi’s have memory issues when installing Composer)

If you satisfy the requirements above, we’ll proceed with the installation process.

Step 1 : Download & Run The Installer

To install Composer, we’ll create a shell script to download & install Composer, shortly followed by verification. Let’s create a file using your favorite text editor, and let’s call it composer-installer.sh. Copy/paste the following into that file:

#!/bin/sh

EXPECTED_SIGNATURE="$(wget -q -O - https://composer.github.io/installer.sig)"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_SIGNATURE="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"

if [ "$EXPECTED_SIGNATURE" != "$ACTUAL_SIGNATURE" ]
then
    >&2 echo 'ERROR: Invalid installer signature'
    rm composer-setup.php
    exit 1
fi

php composer-setup.php --quiet
RESULT=$?
rm composer-setup.php
exit $RESULT

In a nutshell, the script above gets the expected hash of the installer, downloads the installer, and then compares the hash of the downloaded file.

If the two hashes do not match, something has gone wrong with the download process & does not let you proceed.

If the two hashes do match, the download was successful & now executes.

Step 2 : Set Permissions For The Installer

The installation file is now in-tact, but it needs to be have the proper permissions to execute. To do this, we’ll run the chmod command on the installer via terminal.

chmod 755 composer-installer.sh

This allows the installer to be executed by you, but not others in your user’s group & not everybody.

Step 3 : Run the installer

Now that the permissions are set, let’s run the installer. To execute, copy/paste the following command into your terminal.

./composer-installer.sh

Note : the commands in the installer are quiet, so you won’t see any output while it’s running. It should only take a few seconds to execute in its entirety.

Step 4 : Verify The Composer Installation

To verify that the installation was successful & that Composer is good to go, copy/paste the following command into your terminal.

php composer.phar -V

This asks Composer to tell you what version of Composer is installed. You should see an output similar to this.

Composer version 1.8.6 2019-06-11 15:03:05

Note: the version number & date displayed are indicative of the newest version of Composer available at the time of installation.

If you now see a Composer version, you have installed Composer successfully and can proceed to the next step.

If you do not see a Composer version, the installation has failed. To install, replicate the steps above & try again.

Step 5 : Delete the installer

Now that we’ve installed Composer successfully, we no longer need the installer. To delete the installer, run the following command in your terminal.

rm composer-installer.sh

Composer has been installed successfully & all unnecessary files have been removed.

Next Steps

Now that you’ve installed Composer, head on over to Packagist & start looking for some packages you’d like to install. If I may recommend some packages, here are a few of my favorites.

About Me

I’m a San Diego-based software developer & Dev-Ops engineer with a passion for anything relating to technology. I like well-documented code & living near the beach, but I don’t like sand.

Add Comment

About Me

I’m a San Diego-based software developer & Dev-Ops engineer with a passion for anything relating to technology. I like well-documented code & living near the beach, but I don’t like sand.

Social

Need to get in touch?

Recent Posts