
Install Composer on Debian
Composer is one of the best dependency management tools for PHP which can install and update project dependency seamlessly. When installing a package it also checks on which another package current package depends on and then it installs all the dependencies. In this tutorial, you are going to learn how to Composer on Debian 9 system.
Prerequisites
Before you start to install Composer on Debian 9 system. You must have the non-root user account on your server/desktop with sudo privileges.
1. Install Composer
First Update apt package manager index by typing.
sudo apt update
Next, you should install some dependencies for Composer, you can do so by typing
sudo apt install curl php-cli php-mbstring git unzip
Enter the following commands to download Composer Setup.
cd ~ curl -sS https://getcomposer.org/installer -o composer-setup.php
Now, you should verify that the installer matches the SHA-384 hash for the data integrity of the latest installer found on the Composer Public Keys or Signatures page by running the following command.
HASH="$(wget -q -O - https://composer.github.io/installer.sig)"
Check if installation script is corrupted by
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 should get the following output:
Installer Verified
If you don’t see the above output you may get Installer Corrupt output. If this case downloads the composer again and checks hash value until you get Installer Verified output.
Now run following command to install Composer globally inside /usr/local/bin
directory.
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
You should get the following output
Output All settings correct for using Composer Downloading... Composer (version 1.6.3) successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composer
Run the following the command to confirm the installation
composer
You will see the following output
______ / ____/___ ____ ___ ____ ____ ________ _____ / / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/ / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/ /_/ Composer 1.6.3 2018-01-31 16:28:17 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
If you see the above output, you have installed the composer successfully.
Use Composer In PHP Project
Now Composer is globally installed on your system. To use the composer you should have a root directory for your project inside that directory you will install the dependency package with Composer.
Create the directory NewProject
as a root directory of your project.
sudo mkdir NewProject
cd NewProject
Now install the latest version of guzzlehttp/guzzle
package by using the following command.
composer require guzzlehttp/guzzle
After installing the package you will see that Composer created three files composer.json file,composer.lock file which contains package names with version, and vendor directory.
Enter the following command to check.
ls -l
Output -rw-rw-r-- 1 linux4one admin 59 Nov 11 20:13 composer.json -rw-rw-r-- 1 linux4one admin 2934 Nov 11 20:13 composer.lock drwxrwxr-x 4 linux4one admin 4096 Nov 11 20:13 vendor
Now, you have installed package guzzle, create a file test.php and copy the following code inside that file. It will check status code for a URL if it loads successfully it will give 200 otherwise different
numbers.
<?php require __DIR__ . '/vendor/autoload.php'; use GuzzleHttp\Client; $client = new Client(); $res = $client->request('GET', 'https://api.github.com/repos/guzzle/guzzle'); echo "statuscode : ".$res->getStatusCode();
Run the above script by typing
php test.php
Output should be
statuscode : 200
If you want to update the package then you can use the following command.
composer update
Conclusion
You have successfully learned how to install Composer on Debian 9 system. If you have any queries regarding this don’t forget to comment below.