Composer for PHP is a dependency management tool that simplifies the process of handling external libraries and packages in PHP applications. It allows developers to declare their project's dependencies in a "composer.json" file and automates the installation, updating, and autoloading of those libraries.
By fetching packages from repositories, Composer resolves dependencies, manages conflicts, and generates an autoloading file. With its CLI, developers can easily update, remove, or search for packages, making Composer an essential tool for PHP developers to enhance code reusability and streamline the management of dependencies.
This article will explain how to install Composer on VPS and Dedicated servers.
Related Articles
Update Composer
Composer Requires PHP Version Error
Downgrade Composer
Install Composer
IMPORTANT: This solution will require root access. If this needs to be obtained, follow the guide on requesting root access.
TIP: If Composer is already installed but needs to be updated, view this guide.
NOTE: There is a package for Composer that is already installed by default on cPanel servers. If for some reason 'composer' isn't on your $PATH, here is the relevant cPanel documentation on enabling it.
- Access your server via SSH as the root user or using the Terminal in Root WHM
- Install the PHP CLI (command-line interface) package and all other dependencies with the following command
sudo yum install php-zip wget unzip
- Run the following command to download the Composer installation shell script
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
- Use the following wget command to download the expected signature of the latest Composer installer from the Composer's Github page and store it in a variable named HASH
HASH="$(wget -q -O - https://composer.github.io/installer.sig)"
- Verify that the installation script is not corrupted by running the following command
php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
- If the hashes match, the following message will be shown
Installer verified
- If the hashes don't match, the following message will be shown
Installer corrupt
- In this case, you need to re-download the Composer installation script and double-check the value of the $HASH variable with echo $HASH
- Once the installer is verified, continue to Step 6
- In this case, you need to re-download the Composer installation script and double-check the value of the $HASH variable with echo $HASH
- If the hashes match, the following message will be shown
- Run the following command to install Composer in the /usr/local/bin directory
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
EXAMPLE: The expected output of the above command is as follows, with a different version likely.
All settings correct for using Composer
Downloading...
Composer (version 2.2.18) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composerNOTE: The composer is installed as a system-wide command and it will be available for all users.
Verify Installation
- The last step is to verify the installation by running the following command
composer
- The command above will print the Composer's version, commands, and arguments
EXAMPLE:______ / ____/___ ____ ___ ____ ____ ________ _____ / / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/ / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/ /_/ Composer version 1.8.5 2019-04-09 17:46:47 Usage: command [options] [arguments]
- Congratulations, you have successfully installed Composer
Comments
0 comments
Article is closed for comments.