For managing PHP dependencies and packages in your projects, PHP Composer is a potent solution. It is a crucial tool for PHP developers since it makes the process of adding. Once added then updating, and maintaining libraries and frameworks. Enabling you to make the most of this crucial tool for your work on PHP development. Elevate your command-line productivity by mastering how to install and set up Zsh in Ubuntu. A shell that offers unparalleled customization and a wide range of plugins that can transform the way you interact with your Ubuntu system.
Here, we will understand how to install Composer on Ubuntu in detail. I’ll walk you through the installing composer on Ubuntu step-by-step. To track the dependencies for the project the PHP composer has been used. Composer can be used to determine a bunch of libraries for a particular project. They understand how the software comprehends on own different versions and dependencies to install.
Install PHP Composer on Ubuntu 20.04 | 22.04
The use of the modprobe command in Linux is essential for managing kernel modules, allowing enhanced system functionality, and resolving hardware issues easily. Coming back, Follow the Steps Given Below:
First of all, you need to check that PHP is available on the System before Installing PHP
Composer on Ubuntu.
php -v
Once confirmed Follow the steps to install
Step 1: First, Update the Local Repository
Using the Following Command line upgrade the local repository lists.
sudo apt-get update
Continue to the Second Step once the update has been successful.
Step 2: In this Step Get the Composer Installer Downloaded
With the following command download the composer installer:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
This command helps to copy the installer to the machine.
Step 3: Check the Honesty of the Installer that was Downloaded Above.
Follow the steps to check the download one by one:
1. By going to the Composer Public Keys Page you need to copy the Installer Checksum(SHA-384)
2. By Pasting the Key from Step One which was copied following the setting up of the code shell variable
COMPOSER=key
3. The command or the Script below has to be run for the comparison of the official hash and the
one that was downloaded.
php -r "if (hash_file('SHA384', 'composer-setup.php') === '$COMPOSER') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
So once you run the above script it will clear that the download is verified or bribed. If it’s a bribe then the code deletes the installer which is downloaded. Then you need to download the File again and give it another try.
Step 4: Finally, Install PHP Composer
1. In this step with the following command you require curl, unzip to Install PHP Composer, and
some more services. Command as follows:
sudo apt-get install curl php-cli php-mbstring git unzip
2. Installing Composer as the command can be accessible from the whole system. To install to
/usr/local/bin enter:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
The output will display as below:
All settings correct for using Composer Downloading... Composer (version <version>) successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composer
3. Check the Installation after the Installer Finishes.
composer --version
The output will display the version number installed, check the example:
Composer version <version>
4. Run the following command to Remove the Installer File:
php -r "unlink('composer-setup.php');"
Usage of Basic Composer
Simple Composer Use
Dependencies are tracked by Composer on a per-project basis. Other users will find it simpler to establish the same environment as per the result. The composer.json file uses the composer to keep track of the program requirements and permitted versions.
To preserve consistency even if the directory is copied, it also employs a composer .lock file. These files are automatically created when the need command is used.
1. Launch a Terminal and Type the following Commands:
mkdir c_sample && cd c_sample
2. After that, pick a package to load. Downloadable PHP packages can be found on the website packagist.org. We employ the monolog/monolog package in this illustration. Either carry out the subsequent procedures or search the website for a different bundle.
3. Type in the Terminal Window.
composer require monolog/monolog
The system also creates the composer.json and composer.lock files when you download an application.
4. After the Procedure is Finished, List the Directory’s Contents:
ls -l
The list includes a vendor directory as well as the files composer.json and composer .lock.
5. To see the Composer.json file’s Contents:
cat composer.json
The monolog software is added to the system. The software’s minimum version is indicated by the carat symbol next to the version number.
Configuring Autoloading
Classes are not loaded automatically by PHP. To make working with dependencies considerably simpler, configure Composer to autoload classes.
1. Launch the text editor of your choice and make a new file:
sudo nano composer_sample.php
2. Add the subsequent information to the file:
<?php require __DIR__ . '/vendor/autoload.php'; use Monolog\Logger; use Monolog\Handler\StreamHandler; // create a log channel $log = new Logger('name'); $log->pushHandler(new StreamHandler('~/c_sample/text.log', Logger::WARNING)); // add records to the log $log->warning('Foo'); $log->error('Bar');
3. Press Ctrl+O to write the file and Ctrl+X to close it.
4. Execute the autoload monolog command:
php composer_sample.php
There is no output in the result.
Refreshing Dependencies
Enter the following command to update all dependencies in the composer.json file:
composer update
All dependencies are updated under the version in the file.
Use the following to update one (or more) dependencies separately:
composer update vendor/package vendor_b/package_b
Wrapping up with How to Install and Use Composer on Ubuntu
PHP Composer is a useful addition to your arsenal for developing PHP applications. Streamlining the management of dependencies, and boosting the effectiveness of your projects. You may include third-party packages, libraries, and frameworks. Hope you got an idea about Composer install and Into your apps if Composer is installed on your Ubuntu system.
You may easily master Composer’s capabilities by adhering to the clear installation. Also, usage instructions are provided in this manual. So go ahead and give it a shot to streamline the PHP development workflow right now. You should now be able to install and use PHP Composer with ease. We also spoke about upgrading dependencies and setting up autoloading. Hope you got a good idea about installing Composer on Ubuntu 20.04 | 22.04.
Frequently Asked Questions (FAQ)
1. What is Composer?
Composer is a dependency manager for PHP. It helps to manage libraries and packages. It ensures that all packages are up-to-date and all packages are running together correctly.
2. What are the System Requirements to Install Composer on Ubuntu?
Below are the Required needs:
- Ubuntu 18.04 and higher
- PHP 7.2 or above
- Update System Packages