Learn the commands to install and use PHP package manager Composer on Ubuntu 22.04 Jammy or Ubuntu 20.04 Focal fossa. Composer can be used to manage your packages, download new packages, and update existing ones.
What is PHP Composer?
Composer is an application-oriented package manager for PHP distributed under an open-source MIT license. It is meant to load various dependencies required by a project developed in PHP programming language to get set up. The key advantage of Composer is that you can update these dependencies automatically and do not have to deliver these files with your PHP code, as these are downloaded again on the system used with the appropriate command. Available PHP applications can be searched via the “Packagist” platform. It is a command-line that can be easily installed on Linux, macOS, and Windows.
Furthermore, with the help of an autoloader of Composer, a vendor can load all packages from the folder. Of course, you can add your packages to the autoloader, or you can run all of your code through the Composer autoloader.
What do I need a Composer for?
Well, many PHP applications need some external packages to run properly. And the Composer is a shortcut to get all those packages in one short automatically rather than installing each required one manually. For example- a Forum project called Flarum is a PHP-based open-source Forum web platform. Although we can clone files directly we are required to run from its Github repository, however, installing required dependencies and updating them in the future could be a problem. To mitigate that we can use Composer along with its command to update the packages directly, if required, in the future.
The command given below are not just limited to Ubuntu Jammy jellyfish or Focal fossa, you can use them for Linux Mint, Debian 11 or 10, and other similar Linux systems.
Well, unfortunately, the latest version of Composer is not available through the popular package manager such as APT for (Debian or Ubuntu) and Yum/DNF for Redhat-based operating systems. Hence, we have to download it manually from its Github repository for further configuration.
The first thing command which we should go for is running the system update command. Let’s run the given one.
Next, the things that must be on your computer are PHP and cURL. So, for that use the given commands to install both.
As we know the Composer to install is not available via the default Ubuntu 22.04 or 20.04 LTS, hence use the cURL command:
Now, we can use PHP to install the composer setup we have downloaded above while declaring the directory where we want to install it.
Once you are done with the setting up by running the commands given above, we can check its version.
To see the various options available to use Composer, run:
The given command syntax can be used to install any packages.
For example to install a
brick/math package using composer, run:
Alternatively, we can use the Composer.json file to install multiple packages. For example:
Save the file, Ctrl+O hit the Enter key, and to exit use Ctrl+X. After that run:
In the future to update it, we can run the package manager itself for getting any latest version available through its repository.
The Composer also creates a file
vendor/autoloader.php . You can simply include this in the PHP code of the project and receive an autoloader. For example, a common start file in a web project based on PHP is index.php. Just include the given line in that.
This means that the classes can easily be used with all existing packages, they are loaded by the autoloader when they are needed.
Composer and other package managers such as NPM are helpful while developing projects and later installing them as well. Hence, in this way, we can install Composer on Ubuntu 22.04 Jammy or 20.04 Focal fossa using the commands terminal.
• Set root user password in Ubuntu 20.04 | 22.04 LTS focal fossa
• How to install Node js & NPM on Debian 11 Bullseye
• 3 Ways to install Inkscape on Ubuntu 20.04 | 22.04…
AlmaLinux, Rocky Linux
document.getElementById( “ak_js_1” ).setAttribute( “value”, ( new Date() ).getTime() );
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Linux Shout crafted as a part of our main portal H2S Media to publish Linux related tutorials and open source Software.