Wednesday, November 20, 2024
HomeTutorialsHow to Install and Use PHP Composer on Linux Distributions

How to Install and Use PHP Composer on Linux Distributions

The 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 Ubuntu Linux


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

install library on ubuntu

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
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

cURL download from getcomposer

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 on Debian

2. Install PHP Composer on Fedora and RedHat Linux


Before installing the PHP composer on your Red Hat-based Linux system, you must ensure that the PHP framework is installed inside your system. Otherwise, you won’t be able to edit and configure the Composer for the PHP framework. There are many ways to install the framework on RedHat based Linux systems. We will follow the cURL method to get the PHP installed inside our system.

Fedora, Red Hat Linux, and CentOS users can get the PHP composer on their system by running the following cURL command to download the PHP composer.

curl -sS https://getcomposer.org/installer | php

fedora PHP Composer install

Now use the following MV command to move the composer file inside the /usr/local/bin/composer directory.

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

Then run the change mode (chmod) command on your Linux terminal to access the composer directory on your Linux filesystem.

chmod +x /usr/local/bin/composer

Finally, you are good to go. Now update and finish the installation of the PHP composer on your Red Hat-based Linux system.

composer self-update

After everything is done correctly, you can now check the PHP composer’s version to ensure that everything works perfectly.

PHP Composer install

3. Install PHP Composer on Arch Linux


Arch and Arch-based Linux users can install the PHP framework inside their system by installing the Apache server or installing the PHP framework directly from GitHub. After the PHP framework is installed inside your system, you can now initialize the process of installing the PHP composer on your Arch Linux.

In this case, we are using Manjaro as an Arch distribution. All the commands that I am going show will be executable on other Arch Linux systems too. First, you have to make sure that the PHP framework is installed correctly. If you don’t have the PHP installed inside your system, you can run the following command-lines on your terminal shell to install PHP on your Arch Linux system.

sudo pacman -Syu
sudo pacman -Syu php php-apache

Now, as the PHP framework is installed successfully, now, we have to edit the php.ini file to configure the PHP system. Run the following command-line given below on your terminal shell to edit the php.ini file.

sudo nano /etc/php/php.ini

Now, find the following script lines given below and uncomment them by removing the semicolon ; before the lines.

extension=openssl.so
extension=phar.so

Nano edit on composer

Now, you have to look for the open_basedir syntax inside the PHP script, then paste the following line given below at the bottom of the script. Then save and exit the script.

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

After the configuration is done, you can now initialize the process of installing the PHP composer on your Arch Linux. Run the following cURL command to download the composer script from the getcomposer.org website. Then, run the Pacman command to install the PHP composer on your Arch Linux.

curl -sS https://getcomposer.org/installer | php
sudo pacman -S composer

PHP Composer install on manjaro

Now, move the composer script inside the /usr/local/bin/composer directory. If you can’t find any directory like this, you can use the mkdir command to create one.

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

Now run the system control command to restart the PHP transfer protocol (httpd) on your Linux system.

sudo systemctl restart httpd

Finally, you can now run the self-update command on your Arch Linux terminal shell to update your system’s PHP composer.

sudo composer self-update

If you are more curious about the PHP composer on Arch Linux, you can also find the PHP composer on the Arch Linux-Wiki. You can then download and install the PHP composer manually on your Arch Linux system manually as well.

Get started with 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 ubuntupit && cd ubuntupit
composer install

PHP 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.

Extra TIP: Skip the Conflict with the Laravel


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 https://packagist.org

Final Words


If you are not a hardcore PHP developer, you probably don’t need the PHP composer installed inside your Linux system. But it is a widespread decision to get the PHP composer on your system. Moreover, installing the composer can boost your efficiency in the development of the PHP framework.

Those working with the PHP framework might know that the composer will be part and parcel of the PHP ecosystem in the future. The question is, instead of having the PR package manager for the PHP framework, why should we use the PHP composer? Actually, I have already told in this post that the composer is not only a package manager; it’s an entire manager for the PHP framework’s dependencies. In this post, I have described how you can install the PHP composer on various Linux systems.

Please share this post with friends and the Linux community if you find this post useful and handy. You can ask any questions regarding this post in the comment section.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is a passionate enthusiast for technology. He admires all things tech and loves to help others understand the fundamentals of Linux, servers, networking, and computer security in an understandable way without overwhelming beginners. His articles are carefully crafted with this goal in mind - making complex topics more accessible.

You May Like It!

Trending Now