How To Install and Use PHP Composer on Ubuntu

How To Install and Use PHP Composer on Ubuntu


PHP Composer is an application that tracks dependencies for a project. It does not replace system package managers like apt or yum.

Use Composer to specify a set of libraries for a specific project. The application automatically identifies the versions and dependencies to install.

This guide shows how to install and get started with PHP Composer in Ubuntu.

How To Install and Use PHP Composer on Ubuntu


Steps For Installing PHP Composer on Ubuntu

To install PHP Composer on Ubuntu, confirm PHP is available on the system:

php -v

Continue to the steps below to install Composer on Ubuntu.

Step 1: Update Local Repository

Update the local repository lists by entering the following in a command line:

sudo apt-get update

Wait for the update to complete, and continue to the next step.

Step 2: Download the Composer Installer

To download the Composer installer, use the following command:

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

The command copies the installer to the machine.

Step 3: Verify Integrity of the Download

To verify the download, do the following:

1. Visit the Composer Public Keys page and copy the Installer Checksum (SHA-384).

2. Set the code shell variable and paste the key from the previous step:


3. Run the script below to compare the official hash against the one you downloaded:

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

The script will either tell you the download is verified, or that it has been corrupted. If corrupted, the code removes the installer. Redownload the file and try again.

Step 4: Install PHP Composer

1. Installing PHP Composer requires curl, unzip, and a few other utilities. Install them by entering the following:

sudo apt-get install curl php-cli php-mbstring git unzip
install supporting software for composer

2. Install Composer as a command accessible from the whole system. To install to /usr/local/bin enter:

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

The installer outputs the following:

All settings correct for using Composer

Composer (version <version>) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

3. Once the installer finishes, verify the installation:

composer --version

The system outputs the version number installed, for example:

Composer version <version>

4. To remove the installer file, run the following:

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

Basic Composer Usage

Composer tracks dependencies on a per-project basis. This makes it easier for other users to create the same environment. Composer uses a composer.json file to keep track of required software and allowed versions.

It also uses a composer.lock file to maintain consistency if someone copies the directory. These files generate automatically after using the require command.

1. Open a terminal, and enter the following:

mkdir c_sample && cd c_sample

2. Next, choose a package to load. The website has a broad range of different PHP packages for download. In this example, we use the monolog/monolog package. Follow the instructions in the next step, or search the website for a different package.

3. In the terminal window, enter:

composer require monolog/monolog

The system downloads the software and creates the composer.json and composer.lock files.

Note: Monolog is a package for managing logfiles. The name before the slash is the vendor, and the name after the slash is the package name.

4. Once the process completes, list the contents of the directory:

ls -l

The list shows composer.json and composer.lock files, along with a vendor directory.

5. To view the contents of the composer.json file:

cat composer.json

The system adds the monolog software. The carat ^ sign beside the version number indicates the minimum version of the software.

Setting Up Autoloading

PHP doesn’t automatically load classes. Configure Composer to autoload classes to make working with dependencies much easier.

1. Create a new file using your favorite text editor:

sudo nano composer_sample.php

2. Enter the following into the file:


require __DIR__ . '/vendor/autoload.php';
use MonologLogger;
use MonologHandlerStreamHandler;

// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('~/c_sample/text.log', Logger::WARNING));

// add records to the log
PHP autoloading classes example

3. Write the file (Ctrl+O), and exit (Ctrl+X).

4. Run the command to autoload monolog:

php composer_sample.php

The result shows no output.

Updating Dependencies

To update all the dependencies in the composer.json file enter the command:

composer update

This updates all dependencies according to the version in the file.

To update one (or more) dependencies individually, use the following:

composer update vendor/package vendor_b/package_b


Now you have a good understanding of how to install and start using PHP Composer. We also covered setting up autoloading and updating dependencies.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !