How To

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.

Cyber Defence

Recent Posts

Install PHP on Ubuntu 26.04: Apache, Nginx, and Multiple Versions

PHP 8.5 is included in Ubuntu 26.04's default repositories and is the recommended version for…

8 hours ago

Upgrade to Ubuntu 26.04 from 25.10 and 24.04 LTS: Complete Guide

Ubuntu 26.04 LTS "Resolute Raccoon" arrived on April 23, 2026 with Linux kernel 7.0, GNOME 50,…

8 hours ago

Install Kubernetes on Ubuntu 26.04 with kubeadm and containerd

Kubernetes is the standard platform for running containerized workloads across multiple servers with self-healing, rolling…

8 hours ago

Install Ubuntu 26.04: Bootable USB, Partitioning, and First Steps

Ubuntu 26.04 LTS "Resolute Raccoon" was released on April 23, 2026 with Linux kernel 7.0, GNOME desktop, and standard security support until April 2031. A clean install gives you a known-good starting point on new hardware, when replacing another operating system, or when an upgrade path is not practical. This guide walks through how to install Ubuntu 26.04: downloading and verifying the ISO, writing a bootable USB drive, completing the installer, and doing the initial setup after the first boot. Before you start: You need a USB drive with at least 12 GB of free space. Back up any existing data on the target machine — the installer can erase the entire disk. Install Ubuntu 26.04: Download the ISO…

8 hours ago

Change Timezone on Ubuntu: timedatectl and Desktop GUI Guide

The correct timezone affects more than the clock on your screen. It drives cron job scheduling, systemd timer execution, log file timestamps, database record timing, and SSL certificate validity checks. A mismatched timezone can cause scheduled jobs to fire at the wrong hour and make log timestamps impossible to match with real-world events. This guide shows how to change timezone on Ubuntu using the timedatectl command (the recommended approach for servers and remote machines) and through the graphical Date & Time settings on desktop systems. The steps apply to all current Ubuntu releases including 24.04 and 26.04. <strong>Prerequisite:</strong>&nbsp;Only&nbsp;the&nbsp;root&nbsp;user&nbsp;or&nbsp;a&nbsp;user&nbsp;with&nbsp;sudo&nbsp;access&nbsp;can&nbsp;change&nbsp;the&nbsp;system&nbsp;timezone. Check the Current Timezone Before You Change It Run timedatectl with no arguments to see the active timezone and clock status: bashtimedatectl Sample output: Local…

9 hours ago

Install Atom on Ubuntu 18.04: GitHub’s Code Editor APT Setup

Atom is a free, open-source, cross-platform code editor developed by GitHub. Built on Electron, it uses…

20 hours ago