Site icon DesignLinux

How to Install and Use PHP Composer on Ubuntu 20.04

Composer is a dependency manager for PHP (similar to npm for Node.js or pip for Python).

With Composer, you can specify the PHP libraries your project depends on, and it will pull and manage all libraries and dependencies for you. Composer is used in all modern PHP frameworks and platforms such as Laravel, Symfony, Drupal, and Magento.

This tutorial explains how to install and use Composer on Ubuntu 20.04.

Installing PHP Composer on Ubuntu

Before installing Composer, ensure that you have all the necessary requirements installed on your system:

sudo apt updatesudo apt install wget php-cli php-zip unzip

Composer offers an installer written in PHP that we’ll use to install Composer. Use wget to download the installer:

wget -O composer-setup.php https://getcomposer.org/installer

The command above will save the file as composer-setup.php in the current working directory.

Composer is a single file CLI application and can be installed either globally or as part of the project. The global installation requires sudo privileges.

When a new Composer version is available, you can update your installation using the following command:

sudo composer self-update  

Getting Started with Composer

Now that Composer is installed on your Ubuntu system, let’s see how to create a PHP project with Composer.

The first step is to create the project root directory and navigate to it:

mkdir ~/my-first-composer-projectcd ~/my-first-composer-project

In this example, we’ll use a PHP package called carbon to create a sample application that prints the current time.

Run the following command to initialize a new Composer project and install the carbon package:

composer require nesbot/carbon
Using version ^2.35 for nesbot/carbon
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 5 installs, 0 updates, 0 removals
  - Installing symfony/translation-contracts (v2.1.2): Downloading (100%)         
  - Installing symfony/polyfill-php80 (v1.17.0): Downloading (100%)         
  - Installing symfony/polyfill-mbstring (v1.17.0): Downloading (100%)         
  - Installing symfony/translation (v5.1.2): Downloading (100%)         
  - Installing nesbot/carbon (2.35.0): Downloading (100%)         
Writing lock file
Generating autoload files
5 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

As shown in the output, Composer creates the composer.json file and downloads and installs carbon and all its dependencies.

If you list your project’s directory, you will see that it contains two files composer.json and composer.lock, and a vendor directory.

ls -l
-rw-rw-r--. 1 vagrant vagrant    60 Mar 27 18:05 composer.json
-rw-rw-r--. 1 vagrant vagrant 11135 Mar 27 18:06 composer.lock
drwxrwxr-x. 6 vagrant vagrant    82 Mar 27 18:06 vendor
All PHP packages installable with Composer are listed at Packagist.

Composer has autoload capabilities which allow us to use PHP classes without the need to require or include the files.

Create a file named testing.php and add the following code:

<?php

require __DIR__ . '/vendor/autoload.php';

use Carbon\Carbon;

printf("Now: %s", Carbon::now());

Let’s analyze the code line by line.

The vendor/autoload.php file is automatically generated by Composer and autoload all of the libraries.

Next line creates alias Carbon and the last line prints the current time using the Carbon now method.

Run the script by typing:

php testing.php

The output should look something like this:

Now: 2020-06-17 20:41:04

Later, if you need to update the project packages, enter:

composer update

The command above will check for newer versions of the installed packages, and if a newer version is found and the version constraint match with the one specified in the composer.json, Composer will update the package.

Conclusion

We have shown you how to install Composer on Ubuntu 20.04 and how to use it to create a basic PHP project.

For more information about Composer, visit the official documentation page.

If you have any questions, please leave a comment below.

composer php ubuntu
Exit mobile version