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.

Two small things before you go

If something didn’t work on your setup, leave a comment and tell me which distro and version you’re on. I read every one, and most of these articles got better because a reader pointed out what was missing.

And if it did work, share it. TecMint has stayed free for 14 years because people pass these articles around, and that traffic is what pays for writing the next one.

Leave a comment
Get two free Linux courses

Learn the Linux basics with two free courses and join 34,000+ readers getting practical TecMint tutorials and selected Pro TecMint articles every week.

Pro TecMint
Get lifetime access for $99

If you want to go beyond the free tutorials, Pro gives you 18 courses, 5 ebooks, and every future course I add. One payment, lifetime access, no renewals.

Anusha Saive
Anusha Saive
Author
About the author

Anusha is an experienced IT professional who worked as a Project Manager and served as a senior writer and editor at Tecmint. A passionate Linux enthusiast, she is also a fitness and travel lover, HYROX athlete, and certified nutrition specialist.

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.