Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Install Composer on Ubuntu 22.04

Install PHP, then Composer, the dependency manager for PHP, as a global command.

VPS and Cloud Servers2 min read7 screenshots

Requirements

  • Ubuntu 22.04.
  • PHP. Check if it is installed:
php --version
Checking the PHP version

If it is not, install PHP and the extensions Composer needs:

sudo apt update
sudo apt install php php-cli php-mbstring php-xml php-curl unzip -y
Installing PHP

Check again:

php --version
The PHP version after install

Step 1: Download the installer

curl -sS https://getcomposer.org/installer -o composer-setup.php
Downloading the Composer installer

For a secure install, check the hash of the file against the one published on the Composer website before you run it.

Step 2: Run the installer

php composer-setup.php
Running the installer

This creates the file composer.phar in the current folder.

Step 3: Move Composer to a global location

sudo mv composer.phar /usr/local/bin/composer
Moving Composer

Remove the installer file:

rm composer-setup.php

Step 4: Check the installation

composer --version
The Composer version

Use Composer

Create a project or add a package:

composer init
composer require monolog/monolog

Install the packages of an existing project:

composer install

Update Composer itself:

sudo composer self-update

Benefits

  • Manages the libraries of your PHP project and their versions.
  • Autoloads classes, so you do not need many require lines.
  • Makes projects easy to share and repeat.

Projects that use Composer

  • Laravel, a popular PHP framework.
  • Symfony, a high-performance framework.
  • CodeIgniter 4, a light and friendly framework.
  • WordPress tools, such as WP-CLI and Bedrock.
Tip: Do not run Composer as root for your projects. Use your normal user.

Quick recap

  • Install PHP, download the installer and run it.
  • Move composer.phar to /usr/local/bin/composer.
  • Check with composer --version.