PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
No Result
View All Result
Home Tutorial

How to install Composer on Linux

In-depth tutorial on how to install Composer on Linux-based operating systems.

March 8, 2022
Reading Time: 5 mins read
PHP101.net - Tutorial - Install Composer on Linux

PHP101.net - Tutorial - Install Composer on Linux

Share on FacebookShare on Twitter

Table of Contents

    • Introduction
    • What is Composer and what does it do?
    • Install Composer on Linux – Step by step
    • 1. System requirements for Composer on Linux
    • 2. Install Composer on Linux-based OS
    • 4. Verify Composer installation
    • 5. Update Composer on Linux
    • Example Usage
    • Final thoughts
    • You might also like
    • References

Don’t like the intro? Skip to the Tutorial (install Composer on Linux) here.

Introduction

Back then with PHP development, you would want to use other PHP libraries to include into your project. The process is usually pretty time-consuming:

  • First, download the zipped PHP library file;
  • Then extract it into the project directory;
  • Use require_once function to include the library to the project;
  • Upload the whole project with the library files when deploying it online;

Not to mention that, when a library gets an update, you have to re-download it, and overwrite the files again, which is very unproductive.

Composer, a popular PHP dependency manager, helps solving these issues real easy and effective.

What is Composer and what does it do?

As the above short brief on what Composer can do, it is a PHP dependency manager. As a dependency tool:

  • Composer helps install, include, update PHP dependency libraries in a project;
  • Composer can declare dependencies in a PHP package/project easily;

Not only can Composer can handle a project’s dependency on external libraries, it can handle libraries depend on other libraries as well, which is extremely helpful.

As Official Composer Introduction page, Composer can:

  1. Enables you to declare the libraries you depend on.
  2. Finds out which versions of which packages can and need to be installed, and installs them (meaning it downloads them into your project).
  3. You can update all your dependencies in one command.

In short, Composer is a must-use tool if you plan to develop with PHP.

Composer supports Windows, Linux and MacOS. This in-depth article will guide you on how to install Composer on Linux-based operating systems.

Tutorial StartsInstall Composer on Linux – Step by step

1. System requirements for Composer on Linux

Composer has general system requirements on Linux-based operating systems.

  • You must have at least PHP 7.2.5 installed
  • php.ini settings should be properly set. If you install PHP using general methods as dnf, yum, apt-get, it should work fine.

It is possible to use old Composer versions for legacy PHP versions, but at least it has to be PHP 5.3.2. Although so, it is recommended to use a newer PHP version for better compatibilities, as not many PHP libraries use older PHP versions anymore.

2. Install Composer on Linux-based OS

Quick Solution: From the Official Composer Download page, you just need to run these commands (either line-by-line or all at once works) in Linux terminal:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

*These commands will be updated as Composer updates, however, it’s better to get the latest commands from, again, the Official Composer Download page, as they might be updated making the hashes on this page obsolete.

The page also explains how these command works in details, so we will just quote them here:

This installer script will simply check some php.ini settings, warn you if they are set incorrectly, and then download the latest composer.phar in the current directory. The 4 lines above will, in order:

  • Download the installer to the current directory
  • Verify the installer SHA-384, which you can also cross-check here
  • Run the installer
  • Remove the installer

After finished running the commands, copy the composer.phar into your Linux’s $PATH directory, so it can be called globally:

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

Quick tips: To view your $PATH directory, run this command:

echo $PATH

The system will return a number of global paths that you can copy the composer.phar file to. For example:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

After successfully copying the composer.phar file, the installation can be considered completed. We will verify if Composer works in the next step.

4. Verify Composer installation

To check if Composer is probably installed, open Linux terminal and run this command:

composer --version

Alternatively, composer -V (V in capitalized) can be used. If you see the similar message telling Composer version, it has been successfully installed:

[[email protected] ~]# composer --version
Composer version 2.1.8 2021-09-15 13:55:14

If the command returns “not found” error, check if you have copied the composer.phar to the correct $PATH directory on your Linux system. See the Quick tips on the previous section on how to locate it.

5. Update Composer on Linux

To update Composer on Linux, you can follow the above steps again to download the latest Composer version and replace it with your current version.

Example Usage

This quick example will show you on how to use Composer.

Let’s say we have a PHP project that needs URL slug creation functionality, which turns words into a slug URL for SEO purposes (eg. an example project to an-example-project). Instead of having to write this functionality from scratch, we will use a great PHP package, which is ausi/slug-generator library by GitHub user ausi.

On the root project folder, enter this command in Linux terminal:

composer require ausi/slug-generator

This command will tell Composer to:

  • Download the PHP package ausi/slug-generator located on Packagist.org (main Composer package repository)
  • Copy it into our project directory under the vendor folder,
  • Record dependency information into composer.json file
  • Record the package’s version, time and hashes into composer.lock file to keep track of its states
  • Generate necessary dependency inclusion into vendor\autoload.php file

Compared to downloading the package manually and copying it into the project folder, this is very efficient. To use the package, all we need is including the autoload.php file into the project, as this example:

<?php

// Include Composer vendor files
require_once('./vendor/autoload.php');

// Include the Slug library
use Ausi\SlugGenerator\SlugGenerator;

// Start using it!
$generator = new SlugGenerator;
echo $generator->generate('php101.net Install Composer on Linux example'); 

// Output: php101-net-install-composer-on-linux-example

That’s how Composer works. Extremely convenient.

Final thoughts

The tutorial on how to install Composer on Linux for PHP dependency management is now over. As a PHP developer, using Composer is a must to keep your project clean and maintain good PHP packages dependency.

We will constantly update this article as new information releases. Thanks for reading, and hopefully this tutorial is helpful for your PHP development process.

You might also like

  • 3 ways to get PHP memory_limit value
  • How to write PHP comments (and best practices)

References

  • Composer official website
  • Packagist.org for PHP packages
Tags: composer on centoscomposer on linuxcomposer on ubuntuinstall composerphp composer
Previous Post

How to install Composer on Windows

Next Post

How to download file from URL using PHP

Related Posts

PHP101.Net - Tutorial - Change Laragon PHP version on Windows

How to change Laragon PHP version on Windows

September 21, 2022
PHP101.Net - Tutorial - Install ImageMagick on Windows

How to install ImageMagick on Windows

September 9, 2022
Next Post
PHP101.net - How to download file from URL using PHP

How to download file from URL using PHP

We deliver free and helpful resources for learning
and using PHP programming language.

About
Contact Us
Privacy Policy
Main Sections

Home
Deploy
How-to
Tutorial
You might be interested in...

How to download file from URL using PHP

How to count array elements in PHP

How to redirect to another page using PHP

How to remove empty array elements using PHP

How to Add Elements to Array in PHP Easily

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone.

No Result
View All Result
  • Home
  • Deploy
  • How-to
  • Tutorial

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone. | Privacy Policy

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.