How to Install and Use PHP Composer on Ubuntu 20.04?

Photo of author

By admin

The composer program is a PHP tool for managing project dependencies. Recently, it has become an extremely popular tool, and possibly the most effective as well. Follow this guide for complete instructions on installing and using PHP Composer on Ubuntu.

Before we begin with the step-by-step tutorial, let’s understand more about the basics.

What is PHP Composer?

Composer is a PHP project dependency management tool. It lets you stipulate the libraries your project relies on. It also installs or updates them.

Now, you will be thinking about what PHP is.

PHP is an acronym that stands for Hypertext Preprocessor. It is a prominent open-source scripting language. PHP is free to use, and its script runs on the server. Server-side languages like this are easy to use and are suitable for beginners.

It is efficient enough to be at the heart of the world’s largest blogging platform – WordPress! And it is sharp enough to power the biggest social networking site in the world – Facebook!

PHP files can contain HTML, Text, JavaScript, CSS, and PHP code. PHP files are indicated by the extension “.php.”.

PHP works on multiple systems, including Windows, Linux, Unix, Mac OS X, and is compatible with nearly all modern servers, notably Apache, IIS, and others.

PHP has been criticized previously for lacking a dependency management tool. Before the introduction of Composer, there had been unsuccessful efforts to establish a common PHP dependency management tool, such as PHPPM and Pyrus, which were designed to manage PEAR packages.

And finally, in 2012, PHP Composer was released as a package management tool to install and manage PHP modules. Following that, one can easily incorporate these modules into their project.

It assists users in installing the appropriate version of PHP modules within your app. It also keeps track of all the loaded modules and their versions. All entries are saved in a folder titled composer.json.

What is Ubuntu?

Well, this is an easy one. Ubuntu is a free Linux operating system with community and professional support.

Ubuntu is a community built on principles outlined in the Ubuntu Manifesto: that software should be free, that software tools should be accessible in any language and regardless of disability, and that people should be free to modify and customize their software in any way they like.

This guide will walk you through steps to install and configure PHP composer on Ubuntu 20.04 LTS systems.

Requirements:
  • Shell access to Ubuntu system that is up and running
  • Preinstalled PHP 5.3 or above on Ubuntu 20.04
  • You must have Sudo access and should be logged in as a non-root user.
  • Make sure your server’s firewall is powered on.

Let’s find out how to Install and Use PHP Composer on Ubuntu 20.04

Installing:

  • PHP has to be there on your PC for Composer to work. You might not have PHP installed. In that case, run the commands underneath to refresh your apt-cache and install PHP. Composer requires php-cli to run PHP scripts from the command line, as well as unzip to extract compressed packages.
  • Run the following command in your terminal to update the package manager cache:

$ sudo apt update

  • To install the needed packages use the command below

$ sudo apt install php-cli unzip

  • You’ll also have to install a few extra packages on your system. Use the shell to run the commands listed below.

$ sudo apt install unzip curl

  • After you’ve installed all of the essential packages, run the following command to check the existing PHP command line version:

$ php -v

  • Now, we’ll check to see if the downloaded installation matches the SHA-384 hash for the most recent installer, which can be obtained on the Composer Public Keys / Signatures page.

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

  • If you prefer to verify the result, run:`

$ echo $HASH

  • Finally, perform the PHP code given on the Composer download page to ensure that the installation script is suitable to run

php -r “if (hash_file(‘SHA384’, ‘composer-setup.php’) === ‘$HASH’) { echo ‘Installer verified’; } else { echo ‘Installer corrupt’; unlink(‘composer-setup.php’); } echo PHP_EOL;”

  • Now, apply the next command to download the executable file of the composer to your PC.

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

  • Now execute the following commands to make Composer accessible to all users on your system so that all PHP programs on that system may use it.

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

  • Following the installation of the Composer on your PC. At the command terminal, type composer. This will give you the composer version as well as the options accessible with the composer command.

$ composer -v

Using:

A composer.json file is required to utilize Composer in any project. Composer uses the composer.json file to determine which dependencies to download for the project, as well as which versions of each package are authorized for installation. This is critical for maintaining the project’s relevance and preventing installation of fragile versions that may cause backwards compatibility issues.

You do not need to construct this file manually; doing so will almost certainly result in syntax errors. The composer provides an interactive mechanism for generating a new composer.json file depending on the user’s data, which is useful if you plan to distribute the project next as an unrestricted package on Packagist.

  • By using the same commands as during installation, you may obtain the latest versions of the Composer. The Composer is also capable of updating itself. To update the Composer, run the below-mentioned command.
  • Existing Project: Drive to the program root directory and administer the following command. It will read composer.json and install the application’s dependencies.

$ Composer install

  • New Project: To begin a new project, navigate the project directory and run the following command to install the necessary modules.

$ mkdir myapp && cd myapp

$ composer require psr/log

The command above will install the psr/log module in the vendor directory. It also adds an entry to composer.json and updates composer.lock.

Upgrading:

The Composer has the ability to upgrade itself. By executing a self-update, Composer will be brought up to date. Otherwise, you can get the most recent version of Composer by running the same commands you used to install it.

$ sudo composer self-update

Conclusion

Composers are becoming more popular as tools for managing project dependencies in PHP.

In PHP development, if norms are followed, better language could be developed. The composer has successfully resolved a critical issue in the PHP community, so there is no reason not to embrace it immediately.

We’ve covered the basics of Composer and how to install and use PHP Composer on Ubuntu.

We are hoping we’ve made your life a little easier!

Leave a Comment