Composer is a dependency management tool for PHP that allows you to declare the libraries your project depends on and installs them for you. In this article, we will cover the steps for installing Composer on Ubuntu 22.04.
Prerequisites
- Shell access to a running Ubuntu system.
- Install PHP 5.3 or higher version.
- PHP’s package manager,
`php-cli`
, must be installed and available in your `PATH`
Installing PHP Composer on Ubuntu
The Composer’s official team provides a script to install PHP composer on Linux systems. You can download this script using the `curl`
or `wget`
command-line utility. Also, you can download it directly using the PHP script command line.
- To download the
`composer-setup`
script, run the following command in a terminal:php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
- Then execute the downloaded PHP script to install the composer on your Ubuntu system at the desired location. Use
--install-dir
to set the binary location and--filename
to set the binary name. You can choose one of the below option:- Installing PHP composer system-wide: This will install composer in /uer/local/bin directory, that is accessible to all users:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
sudo chmod +x /usr/local/bin/composer
- Installing PHP composer for specific applicaiton: Sometimes you dont’ have permission to install it globally, like shared hosting account. Then you can configure this under you application as well. To install composer locally type:
cd /path/to/php-application && mkdir -p bin
php composer-setup.php --install-dir=bin --filename=composer
chmod +x bin/composer
Make sure to replace
/path/to/php-application
with your application directory.
- Installing PHP composer system-wide: This will install composer in /uer/local/bin directory, that is accessible to all users:
- To see the installed composer version execute binary with
-v
command parameter.composer --version
Output:Composer version 2.3.7 2022-06-06 16:43:28
Upgrade PHP Composer
The PHP composer has the ability to self-upgrade to the latest versions. If the composer is already installed on your system, just type the below command to upgrade the PHP composer to the latest version.
composer self-update
In my case, I already have latest version of composer. So receive the following message on terminal:
Output:You are already using the latest available Composer version 2.3.7 (stable channel).
Working with PHP Composer
You have already installed and configured the composer on your system. The composer will help you to manage modules for your application. For example, to install a new module for your application.
Switch to the PHP application.
cd /path/to/php-application
Run the following command to install psr/log
module in the application.
composer require psr/log
Output:Using version ^3.0 for psr/log ./composer.json has been created Running composer update psr/log Loading composer repositories with package information Updating dependencies Lock file operations: 1 install, 0 updates, 0 removals - Locking psr/log (3.0.0) Writing lock file Installing dependencies from lock file (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Downloading psr/log (3.0.0) - Installing psr/log (3.0.0): Extracting archive Generating autoload files
Composer will automatically create or update composer.json
file at application root directory. Now, the application can use the functionality provided by the module.
The above command will install the latest version of the module. You can also define the module version you want to install for your application. If the module is already installed, it will automatically downgrade/upgrade the package to the specified version.
composer require psr/log=1.0
The module no longer required can be removed with the following command.
composer remove psr/log
All the above commands also update composer.json
file accordingly.
Conclusion
In this article, we have covered the steps for installing Composer on Ubuntu 22.04. By using Composer, you can easily manage the dependencies of your PHP projects and ensure that all necessary libraries are installed and up to date. Whether you are new to PHP development or an experienced developer, Composer can help you streamline your workflow and improve the quality of your projects.