Composer is the package manager for PHP. If you build anything in PHP, from a small script to a Laravel application, Composer is how you add libraries, manage dependencies, and keep them updated. This guide installs Composer on Ubuntu 24.04 the official way: download the installer, verify its signature, install it globally, and then set up your first PHP project with a real package.

By the end you will have Composer 2.11 available as a single composer command anywhere on your system, a working project with autoloading, and the commands to update and remove packages safely.

Prerequisites

  • Ubuntu 24.04 or 22.04 (a VPS or local machine).
  • A user account with sudo access.
  • An internet connection to download the installer and packages.
  • No PHP knowledge is required, but you should be comfortable running commands in a terminal.

Step 1: Update Ubuntu and install PHP

Composer is a PHP program, so PHP must be installed first. Update your package lists and install PHP with the command-line tools and the extensions most packages expect: mbstring, xml, and zip.

sudo apt update && sudo apt upgrade -y
sudo apt install php-cli php-mbstring php-xml php-zip unzip curl -y

The first command refreshes the package index and applies pending updates. The second installs the PHP command-line interpreter, three extensions that Composer and most libraries require, unzip for extracting packages, and curl for downloads. Confirm PHP works:

php -v

You should see output like PHP 8.3.x (cli) on Ubuntu 24.04. If you see a version number, PHP is ready.

Step 2: Download the Composer installer

The recommended way to install Composer is the official installer script from getcomposer.org. Download it to your home directory:

cd ~
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

This saves the installer as composer-setup.php in your home directory. It is a small PHP script that downloads and installs the latest stable Composer release.

Step 3: Verify the installer signature

Before running anything downloaded from the internet, verify it. Composer publishes a SHA-384 signature for the current installer, and the check below compares the downloaded file against it. As of November 2026 the current signature is:

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

If the output says Installer verified, the file is genuine and safe to run. If it says Installer corrupt, the file is deleted automatically; delete it, re-download it, and check again. The signature changes with every Composer release, so if verification fails after a fresh download, fetch the current signature from https://composer.github.io/installer.sig and use that value instead.

Step 4: Install Composer globally

Run the installer and place the composer executable in /usr/local/bin, which is already on the system PATH. That makes composer available from any directory:

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

The --install-dir flag chooses where the executable lives and --filename names it composer. Now delete the installer script, which you no longer need:

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

Step 5: Verify the installation

Check the installed version and run Composer's built-in diagnostics, which flag common problems such as missing extensions or a misconfigured PHP setup:

composer --version
composer diagnose

You should see something like Composer version 2.11.2 followed by diagnostic output ending in OK checks. If diagnose reports a warning about an extension, install it with apt (for example sudo apt install php-curl) and run the check again.

Step 6: Create your first PHP project

Composer manages dependencies per project. Create a project directory and initialise it. The init command asks a few questions and writes a composer.json file that describes your project:

mkdir -p ~/my-php-app && cd ~/my-php-app
composer init --name="myname/my-php-app" --description="My first Composer project" --author="Your Name <you@example.com>" --type=project --no-interaction

The flags answer the setup questions non-interactively. The result is a composer.json file in the project directory. Open it to confirm:

cat composer.json

You will see your project name, description, and an empty require section. Every package you add later is recorded here, which is what makes a project reproducible on another machine.

Step 7: Install your first package

Install a real package to see the workflow. Monolog is a popular logging library and a good first test:

composer require monolog/monolog

Composer downloads Monolog and its dependencies into a vendor/ directory, records the exact versions in composer.lock, and generates vendor/autoload.php. That autoloader is the key piece: include it once and every installed package's classes become available. Test it with a small script:

cat > test.php <<'EOF'
<?php
require __DIR__ . '/vendor/autoload.php';

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$log = new Logger('demo');
$log->pushHandler(new StreamHandler('php://stdout', Logger::INFO));
$log->info('Composer is working!');
EOF
php test.php

The output should include a line like [2026-11-18T...] demo.INFO: Composer is working!. That confirms the package installed, the autoloader works, and your project is ready for real development.

Step 8: Update and remove packages

Dependencies need maintenance. Update all packages in a project to their latest allowed versions with:

composer update

Remove a package you no longer need with:

composer remove monolog/monolog

And keep Composer itself current. New releases fix bugs and security issues, so update it regularly:

sudo composer self-update

Run self-update with sudo because the executable lives in /usr/local/bin. If you ever need to roll back to the previous Composer release, sudo composer self-update --rollback restores it.

Alternative: install from Ubuntu's repositories. Ubuntu ships Composer in its own packages, so sudo apt install composer also works. It is simpler, but the packaged version lags behind the official release, sometimes by several versions. For PHP projects, the official installer above is the better choice because frameworks such as Laravel and Symfony often require a recent Composer.

Verify it worked

Run through this quick checklist. Every item should pass:

  1. composer --version prints a version number (2.11 or newer).
  2. composer diagnose reports no errors.
  3. Your project directory contains composer.json, composer.lock, and a vendor/ folder.
  4. php test.php prints the Monolog test line.
  5. composer --version works from a different directory, proving the global install is on your PATH.

If all five pass, Composer is fully installed and ready for real projects.

Common issues and fixes

1. "php: command not found"

PHP is not installed, or the install did not complete. Run sudo apt install php-cli -y and then php -v to confirm. On a fresh Ubuntu server this is the most common first error.

2. "Installer corrupt" during signature verification

The download was incomplete or the signature has changed since this guide was written. Delete composer-setup.php, re-download it, and fetch the current signature from https://composer.github.io/installer.sig. Never skip the verification step to work around this.

3. "Your requirements could not be resolved to an installable set of packages"

A package needs a PHP extension that is missing. Read the error message: it names the extension, usually ext-mbstring, ext-xml, or ext-zip. Install it with apt, for example sudo apt install php-mbstring -y, then run composer require again.

4. "Allowed memory size exhausted" when installing packages

Composer can use a lot of memory on large projects. Raise the limit for a single command with php -d memory_limit=-1 /usr/local/bin/composer require vendor/package, or set it permanently for Composer with export COMPOSER_MEMORY_LIMIT=-1 in your shell profile.

5. "Permission denied" writing to /usr/local/bin

You ran the installer without sudo. Re-run Step 4 with sudo. If you do not have sudo access, install Composer locally instead: php composer-setup.php --install-dir=$HOME/.local/bin --filename=composer, then add $HOME/.local/bin to your PATH and use composer from there.

Conclusion

You now have Composer installed globally on Ubuntu, a verified installer, and a working project with autoloading and dependency management. From here the workflow is always the same: composer require to add a library, composer update to refresh versions, and composer install to reproduce the exact same dependencies on any other machine from composer.lock. Keep Composer itself updated with self-update, and commit both composer.json and composer.lock to version control, but never the vendor/ directory.

If you need help setting up PHP, Laravel, or a full LEMP stack on your server, the XpertBees team handles server setup and PHP application deployment for clients every day. Reach out and we will get your stack running.