Atlantic.Net Blog

How to Install and Use Composer on Oracle Linux 8

Composer is a dependency manager for PHP. It was created by Nils Adermann and Jordi Boggiano and was initially published on 1 March 2012. It allows you to download, install, and loads dependencies that you have specified in the composer.json file. It helps developers to develop a project inside a specific framework. It is a very useful tool for installing third-party libraries and integrating them into their PHP-based projects.

In this tutorial, we will show you how to install and use Composer on Oracle Linux 8 server.

Step 1 – Install PHP and Other Dependencies

Before installing Composer, you will need to install PHP and other required dependencies on your server.

You can install PHP with other required dependencies with the following command:

dnf install php php-cli php-mbstring curl gnupg2 git unzip -y

After installing all the packages, you can proceed to install the Composer.

Step 2 – Install Composer

Composer provides a php-based installation script to install it on your system.

First, download the Composer installation script with the following command:

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

Next, you will need to verify whether the downloaded script matches the SHA-384 hash for the latest installer found on the Composer signature page. To do so, copy the hash from that page and store it in the shell variable.

HASH=55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae

Next, run the following command to verify the downloaded script:

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 everything is fine, you should get the following output:

Installer verified

Next, run the following script to install Composer globally:

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

Once the installation has been completed, you should get the following output:

All settings correct for using Composer
Downloading...

Composer (version 2.4.1) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

You can now check the Composer version using the following command:

composer --version

You should see the following output:

Composer version 2.4.1 2022-08-20 11:44:50

Step 3 – Working with Composer

Composer is now installed on your server. You can now create a project with Composer.

First, create a new directory for your project:

mkdir composerapp

Next, change the directory to composerapp:

cd composerapp

Next, create a PHP-based application that prints the current time.

composer require nesbot/carbon

The above command will download and install carbon with all required dependencies and creates the composer.json file:

  - Installing symfony/polyfill-php80 (v1.26.0): Extracting archive
  - Installing symfony/polyfill-mbstring (v1.26.0): Extracting archive
  - Installing symfony/deprecation-contracts (v2.5.2): Extracting archive
  - Installing symfony/translation (v5.4.11): Extracting archive
  - Installing nesbot/carbon (2.61.0): Extracting archive
3 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
6 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
No security vulnerability advisories found

Run the following command to verify all files created by Composer:

ls -l

You should see the following output:

-rw-r--r-- 1 root root    60 Aug 23 06:31 composer.json
-rw-r--r-- 1 root root 18911 Aug 23 06:31 composer.lock
drwxr-xr-x 6 root root    82 Aug 23 06:31 vendor

Now, create a new file named app.php:

nano app.php

Add the following code:

<?php

require __DIR__ . '/vendor/autoload.php';

use Carbon\Carbon;

printf("Now: %s", Carbon::now());
?>

Save and close the file, then run the app.php with the following command:

php app.php

This will print the current time of your system:

Now: 2022-08-23 10:33:02

If you want to update the project, run the following command:

composer update

This will check for newer versions of the installed packages, match them with all packages inside composer.json, then update the package.

Conclusion

In the above post, you learned how to install Composer on Oracle Linux 8. You also learned how to create a PHP-based project with Composer. For more information, visit the Composer official documentation. Try it today on VPS hosting from Atlantic.Net!

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year