Installing Composer Quick Start

Published Jan 28, 2021
Updated Feb 16, 2021
By Simon

Composer is a PHP dependency manager, for those from JavaScript and Node.js it is the cousin of npm and uses a composer.json file in the same way npm uses a packages.json.

Composer is my preferred way to install and manage a Drupal website these days. I had been using Drush with Drupal 7 for quite some time but overtime have needed to use Composer to install some modules with libraries, one off the top of my mind being SMTP Authentication, and installing any way other than using composer has been a futile task.

In this article we will look at setting up Composer on your server. I have linked to the official documents and suggest you go there to get any the install scripts. This guide could be considered a quick start guide bringing together the few scripts needed to get Composer running on your server. If you have problems make sure you check the documents or search for the error or warning on a search engine, or drop me a message.

Prerequisite

You will need SSH Terminal access and a sudo user.

You need PHP installed to install and run composer. This should be already installed if you have a LAMP stack set up. My current LAMP stack is:

  • Ubuntu 20.04 LTS
  • Apache 2
  • MySQL
  • PHP7.4

Install Composer

When Composer is initially installed it is downloaded and installed in the current directory and can be run from that directory. We will move the composer.phar archive to be used globally in the second step.

With that said, log in to your server and move into the directory you want to use. I usually download it to a virtual host in the /var/www/domain-name/public_html, but if you do that make sure you don't leave it there as it would be a security risk.

Once in the directory run the script found on the download page, please grab the code from that page, I have only included it here for reference and removed the SHA checksum number.

https://getcomposer.org/download/

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'some-long-SHA-checksum-number-goes-here') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

With the script you can copy all 4 lines and paste it in the terminal, each line will be run doing the following:

  • Download the installer.
  • Check the integrity of the installer.
  • Install composer.
  • Remove the installer.

After it has installed you can check that is installed by running the .phar archive using PHP with the following script.

php composer.phar this should give you this output.

Image
composer terminal output

Moving composer to a global location

As stated it is not very secure to leave the composer.phar in a public_html directory so moving it to a global location is recommended. Generally adding it to the system PATH is a good place.

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

By moving it into a composer directory in your system's PATH you now use Composer by running composer.

Try running composer or composer -v

composer will give the same output as php composer.phar and composer -v with give you a v number only.

If you have any issues with moving Composer into the system PATH check the Composer site at the following location https://getcomposer.org/doc/00-intro.md#globally

Welcome to the world of Composer

Run these two scripts and you will have Composer up and running. Once you are comfortable with the process there are some other options, listed on the download page linked at the start, you can use on the php composer-setup.php line such as --install-dir=bin. Have a play around with these to make the install process even faster.

That's it, pretty easy, now you can run Composer from anywhere on your system. It is suggested not to have Composer set up on a production server though and that it should only be used on development and staging servers and then use a GIT workflow to move updates to production but that's a different story which can be covered in the future.

Happy Composing & thanks for reading.