How To Install PHP Composer on Ubuntu 22.04

In this short tutorial we will learn how to install Composer on Ubuntu 22.04. PHP Composer is a powerful and essential tool for managing dependencies in PHP applications.

Introduction

PHP Composer is a powerful and essential tool for managing dependencies in PHP applications. It simplifies the process of installing and updating external libraries, packages, and frameworks, making it an indispensable part of modern PHP development. In this article, we’ll delve into the world of PHP Composer, exploring its core concepts, how to get started, and some best practices for effectively managing your project’s dependencies.

What is PHP Composer?

PHP Composer is a dependency management tool for PHP, enabling developers to declare the libraries, packages, and frameworks their projects depend on and then easily install and update these dependencies. Composer is often compared to other package managers like npm for Node.js or pip for Python, as it serves a similar purpose.

Core Concepts

Before diving into Composer, let’s familiarize ourselves with some core concepts:

  • Packages: Packages are the fundamental units of code that Composer manages. A package can be a library, framework, or any codebase that you want to include in your project.
  • Composer.json: This JSON file serves as the heart of your project’s configuration. It defines all your project’s dependencies, including the packages you need and their versions.
  • Composer.lock: This file stores the specific version of each package that your project uses. It ensures that everyone working on your project has consistent dependencies, eliminating compatibility issues.

How To Install PHP Composer on Ubuntu 22.04

Installing PHP Composer on Ubuntu 22.04 will be consist of several steps, namely :

Step 1: Install PHP and Additional Dependencies
– Update the package manager cache by running
– Install required packages
Step 2: Download and Install Composer

Step 1: Install PHP and Additional Dependencies

To install Composer dependencies on an Ubuntu 22.04 system, specifically the php-cli and unzip packages. We can use the following commands to install these dependencies:

Update the package manager cache by running

$ sudo apt update

Install Required Packages

To install required packages related to Composer, we will use the following command line:

$ sudo apt install php-cli unzip

Output :

ramansah@bckinfo:~$ sudo apt install php-cli unzip
[sudo] password for ramansah: 
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
unzip is already the newest version (6.0-26ubuntu3.1).
The following NEW packages will be installed:
php-cli
0 upgraded, 1 newly installed, 0 to remove and 193 not upgraded.
Need to get 3.234 B of archives.
After this operation, 25,6 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://id.archive.ubuntu.com/ubuntu jammy/main amd64 php-cli all 2:8.1+92ubuntu1 [3.234 B]
Fetched 3.234 B in 5s (613 B/s) 
Selecting previously unselected package php-cli.
(Reading database ... 285109 files and directories currently installed.)
Preparing to unpack .../php-cli_2%3a8.1+92ubuntu1_all.deb ...
Unpacking php-cli (2:8.1+92ubuntu1) ...
Setting up php-cli (2:8.1+92ubuntu1) ...
update-alternatives: using /usr/bin/php.default to provide /usr/bin/php (php) in auto mode
update-alternatives: using /usr/bin/phar.default to provide /usr/bin/phar (phar) in auto mode
update-alternatives: using /usr/bin/phar.phar.default to provide /usr/bin/phar.phar (phar.phar) in auto mode
Processing triggers for man-db (2.10.2-1) ...

Step 2: Download and Install Composer

To install Composer on Ubuntu 22.04, we will download it from Composer official website. The installer is written in PHP, then we will download it by using curl command and verify it with matches the SHA-384 hash for the latest installer found on the Composer Public Keys / Signatures page.

$ curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
$ HASH=`curl -sS https://composer.github.io/installer.sig`
$ echo $HASH

Output :

ramansah@bckinfo:/tmp$ curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
ramansah@bckinfo:/tmp$ HASH=`curl -sS https://composer.github.io/installer.sig`
ramansah@bckinfo:/tmp$ echo $HASH
e21205b207c3ff031906575712edab6f13eb0b361f2085f1f1237b7126d785e826a450292b6cfd1d64d92e6563bbde02
ramansah@bckinfo:/tmp$ php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Installer verified

We will install Composer under /usr/local/bin as a system-wide globally. For this purpose we will submit the following command line :

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

Output :

ramansah@bckinfo:/tmp$ sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
[sudo] password for ramansah: 
All settings correct for using Composer
Downloading...

Composer (version 2.6.5) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

install Composer

To test the Composer, we will submit the folowing command :

ramansah@bckinfo:/tmp$ composer
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 2.6.5 2023-10-06 10:11:52

Usage:
command [options] [arguments]

Options:
-h, --help Display help for the given command. When no command is given display help for the list command
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi|--no-ansi Force (or disable --no-ansi) ANSI output
-n, --no-interaction Do not ask any interactive question
--profile Display timing and memory usage information
--no-plugins Whether to disable plugins.
--no-scripts Skips the execution of all scripts defined in composer.json file.
-d, --working-dir=WORKING-DIR If specified, use the given directory as working directory.
--no-cache Prevent use of the cache
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
about Shows a short information about Composer
archive Creates an archive of this composer package
audit Checks for security vulnerability advisories for installed packages
browse [home] Opens the package's repository URL or homepage in your browser
bump Increases the lower limit of your composer.json requirements to the currently installed versions
check-platform-reqs Check that platform requirements are satisfied
clear-cache [clearcache|cc] Clears composer's internal package cache
completion Dump the shell completion script
config Sets config options
create-project Creates new project from a package into given directory
depends [why] Shows which packages cause the given package to be installed
diagnose Diagnoses the system to identify common errors
dump-autoload [dumpautoload] Dumps the autoloader
exec Executes a vendored binary/script
fund Discover how to help fund the maintenance of your dependencies
global Allows running commands in the global composer dir ($COMPOSER_HOME)
help Display help for a command
init Creates a basic composer.json file in current directory
install [i] Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json
licenses Shows information about licenses of dependencies
list List commands
outdated Shows a list of installed packages that have updates available, including their latest version
prohibits [why-not] Shows which packages prevent the given package from being installed
reinstall Uninstalls and reinstalls the given package names
remove [rm] Removes a package from the require or require-dev
require [r] Adds required packages to your composer.json and installs them
run-script [run] Runs the scripts defined in composer.json
search Searches for packages
self-update [selfupdate] Updates composer.phar to the latest version
show [info] Shows information about packages
status Shows a list of locally modified packages
suggests Shows package suggestions
update [u|upgrade] Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file
validate Validates a composer.json and composer.lock

install php composer on Ubuntu 22.04

Until this step, we have installed PHP Composer successfully on Ubuntu 22.04 operating system.

Conclusion

PHP Composer is an invaluable tool for PHP developers. It simplifies the management of dependencies, making it easier to build and maintain PHP projects. By understanding the core concepts, following best practices, and integrating Composer into your workflow, you can streamline the process of working with PHP packages and ensure the stability and scalability of your applications. Whether you’re developing a small website or a complex web application, Composer is a must-have tool in your PHP development toolkit.

 

(Visited 69 times, 1 visits today)

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *