How to Install PHP Composer On Linux


PHP composer is a dependency manager of the PHP framework that you can use on your Linux system to install PHP modules, applications, and packages. The PHP composer is a command-line based tool for Linux. If you are a programmer, you might know that different languages use different package dependency managers to install and update modules inside the system. Like, Python uses Pip, RUBY uses Bundler; in the same way, you can use the composer tool to install the PHP modules and packages on your system.

PHP Composer on Linux

As PHP is a well-known server-side script language, we can use the PHP framework to autoload many client-side scripts, classes, functions, and codes via the PHP composer into the system. Nowadays, the PHP framework is mostly used to build dynamic websites and servers. To run the PHP composer into your system, you must ensure that the PHP framework is upgraded to PHP 5.3.2+.

Drupal, Laravel, WordPress, and much other famous content managing software use the PHP composer to reduce the workload and make the interface user friendly. PHP developers often need to work on large projects where they need to deal with many functions and classes; it is time killing to load packages manually. In that case, you can use the PHP composer as a dependency manager to automate some tasks. Before installing the PHP composer, you must ensure that the PHP framework is installed inside your system.

How Does PHP Composer Work?

Two elements mainly rule the PHP composer; they are the composer.json and composer.lock. The composer.json handles the dependency and packages and the composer.lock handles the installation processes. The composer.lock also keeps the logs of package version, state, and other detailed PHP packages’ information.

As a developer, you have to make sure that the composer.lock is not missing while installing the PHP composer on your Linux system. The composer.json is the destination path of PHP libraries. It automatically provides an autoload file to load the requested library functions when we run the composer tool. The PHP composer automatically looks for the perfect version of packages and automatically updates all your PHP packages.

1. Install PHP Composer on Linux Mint/Ubuntu

Ubuntu runs most of the servers and websites in the world. And, no doubt, PHP is the most used framework to build any website. So, installing a PHP composer on Ubuntu Linux can give you some spare time by reducing and automate the workload.

Installing the PHP composer on Ubuntu and other Debian distributions are pretty simple. You can install the PHP composer by using the aptitude package manager. Here, we will install the PHP-GD, PHP-XML, PHP-CLI, and the PHP-ZIP tools in our Ubuntu system. You can run the following command-lines given below to install the PHP packages on your Debian system.

sudo apt update 
sudo apt install php php-gd php-xml php-cli php-zip 
sudo apt install unzip curl

You can now check the PHP framework version to ensure that the PHP and its dependencies are installed inside your system.

php -v

Now, run the following cURL command to download the PHP composer tool on your Linux system. Then move the file into the /usr/local/bin/composer directory. If you cant find a composer directory, you can make a directory by using the mkdir commands.

sudo apt-get install curl 
sudo curl -sS | php 
sudo mv composer.phar /usr/local/bin/composer

Now, run the following change mode (chmod) command given below to get the composer directory’s root access.

chmod +x /usr/local/bin/composer

Finally, you can now check the version of the PHP composer to check whether it’s installed successfully on your Linux system or not.

composer -v

PHP Composer

Till now, we have successfully installed the PHP composer inside our Linux system. It’s time to take a test drive. Suppose you want to install a PHP package dependency through the composer; in that case, you can make a directory or select any other directories as the destination folder. Then, run the following composer command in your terminal shell to install the PHP package dependency.

mkdir shell && cd shell
composer install

Let we want to see the read-write logs of our PHP project (psr/log); in that case, we can run the following composer command to view the log files.

composer require psr/log

We know that the PHP composer keeps the PHP modules updated regularly, but it’s also essential to update the composer tool on a regular basis. Use the following command-line given below to update your PHP composer on Linux.

sudo composer self-update

I have mentioned earlier that two scripts are used to update and install the PHP composer; they are the composer.json and the composer.lock. Here is the explanation, the composer.json is used to update all the installed PHP package dependencies inside your system via the PHP composer. And the composer.lock is used to install new package dependencies, modules, or packages on your PHP framework.

If you are a professional PHP developer, you are probably using the Laravel inside your Linux system. Laravel is a perfect tool to work with PHP. But sometimes Laravel makes the composer slower. You can bypass and avoid the Laravel vs. Composer conflict by running the following command-line given below in your Linux terminal shell.

composer config --global repo.packagist composer