Commands to install PHP Composer on any Linux such as Ubuntu

What is PHP Composer? Composer is a PHP-based dependency manager that can be installed using PHP on Windows, macOS, Linux or FreeBSD using PHP itself. In returns, the Composer allows users to maintain applications in a modular manner to reduce the process of maintaining application components such as frameworks and libraries to a minimum. Dependency Managers also serve to greatly simplify the constantly recurring component installation process for new projects.

In short, it allows to install or update the libraries you need to manage any particular PHP project. For example, if we want to install a PHP based web framework called Silex. We can install it on PHP installed system with just one command of the Composer.

PHP Composer Installation on Linux Distros

Here, we let you know how to set up Composer on Ubuntu 19/18/17/16, macOS Linux Mint, Debian, CentOS, Elementary OS, MX Linux, Fedora, RedHat etc. the steps will be the same for all of them. However, for example, we are using Ubuntu as our test system on which we have PHP.

Note: The only requirement of this Dependency Manager is the PHP, thus a system where you are planning to install it should have PHP installed. Composer requires at least PHP 5.3.2+ to run.

Step 1: Update your Ubuntu System

To go further to download and install Composer, we must ensure the installed packages on our system are up to date. For that use the following command, if you are using any other Linux distro then run your respective system update command:

sudo apt-get update

Step 2: Download and install Composer

Now let’s install the dependencies if you don’t have already on your system.

sudo apt install wget php-cli php-zip unzip

Run the command to get the composer setup on your server or PC.

php -r "copy('', 'composer-setup.php');"

Step 3: Check the downloaded setup 

This command will verify the above-downloaded setup is from an authentic source and not corrupted.

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

Step 4: Run the Composer setup and remove it

The below command will install the Composer in the /usr/local/bin directory so that we can use it directly in the Terminal regardless of the directory in which it has been installed.

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

Step 5: Remove the setup

Finally, run the command to uninstall the Composer installer.

php -r "unlink('composer-setup.php');"

Step 6: Run the PHP Composer

Now, in the Terminal just type composer and you will be the following output

root@a6c5dcb5a59f:/var/www/html# composer
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
Composer version 1.9.2 2020-01-14 16:30:31

command [options] [arguments]

-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 a working directory.
--no-cache Prevent use of the cache
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:

Example: Setup some Project with Composer

Composer requires a file called composer.json in the main directory of your application to carry out the installation and import updates. As the file extension suggests, this file is in JSON format. Composer will automatically create a vendor directory when the components are installed, in which the autoloader and all installed components are stored.

We can either manually create a Composer.json file to define what components and dependencies we want to install or just use the flexible that is run the command by the package provider which will automatically create composer.json file for that particular project.

For example, Silex, which is an open-source microframework for front end web development. We can install it using a single command of the composer.

Simply, run: 

composer require silex/silex "^2.0"

And the above command will download all its dependencies, composer.json file and Vendor folder.

Now, if you want to do it manually, then simply navigate to your project directory for which you want o to install some packages using the composer and create a composer.json file

sudo nano composer.json

Add the following lines:

"require": {
"silex/silex": "~2.0"

Save it.

Now within the directory where you have created the above file run

composer install

This will install the Silex for your application or project.

Update components and installations

Whenever you change your composer.json, run composer as with the component installation, but not with the install addition but with the update.  

composer update

Uninstall Composer on Ubuntu or Linux

To remove it we simply have to delete the composer.phar file where ever we have put it. In our case it under /usr/local/bin.

Thus, simply use the below command

cd /usr/local/bin
rm composer
uninstall composer ubuntu
uninstall composer ubuntu