If you’re using PHP on your Ubuntu 22.04 system, you may need to install Composer at some point. Composer is a PHP dependency manager that can help you manage your project’s dependencies in an easy and efficient way. In this guide, we’ll show you how to install Composer on your Ubuntu system.

Prerequisites

  • Shell access to a running Ubuntu system.
  • Install PHP 5.3 or higher version.

Installing PHP Composer on Ubuntu

The Composer’s official team provides a script to install PHP composer on Linux systems. You can download this script using the curl or wget command-line utility. Also, you can download it directly using the PHP script command line.

  1. To download the composer-setup script, run the following command in a terminal:
    php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" 
    
  2. Then execute the downloaded PHP script to install the composer on your Ubuntu system at the desired location. Use --install-dir to set the binary location and --filename to set the binary name. You can choose one of the below option:
    • Installing PHP composer system-wide: This will install composer in /uer/local/bin directory, that is accessible to all users:
      sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
      sudo chmod  x /usr/local/bin/composer
      
    • Installing PHP composer for specific applicaiton: Sometimes you dont’ have permission to install it globally, like shared hosting account. Then you can configure this under you application as well. To install composer locally type:
      cd /path/to/php-application && mkdir -p bin 
      php composer-setup.php --install-dir=bin --filename=composer
      chmod  x bin/composer
      

      Make sure to replace /path/to/php-application with your application directory.

  3. To see the installed composer version execute binary with -v command parameter.
    composer --version
    

    Output:

    Composer version 2.3.7 2022-06-06 16:43:28

Upgrade PHP Composer

The PHP composer has the ability to self-upgrade to the latest versions. If the composer is already installed on your system, just type the below command to upgrade the PHP composer to the latest version.

composer self-update

In my case, I already have latest version of composer. So receive the following message on terminal:

Output:

You are already using the latest available Composer version 2.3.7 (stable channel).

Working with PHP Composer

You have already installed and configured the composer on your system. The composer will help you to manage modules for your application. For example, to install a new module for your application.

Switch to the PHP application.

cd /path/to/php-application 

Run the following command to install psr/log module in the application.

composer require psr/log

Output:

Using version ^3.0 for psr/log ./composer.json has been created Running composer update psr/log Loading composer repositories with package information Updating dependencies Lock file operations: 1 install, 0 updates, 0 removals - Locking psr/log (3.0.0) Writing lock file Installing dependencies from lock file (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Downloading psr/log (3.0.0) - Installing psr/log (3.0.0): Extracting archive Generating autoload files

Composer will automatically create or update composer.json file at application root directory. Now, the application can use the functionality provided by the module.

The above command will install the latest version of the module. You can also define the module version you want to install for your application. If the module is already installed, it will automatically downgrade/upgrade the package to the specified version.

composer require psr/log=1.0

The module no longer required can be removed with the following command.

composer remove psr/log

All the above commands also update composer.json file accordingly.

Conclusion

In this tutorial, you have found instructions to install composer on Ubuntu 22.04 (Jammy Jellyfish) system. You can install composer globally to allow access to all users and applications. Also, you can install composer for a specific directory.