Introduction
Composer is a popular dependency management tool for PHP, created mainly to facilitate installation and updates for project dependencies. It will check which other packages a specific project depends on and install them for you, using the appropriate versions according to the project requirements.
In this tutorial, you’ll install and get started with Composer on Debian 9.
Prerequisites
To complete this tutorial, you will need:
- One Debian 9 server set up by following the Debian 9 initial server setup guide, including a non-root user with
sudo
access and a firewall.
Step 1 — Installing the Dependencies
Before you download and install Composer, ensure your server has all dependencies installed.
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][composer-sigs] 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:
OutputAll settings correct for using Composer
Downloading...
Composer (version 1.7.2) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer
To test your installation, run:
composer
And you’ll see this output displaying Composer’s version and arguments.
Output ______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 1.7.2 2018-08-16 16:57:12
Usage:
command [options] [arguments]
Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--profile Display timing and memory usage information
--no-plugins Whether to disable plugins.
-d, --working-dir=WORKING-DIR If specified, use the given directory as working directory.
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
. . .
This verifies that Composer installed successfully on your system and is available system-wide.
网友评论