Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How To Install and Configure The Composer on Ubuntu 16.04
In this article, we will learn how to install and configure Composer, a dependency management tool for PHP. Composer facilitates the installation and update of project dependencies while ensuring appropriate versions are maintained for project requirements.
Prerequisites
Ubuntu 16.04 machine
Non-root user with sudo privileges
Installing Dependencies
Before installing Composer, update your system packages:
sudo apt-get update
Install the required packages to run Composer:
sudo apt-get install curl php-cli git -y
Reading package lists... Done Building dependency tree Reading state information... Done git is already the newest version (1:2.7.4-0ubuntu1). curl is already the newest version (7.47.0-1ubuntu2.2). The following NEW packages will be installed: php-cli 0 upgraded, 1 newly installed, 0 to remove and 112 not upgraded. Need to get 2,920 B of archives. After this operation, 11.3 kB of additional disk space will be used. Get:1 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 php-cli all 1:7.0+35ubuntu6 [2,920 B] Fetched 2,920 B in 0s (9,032 B/s) Selecting previously unselected package php-cli. (Reading database ... 92686 files and directories currently installed.) Preparing to unpack .../php-cli_1%3a7.0+35ubuntu6_all.deb ... Unpacking php-cli (1:7.0+35ubuntu6) ... Setting up php-cli (1:7.0+35ubuntu6) ...
Installing Composer
Download the Composer installer script to the /tmp directory:
sudo php -r "copy('https://getcomposer.org/installer', '/tmp/composer-setup.php');"
Install Composer globally in /usr/local/bin:
sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
All settings correct for using Composer Downloading... Composer (version 1.4.1) successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composer
Verify the installation:
composer --version
Composer version 1.4.1 2017-03-10 09:29:45
Clean up by removing the installer file:
rm -rf /tmp/composer-setup.php
How Composer Works
Composer manages project dependencies through the following process:
Identifies required libraries for the application
Searches for suitable packages in Packagist.org (the official Composer repository)
Resolves compatible dependency versions
Updates
composer.jsonand installs packages automatically
Installing Packages
Create a project directory and navigate to it:
cd ~ mkdir demoproj cd demoproj
Install a package using Composer. This example installs the cocur/slugify library:
composer require cocur/slugify
Using version ^2.4 for cocur/slugify ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Installing cocur/slugify (v2.4): Cloning f11f22d4e6 from cache Writing lock file Generating autoload files
Using the Autoloader
Composer generates an autoload script that handles class loading automatically. Include vendor/autoload.php in your PHP scripts:
<?php
require __DIR__ . '/vendor/autoload.php';
// Now you can use installed packages
use Cocur\Slugify\Slugify;
$slugify = new Slugify();
echo $slugify->slugify('Hello World');
Test the autoloader by creating a simple script:
echo "<?php require __DIR__ . '/vendor/autoload.php'; echo 'Hello World, Composer is working!';" > test.php php test.php
Hello World, Composer is working!
Conclusion
You have successfully installed Composer on Ubuntu 16.04 and learned how to manage PHP dependencies. Composer automatically handles package installation, version resolution, and autoloading, making PHP project management significantly easier and more reliable.
