How to Install and Use PHP Composer on Ubuntu 20.04
4 min read
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 update
sudo apt install wget php-cli php-zip unzip
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 .
To install Composer globally as a system-wide command that will be available for all users, simply place the file in a directory that is in the system
PATH. Run the following command to install Composer in the
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
All settings correct for using Composer Downloading... Composer (version 1.10.7) successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composer
You can now use Composer by running
composerin your terminal.
To install composer locally enter:
sudo php composer-setup.php --install-dir=/path/to/project
This will download a file named
composer.pharin your project root directory. To use Composer navigate to the project directory and run
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 :
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.lock, and a
-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
vendoris the directory where the project dependencies are stored.
composer.lockis a file that keeps 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.
Composer has autoload capabilities which allow us to use PHP classes without the need to
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.
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
Run the script by typing:
The output should look something like this:
Now: 2020-06-17 20:41:04
Later, if you need to update the project packages, enter:
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.
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.