What is Composer?
Composer is an application-oriented package manager for PHP distributed under an open-source MIT license. It loads the various dependencies required by a project developed in the PHP programming language to get set up. The key advantage of Composer is that you can update these dependencies automatically and do not have to deliver these files with your PHP code, as these are downloaded again on the system used with the command. Available PHP applications can be searched via the “Packagist” platform. It is a command line that can be easily installed on Linux, macOS, and Windows.
With the help of an autoloader of Composer, a vendor can load all packages from the folder. Of course, you can add your packages to the autoloader, or you can run all of your code through the Composer autoloader.
Why do I need a composer?
Well, a lot of PHP applications need external packages to function properly. And Composer is a shortcut to automatically getting these packages into one rather than manually installing all of them. For example, a forum project called Flarum is an open-source PHP-based forum web platform. While we can clone files directly, we have to run them from its GitHub repository. However, installing the required dependencies and updating them in the future might be a problem. To mitigate this, we can use Composer with its command to update packages directly, if needed, in the future.
Step 1: Prerequisites
- System running Ubuntu 22.04 Or Ubuntu 20.04
- Access Terminal Command line
- Sudo or root privileges on local or remote machines
Step 2: Run APT-GET Update
First, open a terminal and execute the following command on the command line to update apt-get packages in the Linux Ubuntu system:
#! /bin/bash sudo apt-get update
Step 3: Install PHP and cURL
Then execute the following command on the command line to install PHP and cURL:
#! /bin/bash sudo apt-get install curl unzip sudo apt-get install php php-curl
Step 4: Download Composer
Execute the following command on the command line to download the composer using curl:
#! /bin/bash curl -sS https://getcomposer.org/installer -o composer-setup.php
Step 5: Install Composer
Once the composer has been downloaded, then execute the following command on the command line to install and setup composer on the Linux Ubuntu system:
#! /bin/bash sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
When a new Composer version is available, you can update your installation using the following command:
#! /bin/bash sudo composer self-update
Step 6: Check Composer Version
Then execute the following command on the command line to verify composer installation on Linux ubuntu:
#! /bin/bash composer -v
Step 7: How to Use 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 using the following command:
#! /bin/bash mkdir ~/var/www/html/composer-project cd ~/var/www/html/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:
#! /bin/bash composer require nesbot/carbon
#! Output Info from https://repo.packagist.org: #StandWithUkraine Using version ^2.58 for nesbot/carbon ./composer.json has been created Running composer update nesbot/carbon Loading composer repositories with package information Info from https://repo.packagist.org: #StandWithUkraine Updating dependencies Lock file operations: 6 installs, 0 updates, 0 removals - Locking nesbot/carbon (2.58.0) - Locking symfony/deprecation-contracts (v2.5.1) - Locking symfony/polyfill-mbstring (v1.26.0) - Locking symfony/polyfill-php80 (v1.26.0) - Locking symfony/translation (v5.4.9) - Locking symfony/translation-contracts (v2.5.1) Writing lock file Installing dependencies from lock file (including require-dev) Package operations: 6 installs, 0 updates, 0 removals - Downloading symfony/translation (v5.4.9) - Installing symfony/translation-contracts (v2.5.1): Extracting archive - Installing symfony/polyfill-php80 (v1.26.0): Extracting archive - Installing symfony/polyfill-mbstring (v1.26.0): Extracting archive - Installing symfony/deprecation-contracts (v2.5.1): Extracting archive - Installing symfony/translation (v5.4.9): Extracting archive - Installing nesbot/carbon (2.58.0): Extracting archive 3 package suggestions were added by new dependencies, use `composer suggest` to see details. Generating autoload files 6 packages you are using are looking for funding. Use the `composer fund` command to find out more!
As shown in the output, the 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 it contains two files
composer.lock, and a vendor directory.
#! /bin/bash ls -l
#! Output total 28 -rw-rw-r-- 1 smit smit 60 Jun 9 23:46 composer.json -rw-rw-r-- 1 smit smit 18601 Jun 9 23:46 composer.lock drwxrwxr-x 6 smit smit 4096 Jun 9 23:46 vendor
vendoris the directory where the project dependencies are stored.
composer.lockis a file that keeps the information about all installed packages and their versions, locking the project to the specific versions.
composer.jsonis the file that describes your PHP project, including the PHP dependencies and other metadata.
The composer has to autoload capabilities which allow us to use PHP classes without the need to require or include the files.
Read Also: How to install and Secure Redis on Ubuntu 22.04
Create a file named
test.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.
vendor/autoload.php the file is automatically generated by Composer and autoloaded in all the libraries.
The next line creates the alias Carbon, and the last line prints the current time using the Carbon now method. Run the script following by command:
#! /bin/bash php test.php
#! Output Now: 2022-06-09 23:59:02
Later, if you need to update the project packages, enter:
#! /bin/bash 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.
Thank you for reading this blog.
Read Also: How to make a POST request with cURL
If you want to manage your VPS / VM Server without touching the command line go and Checkout this link. ServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a matter of minutes. You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.
If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.