How to Install PHP Composer on a CentOS 9 VPS or Dedicated Server: Complete Guide

How to Install PHP Composer on a CentOS 9 VPS or Dedicated Server: Complete Guide

PHP is one of the most popular programming languages for building web applications. However, while building complex web apps, some developers might find it challenging to manage all the dependencies and libraries. That’s where composer comes in. In this article, we’ll look at how to install composer on CentOS 9 VPS or Dedicated Server.

Contrary to common misconception, composer is not a package manager like Yum or Apt. While it deals with “packages” or libraries, it does so on a per-project basis, installing and updating them in a directory inside a specific project.

Composer does not install anything globally by default, which makes it a dependency manager. However, it supports a “global” project for convenience through the global command which lets you run commands like require, install, remove or update as if you were running them from the COMPOSER_HOME directory.

What is PHP Composer?

Before we learn how to install composer, we need to understand what it actually is. Composer is a PHP dependency management tool. It allows you to declare the libraries your project requires, and it will manage (install and update) them for you.

What is a VPS?

A VPS (Virtual Private Server) is a virtual operating system that runs on a physical server. It’s ideal for organizations that have outgrown shared hosting but can’t afford a dedicated server. VPS providers use virtualization to fragment the physical server into multiple VPS compartments.

While VPS and shared hosting might be similar in that they both involve sharing one server between multiple users, there’s a significant difference between shared hosting and VPS hosting.

What is CentOS?

CentOS (Community Enterprise Operating System) is a linux distribution that provides a free and open-source computing platform that is compatible with its upstream source. The operating system is split into two operating systems: CentOS linux and CentOS Stream.

However, Red Hat (CentOS developer) announced it would start discontinuing support for CentOS linux 7 between 2021 and 2024 to focus on CentOS Stream.

Why Should You Use CentOS for PHP Projects?

While there’s a wide variety of server operating systems you can use for PHP projects, many experts still choose CentOS. Here are some of their reasons:

  • Security: In today’s world, cybersecurity is every professional developer’s concern. CentOS has multiple inbuilt security features like Security-Enhanced Linux (SELinux). This is an access control mechanism that can enforce rules on processes and files based on rules you define.
  • Support: Although CentOS linux 7 will officially be unsupported from 2024, it was released in 2014, which gives it 10 solid years of support. CentOS Stream, on the other hand, will continue to receive support and updates into the foreseeable future.
  • Package management: CentOS uses YUM (Yellowdog Updater Modifier) to keep your system up to date. This system makes it modular, so it’s easy for you to add or remove features.

Why You Need Composer on Your CentOS 9 VPS or Dedicated Server

  • Many PHP developers prefer to use composer for a few reasons:
    composer is utilized in practically all the latest PHP platforms and frameworks including Symfony, Drupal, Magneto 2, and Laravel.
  • When the package you are using gets a new version, a composer update will configure everything without you having to do any file management.
  • Composer takes care of all your project’s dependencies, leaving you free to focus on the programming, instead of dependency management.

Requirements

Ensure that you have the following requirements before you continue with this tutorial:

  1. We recommend a fresh OS install to avoid potential problems.
  2. Php 7 installed in your CentOS Stream release 9 VPS or server.
  3. SSH access to the server.
  4. You’re logged in as a user with sudo privileges.

Step 1: Putting All the Dependencies in Place

Before you download and install composer, you have to ensure that your CentOS 9 server has all the required dependencies to help with this process. The process will be the same for both dedicated servers and VPS as they’re basically the same thing on the software end.

First, you have to update your server with dnf or yum by running the following commands:

yum update

Or

dnf update

Next, install the following dependencies:

  • Curl: This will help you download composer.
  • Php-cli: This is a dependency used to download and run composer.
  • Php-mbstring: This package will provide functions for our library.
  • Git: The composer will use it to download project dependencies.
  • Unzip: This will be used to extract the zipped packages.

To install all the dependencies above, you can run the command below:

dnf install curl PHP-cli PHP-mbstring git unzip

When the screen above appears, press Y to confirm, and the installation will begin.

That’s all! The dependencies are in place, and you’re ready for the next phase.

Step 2: Installing the Composer

There are three ways you can install the composer on your CentOS 9 dedicated server or VPS. We’ll start with the most common one.

First, you need to navigate to the home directory by executing the following command:

cd ~

The composer comes with an installer that is developed in PHP. Run the command below to download the installer:

curl -sS https://getcomposer.org/installer -o composer-setup.PHP

To authenticate this installer, download a SHA-384 hash on this page, and then copy and store this hash as a CentOS shell variable:

HASH=55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae

Alternatively, you can do it automatically by executing the following command:

HASH="$(wget -q -O - https://composer.github.io/installer.sig)"

Then, you can check if it was stored correctly with an echo.

If you have the most recent hash, run the command below to match the installer to the hash:

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 installer is authentic, you will see the following output:

However, if the installer is not authentic, you’ll get the following output:

In such a case, you’ll have to download the installer again and scrutinize the hash thoroughly to ensure it’s the latest one. Once you’re positive that the script and hash are error-free, you can run the verification again.

When the installer is validated, execute the following command to install PHP globally:

PHP composer-setup.PHP --install-dir=/usr/local/bin --filename=composer

Doing this will make composer a system executable file, including it in your system path. As a result, it will be accessible via the command line.

The process will install the PHP composer in the directory, /usr/local/bin, as a system-wide command called composer. Once it’s completed, you will get the following output:

You can now run the command below to test the installation:

composer

This command should show you the following:

  • The composer version installed.
  • A list of command options and their features.

You can also do it with a script

First, create a file called installcomposer.sh with vi:

Then, press i to enter edit mode and enter the following content:

#!/bin/bash
cd ~
dnf update -y;
dnf install curl PHP-cli PHP-mbstring git unzip -y;
curl -sS https://getcomposer.org/installer -o composer-setup.PHP;
HASH="$(wget -q -O - https://composer.github.io/installer.sig)";
PHP -r "if (hash_file('SHA384', 'composer-setup.PHP') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.PHP'); } echo PHP_EOL;";
PHP composer-setup.PHP --install-dir=/usr/local/bin --filename=composer;
composer;

Type ESC to exit edit mode, then type 😡 to save and exit.

Next, make it executable by running the following command:

Chmod 700 ./installcomposer.sh

Then execute it with the command below. It will perform all the steps automatically, including updating the server with dnf:

./installcomposer.sh

When it starts updating, you will see the output below:

It will then begin adding components as seen below:

Finally, the composer and SHA signature will be downloaded, and verification and testing will begin.

Do it with just one line

Alternatively, you can skip all the processes above and install the composer with one line, without using any script.

Enter the command below:

cd ~;dnf update -y;dnf install curl PHP-cli PHP-mbstring git unzip -y;curl -sS https://getcomposer.org/installer -o composer-setup.PHP;HASH="$(wget -q -O - https://composer.github.io/installer.sig)";PHP -r "if (hash_file('SHA384', 'composer-setup.PHP') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.PHP'); } echo PHP_EOL;";PHP composer-setup.PHP --install-dir=/usr/local/bin --filename=composer;composer;

The composer should be downloaded, verified and tested as seen below:

And that’s it! Three different methods of installing composer on your CentOS 9 VPS or dedicated server. Feel free to to try the one you think is easiest.

Conclusion

Composer is an essential tool for developing PHP projects and we recommend that you install it in your CentOS 9 VPS or dedicated server. Hopefully, with the guide above, you’re able to install it and optimize your productivity.

You can also look at how to install PHP composer on a CentOS 7 VPS or dedicated server if you’re still on CentOS 7.

Next Steps: What Now?

Here are a few useful tips you can take from this guide on installing PHP composer on a CentOS 9 VPS:

  • Composer is a PHP dependency management tool that allows you to declare the libraries your project requires, and it will manage (install and update) them for you.
  • A Virtual Private Server (VPS) is a hosting service that uses virtualization technology to give you private dedicated resources on a shared server.
  • With a VPS, you have the power of a dedicated server and the ability to manage your own services and a customizable disk space. Shared hosting, on the other hand, gives you limited administrative access and you have little control over software configurations.
  • It’s important to reinstall your CentOS before installing composer to avoid any potential issues.

Further Reading

Here are other articles related to this topic that you can use to learn more about installing PHP composer on a CentOS 9 VPS or dedicated server:

  1. How to Set up a Virtual Private Server (VPS) in 5 Steps.
  2. VPS Security: How to Keep your Data Secure?
  3. How to Schedule Tasks/Jobs in CentOS 8
  4. How To Install Python 3 On CentOS 7 Using SCL
  5. How to Install and Configure Linux Malware Detect on CentOS 7

FAQ

Still have some questions you’d like answered? Here are some of the most frequently asked questions on installing composer on a CentOS VPS.

How Do I Tell Which PHP Version Composer Is Using?

It can be confusing to tell which PHP version you’re using if you have several versions installed. Here’s how to check which version you’re using:

  1. Open the terminal and type the commands below.
  2. Login in to the remorte server using the SSH command. For instance, ssh user@linux-unix-server-ip
  3. To check the PHP version, type php –version or php-cgi –version.
  4. To print the PHP 7 version, run php7-cgi –version or php7 –version.
  5. To check PHP version, type php8-cgi –version or php8 –version.

How Do I Update Composer in CentOS 9?

To update composer itself to its latest version, you need to run the self-update command. It will replace your composer.phar with the latest version

php composer.phar self-update

If you’d like to update (or downgrade) to a specific release, you can specify the version in the command. For instance:

php composer.phar self-update 2.4.0-RC1

It’s important to note that if you’ve installed composer globally, you may have to use root privileges when running the commands above.

Who Should Use a VPS?

A VPS is like a middle ground between the cheap shared hosting with little control and expensive dedicated server with unlimited access. Of course, you don’t need a VPS if all you need to run is a small WordPress blog. However, there are many instances where a VPS is the ideal solution such as:

When your site has exceeded the architectural or resource limits of your shared hosting.

  • If you’d like to install custom modules that aren’t supported by your host.
  • If you’ll require root access to the server.
  • If you’re going to need a virtual environment for your software development and testing.
  • And many more.

If you find yourself in any of these situations, try our VPS plans and experience the freedom of a Virtual Private Server.

How To Set up a VSFTPD Server on a CentOS 7 VPS or Dedicated Server

Brief Description FTP is usually insecure exposing clear-text passwords, userna
2 min read
Avi Ilinsky
Avi Ilinsky
Hosting Expert

How To Set up a VSFTPD Server on an Ubuntu 16.04 VPS or Dedicated Server

Brief Description FTP data is usually insecure since information (usernames, pa
2 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How to use phpMyAdmin to develop a website (without MySQL experience)

Brief description A web developer who is not well versed into coding websites f
2 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Install MySQL on a Windows Web Server Running Apache

This tutorial will show you how to install the MySQL database on a Windows serve
3 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top