How to install Composer on Ubuntu 22.04 | 20.04 LTS

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.

Install Composer PHP package manager on Ubuntu 22.04 or 22.04 Linux

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.

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

Step 1: Run APT Update

The first thing command which we should go for is running the system update command. Let’s run the given one.

sudo apt update

 

Step 2: Install PHP and cURL

Next, the things that must 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 via the default Ubuntu 22.04 or 20.04 LTS, hence use the cURL command:

curl -sS https://getcomposer.org/installer -o composer-setup.php

 

Step 4: Use the PHP to install Composer

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

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

Composer check version on Ubuntu 20.04 or 22.04

 

Step 5: Check Composer Version

Once you are done with the setting up by running the commands given above, we can check its version.

composer -V

To see the various options available to use Composer, run:

composer -h

composer installation on Ubuntu 22.04 or 20.04

 

How to install packages with Composer?

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

composer require package-name

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

composer require brick/math

composer require package command

Alternatively, 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 exit use Ctrl+X. After that run:

composer update

 

How to Update Composer

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

composer self-update

 

Autoloader

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.

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

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

Ending notes:

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.

 

Other Articles:

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…

 

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.