How to install Composer on Ubuntu 22.04 | 20.04 LTS

Learn the commands to install and use the PHP Composer Package Manager 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 packages.

What is PHP Composer?

Composer is an application-oriented package manager for PHP released under an open source MIT license. It is intended to load various dependencies required by a project developed in PHP programming language to be configured. The main advantage of Composer is that you can update these dependencies automatically, and you don’t have to deliver these files with your PHP code, as these are uploaded back to the system being used with the appropriate command. Available PHP applications can be searched through the “Packagist” platform. This is a command line that can be easily installed on Linux, macOS, and Windows.

Additionally, using a Composer autoloader, a provider can load all packages from the folder. Of course, you can add your packages to the autoloader or run all your code through the Composer autoloader.

Why do I need a composer?

Well, a lot of PHP applications need external packages to function properly. And Composer is a shortcut to automatically getting all of these packages into one rather than manually installing all of them. For example, a forum project called Flarum is an open source PHP-based forum web platform. While we can clone files directly, we have to run them from its Github repository, however, installing the required dependencies and updating them in the future might be a problem. To mitigate this, we can use Composer with its command to update packages directly, if needed, in the future.

Install the PHP Composer package manager on Ubuntu 22.04 or 22.04 Linux

The command given below is not limited to Ubuntu Jammy jellyfish or Focal fossa, you can use them for Linux Mint, Debian 11 or 10 and other similar Linux systems.

Installation with the package manager on Linux

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. Therefore, we have to manually download it from its Github repository for further configuration.

Step 1: Run the APT update

The first command we should use is to run the update system command. Let’s run the given one.

sudo apt update

Step 2: Install PHP and cURL

Then the items that should be on your computer are PHP and cURL. So for that use the given commands to install both.

sudo apt install curl unzip
sudo apt install php php-curl

Step 3: Download Composer on Ubuntu 22.04 or 22.04

As we know the composer to install is not available through Ubuntu 22.04 or 20.04 LTS by default, so use the cURL command:

curl -sS -o composer-setup.php

Step 4: Use PHP to install Composer

Now we can use PHP to install the composer setup we downloaded above while declaring the directory where we want to install it.

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

Step 5: Check the composer version

Once you finish the setup by running the above commands, we can check its version.

composer -V

To see the different options available for using Composer, run:

composer -h

How do I install packages with Composer?

The given command syntax can be used to install any package.

composer require package-name

For example install a brick/math package using composer, run:

composer require brick/math

Composer requires a package order

Alternately, we can use the Composer.json file to install multiple packages. For example:

nano composer.json
"require": {
"brick/math": "^0.9.3",
"monolog/monolog": "1.0.*"

Save the file, Ctrl + O hit the Enter key, and to quit use Ctrl + X. After this race:

composer update

How to update the composer

In the future, to update it, we can run the package manager itself to get the latest version available through its repository.

composer self-update

Automatic charger

The Composer also creates a file vendor/autoloader.php . You can simply include it in the PHP code of the project and receive an autoloader. For example, a common startup file in a PHP based web project is index.php. Just include the given row in that.

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

This means that the classes can easily be used with any existing packages, they are loaded by the autoloader when needed.

End Notes:

Composer and other package managers like NPM are useful when developing projects and installing them later. Therefore, in this way we can install Composer on Ubuntu 22.04 Jammy or 20.04 Focal fossa using the command terminal.

Other articles:

Set the root user password in Ubuntu 20.04 | 22.04 LTS focal pit
How to install Node js and NPM on Debian 11 Bullseye
3 ways to install Inkscape on Ubuntu 20.04 | 04.22 …

Source link

Steven L. Nielsen