YouTube Icon

Code Playground.

How to Install and Use PHP Composer on Ubuntu 20.04


How to Install and Use PHP Composer on Ubuntu 20.04

Arranger is a reliance chief for PHP (like npm for Node.js or pip for Python ). 

With Composer, you can determine the PHP libraries your venture relies upon, and it will pull and deal with all libraries and conditions for you. Arranger is utilized in all advanced PHP structures and stages, for example, Laravel, Symfony, Drupal, and Magento. 

This instructional exercise discloses how to introduce and utilize Composer on Ubuntu 20.04. 

Installing PHP Composer on Ubuntu

Prior to introducing Composer, guarantee that you have all the vital necessities introduced on your framework: 

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

Arranger offers an installer written in PHP that we'll use to introduce Composer. Use wget to download the installer: 

wget -O composer-setup.php

The order above will spare the record as writer setup.php in the current working index . 

Writer is a solitary record CLI application and can be introduced either around the world or as a component of the undertaking. The worldwide establishment requires sudo benefits . 

To introduce Composer around the world as a framework wide order that will be accessible for all clients, essentially place the record in an index that is in the framework PATH . Run the accompanying order to introduce Composer in the/usr/neighborhood/receptacle index: 

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
All settings correct for using Composer

Composer (version 1.10.7) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

You would now be able to utilize Composer by running author in your terminal. 

To introduce author locally enter: 

sudo php composer-setup.php --install-dir=/path/to/project

This will download a document named composer.phar in your task root catalog. To utilize Composer explore to the undertaking catalog and run php composer.phar 

At the point when another Composer variant is accessible, you can refresh your establishment utilizing the accompanying order: 

sudo composer self-update  

Beginning with Composer 

Since Composer is introduced on your Ubuntu framework, how about we perceive how to make a PHP venture with Composer. 

The initial step is to make the undertaking root catalog and explore to it : 

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

In this model, we'll utilize a PHP bundle considered carbon to make an example application that prints the current time. 

Run the accompanying order to instate another Composer extend and introduce the carbon bundle: 

writer require nesbot/carbon 

Utilizing adaptation ^2.35 for nesbot/carbon 

composer require nesbot/carbon

Stacking arranger archives with bundle data 

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 appeared in the yield, Composer makes the composer.json document and downloads and introduces carbon and every one of its conditions. 

On the off chance that you list your venture's index , you will see that it contains two documents composer.json and composer.lock, and a merchant registry. 

ls -l
-rw-rw-r--. 1 linuxize linuxize    60 Mar 27 18:05 composer.json
-rw-rw-r--. 1 linuxize linuxize 11135 Mar 27 18:06 composer.lock
drwxrwxr-x. 6 linuxize linuxize    82 Mar 27 18:06 vendor
  • merchant is where the venture conditions are put away. 
  • composer.lock is a record that keeps data pretty much completely introduced bundles and their renditions, bolting the venture to the particular adaptations. 
  • composer.json is the record that depicts your PHP venture, including the PHP conditions and other metadata. 

All PHP bundles installable with Composer are recorded at Packagist . 

Writer has autoload capacities which permit us to utilize PHP classes without the need to require or incorporate the documents. 

Make a record named testing.php and include the accompanying code: 


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

use Carbon\Carbon;

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

How about we dissect the code line by line. 

The seller/autoload.php record is consequently created by Composer and autoload the entirety of the libraries. 

Next line makes false name Carbon and the last line prints the current time utilizing the Carbon now strategy. 

Show the content to composing: 

php testing.php

The yield should look something like this: 

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

Afterward, in the event that you have to refresh the task bundles, enter: 

composer update

The order above will check for more current variants of the introduced bundles, and if a fresher rendition is found and the form limitation coordinate with the one determined in the composer.json, Composer will refresh the bundle. 


We have told you the best way to introduce Composer on Ubuntu 20.04 and how to utilize it to make a fundamental PHP venture. 

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

On the off chance that you have any inquiries, it would be ideal if you leave a remark underneath.