How to Install Composer on CentOS 8

Composer is the most popular package management program for PHP, that offers a standard form for managing dependencies of PHP applications and needed libraries that your project relies on and it will manage (install/update) them for you easily.

Composer is a command-line program that installs dependencies and libraries for applications that are available on packagist.org, which is its main repository consist of available packages.

Composer is a very helpful tool for developers when they are in need and want to manage and incorporate the packages for their PHP project. It speeds up time and is recommended to solve any crucial issues in most of the web projects.

In this tutorial, we will show you how to install Composer on CentOS 8 Linux.

Requirements

  • A root account or sudo privileged account with shell access.
  • PHP 5.3.2+ with needed extensions and settings.

Installing Composer on CentOS 8

To install Composer, you must install PHP on the system with required PHP extensions using the following dnf command.

# dnf install php php-cli php-zip php-json
Install PHP on CentOS 8
Install PHP on CentOS 8

Now install Composer using an installer that you can execute locally as part of your project, or globally as a system-wide executable.

Install Composer Locally

To install Composer locally on your current directory, execute the following script in your terminal.

# php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
# php -r "if (hash_file('sha384', 'composer-setup.php') === 'c5b9b6d368201a9db6f74e2611495f369991b72d9c8cbd3ffbc63edff210eb73d46ffbfce88669ad33695ef77dc76976') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
# php composer-setup.php
# php -r "unlink('composer-setup.php');"
Install Composer Locally in CentOS 8
Install Composer Locally in CentOS 8

The above installer will check some php.ini settings and alert you if they are set wrongly. Then the installer will download the latest composer.phar in the current working directory.

The 4 lines above will, in order:

  • Download the installer to the current directory.
  • Verify the installer signature (SHA-384).
  • Run the installer.
  • Remove the installer.

Finally, run php composer.phar in order to run Composer.

# php composer.phar
Run PHP Composer in CentOS 8
Run PHP Composer in CentOS 8

Install Composer Globally

To install and access Composer globally system-wide, you need to place Composer PHAR in your system PATH, so that you can execute it without using the PHP interpreter.

To install Composer globally for all users, run the installer using the following commands.

# php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
# php -r "if (hash_file('sha384', 'composer-setup.php') === 'c5b9b6d368201a9db6f74e2611495f369991b72d9c8cbd3ffbc63edff210eb73d46ffbfce88669ad33695ef77dc76976') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
# php composer-setup.php
# php -r "unlink('composer-setup.php');"
# mv composer.phar /usr/local/bin/composer
# chmod +x /usr/local/bin/composer
# composer -V
Install Composer Globally in CentOS 8
Install Composer Globally in CentOS 8

Now that you’ve installed Composer successfully on your CentOS 8 system. To learn more about the PHP Composer and how can you utilize it in your projects visit the official documentation.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Anusha Saive
Anusha worked for many years in the IT industry as a Project Manager, and also a senior writer and editor at Tecmint. She is a huge fan of Linux and is passionate about writing Linux and technology-related stuff.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

5 Comments

Leave a Reply
  1. Hi,

    Thank you for this tutorial, can you help me to solve this issue?

    # php -r “copy(‘https://getcomposer.org/installer’, ‘composer-setup.php’);”
    PHP Warning: copy(): SSL operation failed with code 1. OpenSSL Error messages:
    error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed in Command line code on line 1
    PHP Warning: copy(): Failed to enable crypto in Command line code on line 1
    PHP Warning: copy(https://getcomposer.org/installer): failed to open stream: operation failed in Command line code on line 1

    Reply
    • @Hachem,

      You can use http:// instead of https as shown.

      # php -r “copy(‘http://getcomposer.org/installer’, ‘composer-setup.php’);”
      
      Reply
  2. This article is published on Janurary 2020, and it has been more than 8 months now. Please check if the hash code remains the same. Thanks.

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.