A complex or simple PHP application usually requires extra libraries or components that you need to manage. Doing it manually is effective but also a tedious process. Composer is the tool that can ease the dependencies management in PHP.
In this article, we will show you how to install PHP Composer on Ubuntu 20.04 with a quick and easy process.
What is Composer and why do I need it?
According to the official Composer documentation
Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
It is necessary to clarify that Composer is not a system package manager but it manages the dependencies of your project. For example, You have a project that depends on several libraries and some of them depend on others, what Composer does is to find versions of these libraries and download them to your project automatically. You can update them later.
That is the reason the most recommended way to install PHP frameworks like Laravel or CakePHP is using PHP Composer. This indicates the usefulness and power of the tool.
Install PHP Composer on Ubuntu 20.04
Open the Ubuntu terminal, first update and upgrade the system. Next, install PHP and finally download the composer from the official site. When the download completes run composer-setup.php to complete the installation of the composer.
1. Update your entire distro.
sudo apt update sudo apt upgrade
2. Install PHP, git, and unzip package.
sudo apt install curl php-cli php-mbstring git unzip
Basically, it’s PHP along with other tools for downloading and dependency management.
3. Download the Composer installer.
curl -sS https://getcomposer.org/installer -o composer-setup.php
4. Proceed to install it globally on the system by running the following command.
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Sample Output:
All settings correct for using Composer Downloading… Composer (version 2.1.3) successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composer
5. Test if the installation has been successful by displaying the version of Composer.
composer --version Composer version 2.1.3 2021-06-09 16:31:20
Basic use of PHP Composer
Suppose you have a folder project where your project is located. To make Composer handle your project’s dependencies, you have to tell it to do so.
To do this, run the command
composer init
This generates a file composer.json
where your project information is located. You can always edit it later.
Just run the following command to add a dependency to your project
composer require [dependency]
Once dependencies added, you can install them using the below command
composer install
This way Composer will read all the configurations from the composer.json
file as well as all its dependencies and start downloading and adding them to your project.
To update the project dependencies to the latest version.
composer update
Also, you can search for dependencies with the search subcommand.
composer search [package]
Of course, Composer has many worthwhile options, but these are the most basic ones.
Removing PHP Composer on Ubuntu 20.04
To uninstall PHP Composer simply remove the compiled file from the system.
rm /usr/local/bin/composer
This way it will no longer be present on the system.
Conclusion
PHP Composer is an important tool for every web developer using PHP. In this sense, it is amazing that such a small application can be so powerful and help so much in application development.
In this post, you have learned how to install it on Ubuntu 20.04 in a quick way that you can take advantage of.
Share our post, and help us grow.