How to Install and Use Composer on Ubuntu 20.04

how to install and use composer on ubuntu 22.04

Composer is a dependency management tool in PHP; unlike ‘apt’ and ‘yum’, it’s not a package manager. Because it handles the dependencies for PHP projects, per project basis, you can declare, manage and install dependencies with Composer on any PHP project running PHP version 7.2.5 or above.

It’s used in Magento to manage its components and dependencies. The composer uses a composer.json file which specifies version and dependency information. In this tutorial, you are going to install PHP 7.4 and Composer on Ubuntu 20.04 and try out Composer commands. Let’s get started.

Prerequisites

    • A server with Ubuntu 20.04 as OS
    • User privileges: root or non-root user with sudo privileges

Before you start, make sure to update apt-cache with:

$ sudo apt-get update

Now, go ahead and install PHP. If you don’t already have it installed with the command:

 $ sudo apt install php7.4

Now you can install php-cli and unzip also.

 $ sudo apt install php-cli unzip

Download and Install Composer

Composer is installed with a composer-setup.php script, which we will first download in /tmp, verify it and then run the script to install Composer.

First, run the command to download the script in /tmp/composer-setup.php:

 $ curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php

Next, we will obtain the Composer signature and save it in the HASH system variable with:

 HASH=`curl -sS https://composer.github.io/installer.sig`

Now, to verify if the installation script is safe to run, execute the following code:

 $ php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

If you get this output, the installation is safe to run.
Installer verified
You can now execute the installation script that we previously downloaded in /tmp/composer-setup.php to install composer system-wide in the /usr/local/bin directory with the command:

 $ sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer

To check if the installation went through successfully, simply run:

 $ composer

You should receive similar output to this:

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.3.5 2022-04-13 16:43:00
 
Usage:
  command [options] [arguments]
 
Options:
  -h, --help                     Display help for the given command…
….

Composer can also be installed locally to the project’s directory or in the home directory of the user. For example, if you want to install Composer in the home directory of the user dev, you will need to navigate to his directory and run the following command as the user dev

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

Now you can run composer with the command:

$ php composer.phar

And you will receive similar output as previously.

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS

Using Composer in a PHP project

Now to use Composer in a PHP project, you will create a directory for your project and add the PHP dependencies using composer. For this tutorial, we will use the Monolog package for sending logs to files, sockets, databases, and various web services.

Now to create the directory monolog and add the Monolog package, use the following commands:

 $ mkdir monolog
$ cd monolog
$ composer require monolog/monolog

Now you can list your directory, and you will see that there is a composer.json file in your directory, which has the monolog package as a dependency.

 $ cat composer.json
{
    "require": {
        "monolog/monolog": "^2.6"
    }
}

Now you can add the following PHP code in the monolog.php file to test the functionality of the monolog.

To update only a specific package in your PHP project, you can use the command:

 $ composer update vendor/package

Congratulations! You successfully installed Composer on Ubuntu 20.04 and added package dependency in the composer.json file. If you find it difficult to install Composer, you can always contact our technical support, and they will do the rest for you. We are available 24/7.

If you liked this post on how to install and use Composer on Ubuntu 20.04, please share it with your friends on social networks or simply leave a reply below. Thanks.

Leave a Comment