PHP Composer is a tool that can help you manage and organize the external libraries and packages used in PHP projects, making it easy to add, update, or remove packages as needed.
How to Check the Composer Version
Both Composer 1 (deprecated) and Composer 2 are already pre-installed on our Web Premium, Web Business, and Cloud plans. You can verify the installed versions via SSH by running these commands:
For Composer, type
composer
. You will get the following result:
Similarly, you can type
composer2
for the Composer 2 version:
In both cases, the command-line template will be shown below the Composer version, as well as a list of available options and commands.
How to Install a Package Using Composer
Go to the Packagist repository and find the PHP package you want to install. Keep this page open for reference
Access your hosting environment using SSH
Use the
cd
command to navigate to the desired directory on your hosting. Consider that each package needs to be installed separately on every website or project where you will need itCopy the command from Packagist and paste it onto the terminal. This will either create the composer.json, composer.lock files and the vendor directory or update them if they already exist
Proceed to test the installed package by creating or modifying your relevant PHP files
If your hosting uses PHP version 8.0 or higher, use composer2 instead of composer, like this: composer2 [command]
Example
Let's use PHPMailer as an example. Open the Packagist repository to find the installation command:
Now, on your SSH terminal, navigate to the website's root folder. In this case, for domain.tld, the cd
command will be as follows:
cd domains/domain.tld/public_html/
Once you've reached the desired location, enter the composer command from Packagist. Since support for Composer 1 is deprecated, the composer2 command will be used instead:
composer2 require phpmailer/phpmailer
The output will look similar to this:
That's it - PHPMailer is successfully installed for domain.tld. You can verify it because the vendor/phpmailer directory will be created.
For installing it in another project, return to the domains directory using the command cd ../..
then navigate to the public_html directory of the new domain, such as website.tld:
cd website.tld/public_html/
Repeat the installation process as before. In case you encounter any issues, check this article: Solving Common Composer Errors.
Additional Resources
Check the following tutorial for a comprehensive PHPMailer guide: How to Send Emails with PHP Mailer