How To Install PHP Composer on Ubuntu 18.04

Introduction

Composer is an incredible dependency manager tool used to install and upgrade libraries for dynamic, PHP applications and projects. Many open-source, PHP-based frameworks and projects utilize Composer to manage their dependencies and packages.

For this reason, it may be vital to install Composer on your Ubuntu system for these applications to work and get updated flawlessly.

This tutorial will help you install PHP Composer on Ubuntu 18.04 system.

Ready? Let’s get started!

Step 1 - Installing All The Required Dependencies

Before we install PHP Composer, you should ensure that all the required dependencies are properly installed on your Ubuntu 18.04 server.

First, execute the command below to update Ubuntu 18.04 package manager cache:

$ sudo apt update

Next, run the command below to install the dependencies:

$ sudo apt install curl php-cli php-mbstring git unzip

That’s it! By now you should have all the dependencies installed. The stage is set and we can now install PHP composer

Step 2 - Installing PHP Composer

Installing Composer is easy thanks to an installer which is easy to implement on the Ubuntu system. We’ll first download the installer and authenticate the application, before using it to install PHP Composer.

Execute the commands below to change to the home directory and retrieve the installer:

$cd ~
$ curl -sS https://getcomposer.org/installer -o composer-setup.php

Next, we need to ensure the Installer rhymes with the SHA384 hash for the most recent installer available on the [Composer Public Keys / Signatures][composer-sigs] website. Copy the hash on this website, and save it in a format similar to a shell variable.

$ HASH=544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061

Make sure you replace the latest hash with the highlighted value.

Run the script below to authenticate the installation script:

$ php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

If everything is okay, you should get the output below:

Installer verified

If you get a different output (Installer corrupt), then download the installation script again and confirm that you are using the right hash.

Furthermore, the Composer can be installed globally. To accomplish this execute the command below:

$ sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

The above command will download and implement PHP Composer as a server-wide command called composer, in the directory /usr/local/bin. The installation will give you the output below:

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

Execute the command below to test the installation:

$ composer

This will give you an output showing Composer’s arguments and versions

/ ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ / __ `__ / __ / __ / ___/ _ / ___/
/ /___/ /_/ // // // /_/ //_/ (__  )  __/ /
____/____/_/ /_/ /_/ .___/____/____/___/_/
                    /_/
Composer version 1.6.52018-06-0910:54:44

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: 1for normal output, 2for more verbose output and 3for debug
. . .

The output signifies the success of the installation.

Conclusion

That’s it! You have successfully installed PHP Composer on your Ubuntu 18.04 server. Next, you need to learn how to use Composer in your PHP projects, how to add the Autoload script, and how to update all the project dependencies. Follow our guide on “How To Use PHP Composer?” to learn all these tasks.

Check out these top 3 Linux hosting services

Was this article helpful?