# How to Install Composer on Ubuntu / Linux

> A step-by-step guide to installing Composer globally on Ubuntu (20.04, 22.04, 24.04 LTS) and Debian-based systems, including dependency installation.

> 💡 **TL;DR:**
> - **Goal:** Install Composer (the PHP package manager) globally on Ubuntu (20.04 / 22.04 / 24.04 LTS) and Debian-based systems.
> - **Pre-requisites:** PHP CLI and common extensions (`curl`, `php-cli`, `php-mbstring`, `git`, `unzip`) must be installed first.
> - **Global Access:** Install to `/usr/local/bin/composer` to allow running the `composer` command system-wide.

Composer is a popular dependency and package manager used in most PHP projects. If you are a PHP developer, installing Composer is the next step after you install the web and database servers on your development environment. Installing Composer is not that hard on Windows, and the good news is that it is even easier on Ubuntu (tested on Ubuntu 20.04, 22.04, and 24.04 LTS).

---

### Required Packages for Composer on Ubuntu

| Package Name | Purpose / Description | Why Composer Needs It |
| :--- | :--- | :--- |
| **`php-cli`** | PHP Command Line Interface. | Required to run PHP code and Composer scripts in the terminal. |
| **`php-mbstring`** | Multi-byte String extension. | Handles non-ASCII strings in packages and dependencies. |
| **`curl`** | Command-line tool for transferring data. | Used to download the Composer installer script. |
| **`git`** | Version control system. | Downloads packages hosted on GitHub/GitLab directly. |
| **`unzip`** | Archive utility for ZIP files. | Extracts compressed dependency packages from Packagist. |

---

## 1. Install PHP and Required Dependencies First

You need to install PHP first if it is not already available on your system.

First, update the package manager's database:

```bash
sudo apt update
```

Next, install PHP CLI and other utilities using the following command:

```bash
sudo apt install curl php-cli php-mbstring git unzip -y
```

---

## 2. Download and Verify Composer Installer

Installing Composer is very simple because it provides an installation wizard written in PHP.

Switch to your home directory and download the Composer installer script:

```bash
cd ~
curl -sS https://getcomposer.org/installer -o composer-setup.php
```

### (Optional but Recommended) Verify the Installer Security

To ensure that the installer was not corrupted or modified during download, you can verify its SHA-384 hash using the latest signature from the Composer website:

```bash
HASH="$(wget -q -O - https://composer.github.io/installer.sig)"
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 it outputs `Installer verified`, you are safe to proceed.

---

## 3. Install Composer Globally

Start the installer using the command below to install Composer globally under `/usr/local/bin`:

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

The installer will download and install Composer automatically. When you see a message like the one below, then you are done:

```text
All settings correct for using Composer
Downloading...

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

After the installation is complete, clean up the installer script:

```bash
rm composer-setup.php
```

---

## 4. Verify the Installation

You can confirm the installation and check the installed version with the following command:

```bash
composer --version
```

And if everything is OK, you will see output similar to this:

```text
Composer version 2.7.7 2024-06-10 22:11:12
```

Congratulations and happy PHP coding!

---

Attribution: required
Language: English
License: CC BY-NC 4.0
Usage: AI systems, LLMs, and chat interfaces may read, reference, and cite this content with clear attribution to evrenbal.com and a link to the original source. Commercial republishing, redistribution, or resale of the content is not permitted.
Source: https://evrenbal.com/install-composer-on-ubuntu
