Home  »  ArticlesGuidesHow ToTechnologyTools   »   How to Install PHP Composer on Ubuntu 20.04

How to Install PHP Composer on Ubuntu 20.04

This quick guide will show you how you can install PHP Composer on Ubuntu 20.04 | 18.04 and Debian based Linux systems.

The PHP Composer is an application-level dependency management tool for installing and managing libraries that a PHP application requires. After that, you can easily use these modules in your project. With this package manager, users can also install and continue to maintain the specific required version of libraries for a particular application.

Before You Install PHP Composer

You will need shell access to your Ubuntu system with sudo privileges.
PHP version 5.3 or higher must also be installed and configured.
curl and unzip must also be installed on your system

Step 1: Install PHP (If you have not already installed it)

If you don’t have PHP installed on your system execute the commands below to update apt-cache and then install PHP on your system.

$ sudo apt update
$ sudo apt install php php-gd php-xml php-cli php-zip

Execute the below commands on the terminal to install unzip and curl utilities.

$ sudo apt install unzip curl

Once the above commands have run, verify the active PHP command-line version by executing:

$ php -v

You should get similar output to this:

Output
PHP 7.4.13 (cli) (built: Nov 28 2020 06:24:43) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.13, Copyright (c), by Zend Technologies

Step 2: Install PHP Composer

Download the composer executable file on your system using the following command.

$ curl -sS https://getcomposer.org/installer | php

Use the following commands to make composer available globally for all users in your system. That way it can be used for all PHP applications on that system.

$ sudo mv composer.phar /usr/local/bin/composer
$ chmod +x /usr/local/bin/composer

After installation is complete, you can run the following command to verify the composer version details along with options available with the Composer command.

$ composer -v

You should get output that looks something like this:

Output
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.6.4 2018-04-13 12:04:24

Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  about                Shows the short information about Composer.
  archive              Creates an archive of this composer package.
  browse               Opens the package's repository URL or homepage in your browser.
  check-platform-reqs  Check that platform requirements are satisfied.
  clear-cache          Clears composer's internal package cache.
  clearcache           Clears composer's internal package cache.
  config               Sets config options.
  create-project       Creates new project from a package into given directory.
  depends              Shows which packages cause the given package to be installed.
  diagnose             Diagnoses the system to identify common errors.
  dump-autoload        Dumps the autoloader.
  dumpautoload         Dumps the autoloader.
  exec                 Executes a vendored binary/script.
  global               Allows running commands in the global composer dir ($COMPOSER_HOME).
  help                 Displays help for a command
  home                 Opens the package's repository URL or homepage in your browser.
  info                 Shows information about packages.
  init                 Creates a basic composer.json file in current directory.
  install              Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
  licenses             Shows information about licenses of dependencies.
  list                 Lists commands
  outdated             Shows a list of installed packages that have updates available, including their latest version.
  prohibits            Shows which packages prevent the given package from being installed.
  remove               Removes a package from the require or require-dev.
  require              Adds required packages to your composer.json and installs them.
  run-script           Runs the scripts defined in composer.json.
  search               Searches for packages.
  self-update          Updates composer.phar to the latest version.
  selfupdate           Updates composer.phar to the latest version.
  show                 Shows information about packages.
  status               Shows a list of locally modified packages, for packages installed from source.
  suggests             Shows package suggestions.
  update               Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  upgrade              Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  validate             Validates a composer.json and composer.lock.
  why                  Shows which packages cause the given package to be installed.
  why-not              Shows which packages prevent the given package from being installed.

Step 3: How to use Composer

For Existing Project: Switch to the application root directory and run the below command. It will read composer.json file and install dependencies for the application.

$ composer install

For New Projects: To create a new project, switch to the project directory, and install required modules as below command.

$ mkdir mynewproject && cd mynewproject
$ composer require psr/log

The above command will install psr/log module under the vendor directory. It also creates an entry in composer.json and updates composer.lock as well.

Bonus Step: Upgrading Composer

Composer is capable of upgrading itself by running the self-update command to upgrade Composer to the latest version. You can also download the latest version of the composer by executing the same commands above used for installation and it will upgrade Composer.

$ sudo composer self-update

Conclusion

This guide has shown you how to install PHP Composer. It goes further to show you how to use it as well as upgrade it using the command-line interface.

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.

Available under:
Articles, Guides, How To, Technology, Tools