How To install PHP Composer in Linux/Windows

This tutorial helps to install Composer into your system. The PHP Composer is a powerful dependency manager for PHP projects. It helps to adding, managing, and updating libraries and packages in your project.

You can use Composer as a dependency manager for web applications, a content management system, or any PHP-based project, It helps to automate the management of your project’s dependencies.

You can also read: How to Uninstall Composer in Windows 10/11

How to Install PHP Composer

Let’s walk you through the step-by-step process of installing PHP Composer on your system.

Step 1: Prerequisites

There are the following prerequisites in place to install Composer:

You need to requires PHP to be installed on your system. You can check if PHP is installed by opening a terminal or command prompt and run the below command:

php -v

If PHP is not installed, you’ll need to install it. You can download PHP from the official PHP website https://www.php.net/downloads.php.

  • Linux: You can install PHP using a package manager like apt.
  • macOS: You can install PHP using the Brew package manager.
  • Windows: You can install PHP using XAMPP or WAMP.

Step 2: Verify the Installer

The Composer is distributed as a PHP script, and you can download it using the following command:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

The above command will download the Composer installer script (composer-setup.php) to your current directory.

Step 3: Verify the Installer

You can verify its SHA-384 hash with the expected hash from the Composer website https://composer.github.io/pubkeys.html

php -r "if (hash_file('sha384', 'composer-setup.php') === 'EXPECTED_HASH_HERE') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

Replace ‘EXPECTED_HASH_HERE‘ with the hash you obtained from the Composer website. The above command will compare with the actual hash of the downloaded installer script.

Step 4: Run the composer Installer

We have already installed composer and navigated to that location and run the below command:

php composer-setup.php

The above command will install Composer globally on your system and make it available system-wide.

Composer Globally Accessible on Linux

You can make Composer globally accessible from the command line in Linux or macOS. You need to move the Composer executable to a directory in your system’s PATH. You can use the following command:

sudo mv composer.phar /usr/local/bin/composer

Composer globally accessible in Windows

You need to set composer.phar file path in your system’s PATH, such as C:\Windows\System32.

Verify the Installation

To verify that Composer has been successfully installed, open a new terminal or command prompt window and run the following command:

composer --version

How To Update Composer in Linux

You can update the composer using the below command:

composer self-update

The above command will fetch the latest version of Composer and update it on your system.

Conclusion

We’ve walked you through the step-by-step installation of PHP Composer on your system. The Composer is a powerful tool that will help you manage dependencies in your PHP projects. You can manage PHP project package dependencies and seamlessly transfer projects between different systems without concerns about package versions.

Leave a Reply

Your email address will not be published. Required fields are marked *