Posted on
Software

composer: PHP dependency manager

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Mastering Composer: The Essential PHP Dependency Manager on Linux

For any modern PHP developer, managing libraries and packages is a significant task that can be efficiently handled using Composer, a dependency manager. Composer allows you to declare the libraries your project depends on and it will manage (install/update) them for you. In this article, we are going to delve into how to install Composer on a Linux system through different package managers like apt, dnf, and zypper, ensuring that whatever flavor of Linux you are using, you’ll be able to get Composer up and running.

What is Composer?

Composer is a tool for dependency management in PHP. It allows developers to manage their libraries on a project-by-project basis. Composer is not a package manager in the same sense as Yum or Apt are. Rather than installing software on a global level, Composer handles dependencies on a per-project basis, installing them in a directory (e.g., vendor) inside your project. By using Composer, developers can ensure that they are working with the correct versions of the libraries they need and their project demands.

Installing Composer on Linux

Prerequisites

Before installing Composer, you need to have PHP installed on your system. Composer requires PHP 5.3.2 or later.

To check your PHP version, you can run:

php -v

If PHP is not installed, you can install it using the package manager specific to your Linux distribution.

1. Installation on Ubuntu/Debian (Using apt)

For those using distributions based on Debian, such as Ubuntu, you can install Composer through the command line by using apt:

sudo apt update
sudo apt install composer

This command installs Composer globally, allowing you to use it from anywhere in your file system.

2. Installation on Fedora, CentOS, or Red Hat (Using dnf)

If you're on a distribution that uses dnf such as Fedora, you can install Composer by typing:

sudo dnf install composer

This will download and install Composer globally.

3. Installation on openSUSE (Using zypper)

For those who are on an openSUSE system, Composer can be installed using zypper:

sudo zypper install composer

Similar to the above methods, this command installs Composer globally.

Manual Installation (All Systems)

If for some reason you want to install Composer manually or your distribution does not provide an updated package, you can install it manually using the following steps:

  • Download the Composer installer:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === 'HASH_VALUE_HERE') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
  • Run the installer:
php composer-setup.php
  • Move the composer.phar (an executable PHAR archive) to a directory that is in your path:
sudo mv composer.phar /usr/local/bin/composer
  • Verify the installation:
composer --version

Conclusion

By having Composer installed in your development environment, you leverage the power of PHP dependencies management effectively and easily. With Composer, managing libraries and packages in PHP becomes streamlined, allowing you to focus more on development rather than the setup and maintenance of the software. Regardless of your Linux distribution, installing Composer is straightforward and quick, setting up your PHP projects for success.