Cybersecurity Updates & Tools

Install PHP Composer on Ubuntu 18.04: Setup and Getting Started

Composer is a dependency manager for PHP. It works similarly to npm for Node.js or pip for Python; you declare the packages your project needs and Composer downloads, installs, and manages them automatically. It also handles autoloading, so you can use any installed library in your PHP code without writing a single require statement by hand.

This guide shows you how to install Composer on Ubuntu 18.04 using the verified installation method, then walks you through creating a real PHP project with a third-party dependency.

<strong>Prerequisites:</strong>&nbsp;You need sudo access and PHP installed. If PHP CLI is not already on your system, install it with&nbsp;<code>sudo apt install php-cli</code>.

Install Composer on Ubuntu

Update the package index and install the required tools:

bashsudo apt updatesudo apt install wget php-cli php-zip unzip

Download the Composer installer script to your current directory:

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

Before running the installer, verify its integrity using a SHA-384 hash. This step protects you from running a corrupted or tampered download. Fetch the expected hash from Composer’s GitHub page and store it in a variable:

bashHASH="$(wget -q -O - https://composer.github.io/installer.sig)"

Compare the hash against the downloaded file:

bashphp -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 output says Installer verified, continue. If it says Installer corrupt, re-download the file and double-check the value of $HASH with echo $HASH before trying again.

Install Composer system-wide so it is available for all users:

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

Verify the installation:

bashcomposer

This prints the Composer version, available commands, and arguments.

Quick Install (Skip Verification)

If you do not need to verify the installer hash, Composer can be installed in two commands:

bashcurl -sS https://getcomposer.org/installer | phpsudo mv composer.phar /usr/local/bin/composer

Create Your First PHP Project with Composer

Create a project directory and navigate into it:

bashmkdir ~/my-first-composer-projectcd ~/my-first-composer-project

Use composer require to add a package. This example installs Carbon, a popular PHP library for handling dates and times:

bashcomposer require nesbot/carbon

Composer downloads the package along with all its own dependencies and creates three items in your project directory:

  • composer.json – describes the project and lists its declared dependencies
  • composer.lock – records the exact installed version of every package, including nested ones
  • vendor/ – the directory where all downloaded packages are stored

The composer.lock file is important for teams. Commit it to version control and every developer or CI server that runs composer install will get identical package versions — no version drift, no inconsistent environments. The vendor/ directory should be added to .gitignore since it can always be regenerated from the lock file.

Composer also generates vendor/autoload.php. Include it once at the top of your scripts and every installed package is available immediately:

phprequire __DIR__ . '/vendor/autoload.php';use Carbon\Carbon;printf("Now: %s", Carbon::now());

To update all packages to the latest compatible versions allowed by your composer.json:

bashcomposer update

Composer is now installed on your Ubuntu 18.04 system. The hash verification, version locking, and autoloading features make it an essential part of any modern PHP project workflow. Leave a comment below if you run into any issues during setup.