Monday, 11 March 2019

Install Composer

First, update the package manager cache by running:
  • sudo apt update
Now, let's install the dependencies. We'll need curl in order to download Composer and php-cli for installing and running it. The php-mbstring package is necessary to provide functions for a library we'll be using. git is used by Composer for downloading project dependencies, and unzip for extracting zipped packages. Everything can be installed with the following command:
  • sudo apt install curl php-cli php-mbstring git unzip
With the prerequisites installed, we can install Composer itself.

Step 2 — Downloading and Installing Composer

Composer provides an installer, written in PHP. We'll download it, verify that it's not corrupted, and then use it to install Composer.
Make sure you're in your home directory, then retrieve the installer using curl:
  • cd ~
  • curl -sS https://getcomposer.org/installer -o composer-setup.php
Next, verify that the installer matches the SHA-384 hash for the latest installer found on the Composer Public Keys / Signatures page. Copy the hash from that page and store it as a shell variable:
  • HASH=544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061
Make sure that you substitute the latest hash for the highlighted value.
Now execute the following PHP script to verify that the installation script is safe to run:
  • php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
You'll see the following output.
Output
Installer verified
If you see Installer corrupt, then you'll need to redownload the installation script again and double check that you're using the correct hash. Then run the command to verify the installer again. Once you have a verified installer, you can continue.
To install composer globally, use the following command which will download and install Composer as a system-wide command named composer, under /usr/local/bin:
  • sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
You'll see the following output:
Output
All settings correct for using Composer Downloading... Composer (version 1.6.5) successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composer
To test your installation, run:
  • composer

No comments:

Post a Comment