Choose a different version or distribution
Introduction
Before we begin talking about how to install PHP Composer on Ubuntu 20.04, let’s briefly understand – What is PHP Composer?
PHP Composer is a popular dependency management tool used in web development. It simplifies the process of including external libraries and managing project dependencies. By creating a central repository, Composer allows developers to easily add, update, and remove libraries from their PHP projects. With its simple command-line interface, Composer enhances code reusability and streamlines project setup, making development more efficient and manageable. Harness the power of PHP Composer to enhance your web development workflow and keep your projects organized and up-to-date.
In this tutorial, you will install PHP Composer in an independent environment on Ubuntu 20.04. We will also address a few FAQs on how to install and use PHP Composer on Ubuntu 20.04.
Advantages of PHP Composer
- Simplifies dependency management: Composer makes it easy to include external libraries and manage dependencies in PHP projects.
- Central repository: With a vast collection of packages, Composer offers a central repository for developers to access and integrate into their projects.
- Effortless updates: Composer allows seamless updating of libraries, ensuring your projects stay up-to-date with the latest features and bug fixes.
- Streamlined project setup: Composer simplifies project setup by automatically resolving and installing dependencies, saving time and effort.
- Code reusability: By managing dependencies, Composer promotes code reusability, reducing redundancy and enhancing the overall efficiency of development.
Installing PHP Composer on Ubuntu
Make sure your system has all the prerequisites installed before installing Composer:
sudo apt update
sudo apt install wget php-cli php-zip unzip
Composer provides a PHP installer that we will use to install Composer. Download the installer with wget
:
wget -O composer-setup.php https://getcomposer.org/installer
The file will be saved in the current working directory as composer-setup.php
by the above command.
Composer is a single-file CLI application that can be set up either globally or as part of the project. It is necessary to have sudo privileges for the global installation.
- Simply place the file in a directory that is in the system
PATH
to install Composer globally as a system-wide command that will be available to all users. To install Composer in the/usr/local/bin
directory, run the following command:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Output
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, simply running composer
into your terminal.
- To set up composer locally, type:
sudo php composer-setup.php --install-dir=/path/to/project
This will download a file called composer.phar
to the root directory of your project. Go to the project directory and run php composer.phar
to use Composer.
You can update your installation whenever a new Composer version becomes available by running the command:
sudo composer self-update
Getting Started with Composer
Now that Composer has been installed on your Ubuntu system, let us look at how to use Composer to create a PHP project.
The first step is to create and navigate to the project root directory:
mkdir ~/my-first-composer-project
cd ~/my-first-composer-project
In this example, we will create a sample application that prints the current time using the PHP package carbon.
To start a new Composer project and install the carbon package, enter the following command:
composer require nesbot/carbon
Output
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 can be seen in the output, Composer generates the composer.json
file and downloads and installs carbon and all its dependencies.
Two files, composer.json
and composer.lock
, as well as a vendor
directory, can be found in your project's directory if you list it.
ls -l
Output
-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
vendor
is the directory where the project dependencies are kept.compose.lock
is a file that keeps track of all installed packages and their versions, thus locking the project to those versions.composer.json
is the file that specifies your PHP project, including PHP dependencies and other metadata.
Composer supports autoloading, which allows us to use PHP classes without having to require
or include
the files.
The following code should be added to a file called testing.php
:
<?php
require __DIR__ . '/vendor/autoload.php';
use Carbon\Carbon;
printf("Now: %s", Carbon::now());
Let us examine each line of code individually.
Composer automatically generates the vendor/autoload.php
file, which autoloads all the libraries.
The following line creates the alias Carbon
, and the final line prints the current time using the Carbon now
method.
To run the script, type:
php testing.php
An example of the output would be as follows:
Output
Now: 2020-06-17 20:41:04
If you need to update the project packages later, run:
composer update
The command above will check for updated versions of the installed packages, and if a newer version is found and the version constraint matches the one given in composer.json
, Composer will update the package.
FAQ to Install and Use PHP Composer on Ubuntu 20.04
How do I check if PHP Composer is successfully installed?
Simply type composer --version
in your terminal. If installed correctly, it will display the installed version of Composer.
How can I manage project dependencies using Composer?
Create a composer.json
file in your project directory with the required dependencies. Then run composer install
to download and install the dependencies.
Can I use Composer for global installations of PHP packages?
Yes, Composer allows for global installations of PHP packages. Use the global
flag when installing: composer global require package/name
.
Can I use Composer to autoload classes in my PHP project?
Yes, Composer provides autoloading capabilities for classes. Simply define the autoloading rules in your project's composer.json
file.
How can I handle conflicts between dependencies in Composer?
Composer automatically resolves conflicts by selecting the most compatible versions. If conflicts persist, manually update the conflicting packages to compatible versions.
What is the purpose of the composer.lock
file?
The composer.lock
file ensures that your project always uses the same versions of packages, guaranteeing consistent behavior across different environments or when collaborating with others.
How do I uninstall Composer from Ubuntu 20.04?
To uninstall Composer, simply remove the composer.phar
file from /usr/local/bin/
directory using the command sudo rm /usr/local/bin/composer
.
Conclusion
You now know how to install Composer on Ubuntu 20.04 and how to use it to create a basic PHP project.
Visit the official Composer documentation page to learn more.
If you have any suggestions or queries, kindly leave them in the comments section.