3.6.1 Using Composer on Hosting
What is Composer
PHP Composer is a tool for managing dependencies in projects written in the PHP programming language. With it, you can easily manage the libraries and dependencies of your project, ensuring they are up to date and simplifying your work.
In fact, Composer is an application-level package manager executed on the command line. The main repository for Composer is Packagist.
Using Composer on hosting
Virtual hosting TheHost supports the PHP Composer dependency manager. Composer can only be used on the command line using an SSH connection. How to connect to the hosting command line is described in detail in this article.
The current current version of PHP Composer on hosting services is 2.7.9.
The following commands are available to use Composer in the terminal:
- composer – launches Composer running PHP version 5.3
- composer-php-5.3 – launches Composer running PHP version 5.3
- composer-php-5.4 – launches Composer running PHP version 5.4
- composer-php-5.5 – launches Composer running PHP version 5.5
- composer-php-5.6 – launches Composer running PHP version 5.6
- composer-php-7.0 – launches Composer running PHP version 7.0
- composer-php-7.1 – launches Composer running PHP version 7.1
- composer-php-7.2 – launches Composer running PHP version 7.2
- composer-php-7.3 – launches Composer running PHP version 7.3
- composer-php-7.4 – launches Composer running PHP version 7.4
- composer-php-8.0 – launches Composer running PHP version 8.0
- composer-php-8.1 – launches Composer running PHP version 8.1
- composer-php-8.2 – launches Composer running PHP version 8.2
- composer-php-8.3 – launches Composer running PHP version 8.3
Usage example:
- composer-php-8.1 about
This command will run the Composer information on the hosting.
Principles of working with Composer
It is important to keep the following points in mind when working with Composer:
-
Composer.json File. All Composer operations are based on the information contained in the
composer.json
file at the root of your project. This file specifies dependencies, scripts, autoloaders, and other project settings. -
Updating composer.json. After every change to the
composer.json
file, such as adding a new dependency or changing the version, you must run thecomposer install
orcomposer update
command to apply the changes to your project . -
Dependencies and Versions. When specifying dependencies in the
composer.json
file, you can specify specific package versions or version ranges. Composer will install or update dependencies based on the specified version requirements. -
Composer.lock File. Composer creates a
composer.lock
file that stores the actual installed versions of the dependencies. This file ensures a consistent development and production environment and ensures that dependencies are accurately reproduced.
Examples of basic commands for working with Composer
Please note: on hosting Composer works exclusively in the context of the PHP version specified in the command. Below are examples for version PHP 8.1.27. As you can see, the command looks like composer-php-8.1
, without the full indication of version 8.1.27. Simplified version indication is not just a nice feature – but a necessary requirement for Composer to work correctly on hosting. For example, for PHP 7.4.33, the command should be composer-php-7.4
.
Installing dependencies
This command installs the dependencies specified in the composer.json
file into the vendor
directory:
- composer-php-8.1 install
Updating dependencies
This command updates all dependencies to their latest versions, as specified in the composer.json
file:
- composer-php-8.1 update
Adding a new dependency
This command adds a new dependency to your project:
- composer-php-8.1 require package_name
Removing dependency
This command removes the dependency from your project:
- composer-php-8.1 remove package_name
Search for packages
This command searches for packages on Packagist, the largest repository of PHP packages:
- composer-php-8.1 search keyword
View a list of installed packages
This command lists all installed packages in your project:
- composer-php-8.1 show
Composer update
This command updates PHP Composer itself to the latest version:
- composer-php-8.1 self-update