Don’t like the intro? Skip to the Tutorial (install Composer on Linux) here.

Introduction

Back then with PHP development, you would want to use other PHP libraries to include into your project. The process is usually pretty time-consuming:

  • First, download the zipped PHP library file;
  • Then extract it into the project directory;
  • Use require_once function to include the library to the project;
  • Upload the whole project with the library files when deploying it online;

Not to mention that, when a library gets an update, you have to re-download it, and overwrite the files again, which is very unproductive.

Composer, a popular PHP dependency manager, helps solving these issues real easy and effective.

What is Composer and what does it do?

As the above short brief on what Composer can do, it is a PHP dependency manager. As a dependency tool:

  • Composer helps install, include, update PHP dependency libraries in a project;
  • Composer can declare dependencies in a PHP package/project easily;

Not only can Composer can handle a project’s dependency on external libraries, it can handle libraries depend on other libraries as well, which is extremely helpful.

As Official Composer Introduction page, Composer can:

Enables you to declare the libraries you depend on. Finds out which versions of which packages can and need to be installed, and installs them (meaning it downloads them into your project). You can update all your dependencies in one command.

In short, Composer is a must-use tool if you plan to develop with PHP.

Composer supports Windows, Linux and MacOS. This in-depth article will guide you on how to install Composer on Linux-based operating systems.

Install Composer on Linux – Step by step

1. System requirements for Composer on Linux

Composer has general system requirements on Linux-based operating systems.

  • You must have at least PHP 7.2.5 installed
  • php.ini settings should be properly set. If you install PHP using general methods as dnf, yum, apt-get, it should work fine.

It is possible to use old Composer versions for legacy PHP versions, but at least it has to be PHP 5.3.2. Although so, it is recommended to use a newer PHP version for better compatibilities, as not many PHP libraries use older PHP versions anymore.

2. Install Composer on Linux-based OS

Quick Solution: From the Official Composer Download page, you just need to run these commands (either line-by-line or all at once works) in Linux terminal:

SHELL

*These commands will be updated as Composer updates, however, it’s better to get the latest commands from, again, the Official Composer Download page, as they might be updated making the hashes on this page obsolete.

The page also explains how these command works in details, so we will just quote them here:

This installer script will simply check some php.ini settings, warn you if they are set incorrectly, and then download the latest composer.phar in the current directory. The 4 lines above will, in order: Download the installer to the current directory Verify the installer SHA-384, which you can also cross-check here Run the installer Remove the installer

After finished running the commands, copy the composer.phar into your Linux’s $PATH directory, so it can be called globally:

SHELL

Quick tips: To view your $PATH directory, run this command:

PHP

The system will return a number of global paths that you can copy the composer.phar file to. For example:

SHELL

After successfully copying the composer.phar file, the installation can be considered completed. We will verify if Composer works in the next step.

4. Verify Composer installation

To check if Composer is probably installed, open Linux terminal and run this command:

SHELL

Alternatively, composer -V (V in capitalized) can be used. If you see the similar message telling Composer version, it has been successfully installed:

SHELL

If the command returns “not found” error, check if you have copied the composer.phar to the correct $PATH directory on your Linux system. See the Quick tips on the previous section on how to locate it.

5. Update Composer on Linux

To update Composer on Linux, you can follow the above steps again to download the latest Composer version and replace it with your current version.

Example Usage

This quick example will show you on how to use Composer.

Let’s say we have a PHP project that needs URL slug creation functionality, which turns words into a slug URL for SEO purposes (eg. an example project to an-example-project). Instead of having to write this functionality from scratch, we will use a great PHP package, which is ausi/slug-generator library by GitHub user ausi.

On the root project folder, enter this command in Linux terminal:

SHELL

This command will tell Composer to:

  • Download the PHP package ausi/slug-generator located on Packagist.org (main Composer package repository)
  • Copy it into our project directory under the vendor folder,
  • Record dependency information into composer.json file
  • Record the package’s version, time and hashes into composer.lock file to keep track of its states
  • Generate necessary dependency inclusion into vendor\autoload.php file

Compared to downloading the package manually and copying it into the project folder, this is very efficient. To use the package, all we need is including the autoload.php file into the project, as this example:

PHP

That’s how Composer works. Extremely convenient.

Final thoughts

The tutorial on how to install Composer on Linux for PHP dependency management is now over. As a PHP developer, using Composer is a must to keep your project clean and maintain good PHP packages dependency.

We will constantly update this article as new information releases. Thanks for reading, and hopefully this tutorial is helpful for your PHP development process.