How to Use Composer

Using Composer with on Web and Cloud hosting plans at Hostinger

Updated over a week ago

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:

    The installed Composer 1 version on your hosting environment


  • Similarly, you can type composer2 for the Composer 2 version:

    The installed Composer 2 version on your hosting environment

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

  1. Go to the Packagist repository and find the PHP package you want to install. Keep this page open for reference

  2. Access your hosting environment using SSH

  3. 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 it

  4. Copy 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

  5. 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:

The composer command to install phpmailer on your hosting

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:

A terminal window showing the output of a successful phpmailer installation using composer2

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

Did this answer your question?