How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Install Composer on Ubuntu: Step-by-Step Guide
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Install Composer on Ubuntu: Step-by-Step Guide
Web Development

How to Install Composer on Ubuntu: Step-by-Step Guide

how7o
By how7o
Last updated: April 16, 2026
8 Min Read
Install Composer on Ubuntu — terminal with composer-setup.php and PHP elephant icon
SHARE

If you’re about to install Composer on Ubuntu, this guide walks through the exact steps I run on a fresh Ubuntu 22.04 or 24.04 box: installing the PHP CLI and extensions Composer needs, downloading the official installer, verifying its signature against getcomposer.org’s live hash, and placing composer as a system-wide command in /usr/local/bin. By the end you’ll have a working Composer install that can bootstrap Laravel, Symfony, or any PHP project on the same machine.

Contents
  • TL;DR
  • Prerequisites
  • Step 1 — Install PHP and required extensions
  • Step 2 — Download the Composer installer
  • Step 3 — Verify the installer signature
  • Step 4 — Run the installer system-wide
  • Step 5 — Clean up and verify
  • Troubleshooting
    • “Installer corrupt” on a freshly downloaded installer
    • “command not found: composer” after install
    • PHP extension errors like “ext-mbstring missing”
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-04-16 on Ubuntu 22.04 LTS with PHP 8.1 from the default repos. Originally published 2023-02-24, rewritten and updated 2026-04-16.

TL;DR

Install PHP and the extensions Composer needs (cli, mbstring, xml, curl, zip), download the installer with php -r "copy(...)", verify it against the hash at https://composer.github.io/installer.sig, then run php composer-setup.php --install-dir=/usr/local/bin --filename=composer. Don’t hard-code the SHA-384 hash — fetch it live every time, because the Composer team rotates it with every installer release.

Prerequisites

  • An Ubuntu 22.04 or 24.04 system (22.04.2 was the tested baseline; 20.04 works with the same commands).
  • A user with sudo privileges.
  • Internet access to getcomposer.org and composer.github.io.

Step 1 — Install PHP and required extensions

Composer is a PHP script, so PHP itself plus a handful of extensions must be in place first. Skip this section if you already have PHP installed.

Open a terminal and refresh the package index:

sudo apt update

Install PHP 8.1 along with the extensions Composer and most modern PHP apps depend on:

sudo apt install -y php8.1-cli php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1-mbstring php8.1-curl php8.1-xml php8.1-bcmath unzip curl

On Ubuntu 24.04 the default PHP is 8.3 — swap php8.1-* for php8.3-* above. The extension names are the ones Composer strictly needs (mbstring, xml, curl, zip) plus the MySQL and GD extras most application projects pull in later, so you don’t have to re-apt later in the project.

Step 2 — Download the Composer installer

Composer’s official installer is a PHP script hosted at https://getcomposer.org/installer. Pull it into your current working directory:

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

You now have a file named composer-setup.php in the current directory. Don’t run it yet — verify it first.

Step 3 — Verify the installer signature

The Composer team publishes the SHA-384 hash of the current installer at https://composer.github.io/installer.sig. Fetch the hash live — don’t copy one from a blog post, including this one. The hash changes with every installer release, and an out-of-date hash will always report “Installer corrupt” even when your download is fine.

HASH="$(curl -sS https://composer.github.io/installer.sig)"
php -r "if (hash_file('sha384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'.PHP_EOL; } else { echo 'Installer corrupt'.PHP_EOL; unlink('composer-setup.php'); }"

If the verification succeeds you’ll see:

Installer verified

If you see Installer corrupt, the script deletes the download automatically — re-run Step 2 and try again. If it fails a second time, skip ahead to the Troubleshooting section.

install composer on ubuntu — install php, download installer, verify hash, run setup flow

Step 4 — Run the installer system-wide

Install Composer as a system-wide command called composer, living in /usr/local/bin so every user on the machine can run it:

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

The --install-dir flag controls where the final binary lands, and --filename=composer names it so you don’t have to type composer.phar forever. sudo is required because /usr/local/bin is root-owned.

Step 5 — Clean up and verify

Delete the installer script now that Composer itself is installed:

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

Confirm Composer is on your PATH and runs:

composer --version

You should see something like Composer version 2.x.x YYYY-MM-DD. Running composer with no arguments prints the full command list — the same “screen similar to this” the original guide showed.

Troubleshooting

“Installer corrupt” on a freshly downloaded installer

Nine times out of ten this means the SHA-384 hash you’re comparing against is stale. Use the live-fetch command in Step 3 rather than pasting a hash from another guide. If the live fetch also fails, check that curl can reach composer.github.io from your server — captive portals, corporate proxies, and firewalled VPS images occasionally block it.

“command not found: composer” after install

/usr/local/bin is on PATH for interactive shells by default on Ubuntu, but not always for non-interactive ones (cron, some CI runners). If you hit this in an automated script, invoke Composer by full path: /usr/local/bin/composer.

PHP extension errors like “ext-mbstring missing”

Re-run the apt install from Step 1 — every extension on that line is a regular Composer requirement. If you’re on a non-default PHP version, make sure the extension package names match it (e.g. php8.3-mbstring for PHP 8.3).

Frequently asked questions

Why fetch the Composer installer hash live instead of hard-coding it?

The Composer team rotates the installer and publishes a new SHA-384 at https://composer.github.io/installer.sig with each release. A hard-coded hash starts failing as soon as the installer is rebuilt, producing misleading “Installer corrupt” errors on perfectly clean downloads.

Should I install Composer with apt instead?

Ubuntu’s composer apt package is almost always behind upstream. The official installer gives you the current 2.x release and lets Composer self-update in place, which Laravel and most framework docs assume.

What’s the difference between global and project-local Composer?

Following this guide gives you a global composer binary in /usr/local/bin. Project dependencies it installs live inside each project’s vendor/ directory and are defined by that project’s composer.json. The Composer binary is shared; project dependencies are not.

How do I upgrade Composer later?

Run sudo composer self-update. Composer downloads the current stable release, verifies it, and atomically replaces the binary at /usr/local/bin/composer. Use sudo composer self-update --rollback to revert.

Do I need to install Composer as root?

Only the Step 4 install command needs sudo, because it’s writing into /usr/local/bin. The download, verify, and cleanup steps all run as your normal user. After install, run composer as your normal user — never as root inside a project.

Related guides

  • How to Install MySQL on Ubuntu 22.04 — the database half of a typical LAMP stack, same server.

References

Official Composer download and signature page: getcomposer.org/download. Ubuntu server documentation: ubuntu.com/server/docs.

TAGGED:BashcomposerDependency ManagementinstallationphptroubleshootingUbuntu

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Install MySQL on Ubuntu 22.04 — terminal with apt command and database cylinder icon How to Install MySQL on Ubuntu 22.04: Step-by-Step Guide
Next Article Install PHP on Ubuntu — terminal with apt install php command and stylized elephant icon How to Install PHP on Ubuntu (22.04 & 24.04): Step-by-Step Guide
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026
Caddy server — automatic HTTPS, 3-line Caddyfile vs 25-line nginx config
How to Install Caddy Server on Ubuntu (Automatic HTTPS, Drop-in nginx Alternative)
May 24, 2026
Cloudflare Tunnel — outbound-only connection from server, no inbound port forward
How to Install Cloudflare Tunnel on Ubuntu (Expose Local Services, No Port Forwarding)
May 24, 2026
WireGuard encrypted tunnel between server and clients with lock icons
How to Set Up WireGuard VPN on Ubuntu (Server, Linux Client, and iOS)
May 24, 2026

You Might Also Like

Change SSH port on Linux — firewall, SELinux, sshd_config, systemctl restart
Server Management

How to Change the Default SSH Port on Linux

7 Min Read
Create custom exception class in Laravel (Artisan command + secure error handling)
Web Development

How to Create a Custom Exception Class in Laravel (With Clean JSON Responses)

6 Min Read
WordPress too many redirects HTTPS — Cloudflare flexible SSL loop and the wp-config fix
Web Development

Fix ERR_TOO_MANY_REDIRECTS in WordPress After Switching to HTTPS

7 Min Read
Debug PHP like console.log using error_log and server logs
Web Development

How to Debug in PHP Like console.log (echo, error_log, WordPress debug.log)

6 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?