Composer is a powerful dependency manager for PHP that simplifies the process of managing libraries and packages in your projects. This guide will walk you through the steps for installing and using Composer on Ubuntu 18.04.
Step 1: Update the System
Before installing Composer, it’s a good practice to update your system packages to ensure everything is up-to-date. Run the following commands:
sudo apt update && sudo apt upgrade
This will refresh the package lists and install any available updates.
Step 2: Install Required Dependencies
Composer needs several packages and tools to function properly. These include curl
, php-cli
(the PHP command-line interface), php-mbstring
(to handle string functions in PHP), and unzip
. Install them with the following command:
sudo apt install curl php-cli php-mbstring unzip
These are essential for downloading and running Composer’s installer script.
Step 3: Download and Install Composer
With the dependencies installed, you can now download and install Composer. To do this, you’ll first download Composer’s installer using curl
:
curl -sS https://getcomposer.org/installer -o composer-setup.php
Next, verify the installer’s integrity by comparing its hash to the official signature. Run the following command to fetch the hash value:
HASH=$(curl -sS https://composer.github.io/installer.sig)
Then verify the downloaded file against the hash:
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 the installer is verified, you will see the message “Installer verified.” If not, the script will delete the corrupt installer.
Once verified, you can proceed with the installation by running:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
This installs Composer globally so it can be used from anywhere on your system. You can verify the installation by checking the Composer version:
composer --version
Step 4: Using Composer
Now that Composer is installed, let’s explore how to use it in a project.
Creating a New Project
- Create a Directory: Navigate to your project’s directory or create a new one:
mkdir my_project && cd my_project
- Initialize Composer: To start using Composer in this project, run the following command to create a
composer.json
file:
composer init
Composer will ask you several questions, such as the project’s name, description, and author. It will also let you define dependencies for the project.
Installing Dependencies
Composer allows you to easily install packages from Packagist, a repository for PHP packages. To install a package, such as monolog/monolog
(a logging library), you can run:
composer require monolog/monolog
This will automatically download and install the package in a vendor/
directory and update your composer.json
file to reflect the dependency.
Updating Dependencies
If you ever need to update the installed packages, simply run:
composer update
This will update all packages according to the version constraints specified in your composer.json
file.
Removing Dependencies
To remove a package from your project, use the remove
command:
composer remove vendor/package_name
This will delete the package and update the composer.json
and composer.lock
files accordingly.
Step 5: Auto-loading with Composer
One of Composer’s most useful features is its ability to autoload classes from the installed packages. To set up autoloading, include the following line in your PHP script:
require 'vendor/autoload.php';
This will allow you to use the installed packages without having to manually include them in your files.
Step 6: Global Composer Commands
Composer also supports global installation of packages that can be used system-wide. For example, to install phpunit
globally, you would run:
composer global require phpunit/phpunit
Ensure that the ~/.composer/vendor/bin
directory is in your system’s PATH
, so globally installed packages can be executed from any location.
Conclusion
You have now installed and configured Composer on your Ubuntu 18.04 server, and you’ve learned the basics of using Composer to manage dependencies in your PHP projects. Composer simplifies the process of handling libraries and ensures your projects stay up-to-date and maintainable. For more advanced usage, refer to Composer’s official documentation.