How to install Composer on Ubuntu 18.04

Table of Contents
Install Composer on Ubuntu
Composer is one the best dependency management tool 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 Ubuntu 18.04.
Prerequisites
Before you start to install Composer on Ubuntu 18.04. You must have the non-root user account on your server/desktop with sudo privileges.
Install Composer
Before you install Composer you should update apt package manager index by typing.
sudo apt update
Now you should install some dependencies for Composer, you can do so by typing
sudo apt install curl php-cli php-mbstring git unzip
Enter 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 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 following output:
Installer Verified
If you dont see above output you may get Installer Corrupt output. If this case download composer again and check 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 following command to confirm the installation
composer
You will see 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 above output, you have installed composer successfully.
Use Composer In PHP Project
Now Composer is globally installed on your system. To use composer you should have a root directory for your project inside that directory you will install the dependency package with Composer.
Create directory NewProject
as a root directory of your project.
sudo mkdir NewProject
cd NewProject
Now install latest version of guzzlehttp/guzzle
package by using following command.
composer require guzzlehttp/guzzle
After installing 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 total 12 -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 following code inside that file. It will check statuscode for a URL if it loads successfully it will give 200 otherwise different
number.
<?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 above script by typing
php test.php
Output should be
statuscode : 200
If you want to update package then you can use following command.
composer update
Conclusion
You have successfully learned how to install Composer on Ubuntu 18.04. If you have any queries regarding this dont forget to comment bellow.
LATEST POSTS
-
How to List Installed Packages on Ubuntu
-
How to Install Papper Flash on Linux Mint 19
-
How to Disable SELinux on CentOS 8
-
How to install Node.js with npm on Ubuntu 18.04
-
How To Check Ubuntu Version
-
Generate Random Unique String in Laravel
-
How to Install Git on Manjaro 18
-
How to Install Gradle on CentOS 7
-
How to Setup Passwordless SSH Login in Linux
-
How to Install Anaconda on Linux Mint 19
-
How to Change Hostname on CentOS 7
-
How to Copy, Cut, Paste in Vim or Vi