php Ubuntu

How to Install and Use PHP Composer on Ubuntu 20.04

A composer in its nature is a tool that is employed to administer the essentials for PHP. It assists its user in for installing and updating of the essentials of the project. It installs the packages compatible with the project and their most suitable version for the project by a thorough evaluation. It exhibits a leading function in the platform of programming to efficiently use the depositories, modules, and packages. The frameworks of PHP involve the functioning of Laravel, Drupal, Symfony, and some other platforms. You can specify the library that you will need for your project through the PHP composer. It also acts as an arbitrary factor in finding a suitable version of packages for those libraries. This guide is exclusively formed to make you familiar with the installation of the PHP composer on ubuntu 20.04. Not only that you can also learn the use of PHP on ubuntu effectively.

Requirements

Before the installation process, you are required to have access to the following for the initial installation set-up in your system.

Ubuntu 20.04 server.

A non-root user encompassing the privileges of sudo.

Essentials encompassing php-cli, and unzip.

Firewall with permission access on your server.

Installation process

The core process of installing PHP composer is dependent on the 3 primary functions that are given below.

Step# 1: Initial updating

Step# 2: Installation of PHP composer

Step# 3: Global access

Initial Updating

Use the apt command of sudo to update your existing system packages.

$ sudo apt update

If you don’t have the dependencies mentioned in requirements, then use one of these commands to install them.

$ sudo apt install php libapache2-mod-php
php-mbstring php-xmlrpc php-soap php-gd php-xml php-cli php-zip

$ sudo apt install php-cli unzip curl

If you want to check the compatible version of the PHP composer with your system, then apply the following command.

$ php -v

You will need curl to proceed with the installation of PHP composer so implement the following command to download curl, in case you don’t already have it in your system.

$ sudo apt install curl

Installation of PHP composer

Now to install the composer type the curl command in your terminal system. you can also apply the “wget command” instead of the curl command.

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

The output window will show you the installation description of the composer along with its version.

Global access

If you want to broaden the access of the PHP composer globally exploitable then you can use impose the following command in your terminal system.

$ sudo mv composer.phar /usr/local/bin/composer

$ sudo chmod +x /usr/local/bin/composer

$ source ~/.bashrc

Lastly, you can copy this command to validate the operationality of the composer you installed.

$ composer -v

Once you have successfully installed the composer you will receive some basic repository packages along with the PHP composer package. Those packages encompass “composer.json”, “composer.lock”, “vendor”, “autoload.php”, “composer.phar”.

Using PHP Composer on Ubuntu 20.04

To begin the implementation of PHP composer for your project you will need to undergo the following procedures.

Add a root directory

The first thing you need to do to start a project by using the PHP composer is to generate a root directory for your project.

$ mkdir ~/composer-project
$ cd ~/composer-project

You can replace the composer-project with your project name or anything to make a URL path commonly known as a slug in programming.

Installation of packages

In this step, you will look for the libraries that are mandatory to create your project. There will be a central repository for the composer that contains all the generally accessible PHP composer packages you get with installing the composer. You can use either the package of nesbot/carbon, cocur/slugify, and cakephp/chronos, all of these are the simple packages for time.

$ composer require cakephp/chronos

The output window will display the installation of the composer.json file along with the essentials of the package we applied above. Now run the following command to check the addition of composer.json and cakephp/chronos in your project root depository.

$ cat composer.json

Forming a PHP file

We can generate a PHP file by taking “Chronos” into account and utilizing the gedit editor and .php package.

$ sudo gedit demo.php

You can replace the demo with your actual project. Now copy this code in the demo.php function

# <?php
# require 'vendor/autoload.php';

# use Cake\Chronos\Chronos;

# printf("Now: %s \n", Chronos::now());
# ?>

This code comprises 7 lines where the 2nd line portrays the composer file that will assist in auto loading of the libraries and packages specifically installed for your project

Now execute the operation by running this command

$ php demo.php

You can conveniently update all your project-based packages using this command.

$ composer update

Conclusion

Installation of PHP composer on ubuntu 20.04 is a convenient process. Though, the usage of PHP composers for your projects is a complicated concept. But once you grasp the feel of it you can easily run any project dependent on the PHP composer. Make sure to replace the demo in the above-mentioned steps with your project to effectively manage your project build.

About the author

Younis Said

Younis Said

I am a freelancing software project developer, a software engineering graduate and a content writer. I love working with Linux and open-source software.