- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How To Install and Configure The Composer on Ubuntu 16.04
46 Lectures 3.5 hours
In this article, we will be learning about – how to configure and install the Composer, A composer is a tool which manages dependency for the PHP, which will be useful to facilitate the installation and update of the project dependencies which also show the appropriate versions required for the project requirements.
- One machine with Ubuntu 16.04.
- A non-root user with root privileges on the machine.
Installing the Dependencies
Before installing the composer, we need to update the machine with the below command –.
$ sudo apt-get update
Once the system updates, we shall proceed with the installation setup where we will be installing the packages required to run the Composer.
$ sudo apt-get install curl php-cli git -yReading 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 the Composer
We will use the installation instruction, which is given on the Composer official documentation.
Composer will be installed under /usr/local/bin.
Download the Composer using the below command in the /tmp folder.
$ sudo php -r "copy('https://getcomposer.org/installer', '/tmp/composer-setup.php');"
Once you have downloaded the script, we will install the packages using the below command, and will install the Composer in the /usr/local/bin with –install-dir flag;.
$ sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer Output: All settings correct for using Composer Downloading... Composer (version 1.4.1) is successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composer
Once the installation is completed, we will run the below command to verify the installation and version.
$ composer --version Output: Composer version 1.4.1 2017-03-10 09:29:45
Finally, we will remove the installation file from the /tmp directory with the below command.
$ rm -rf /tmp/composer-setup.php
Installing the Required Package
To use the Compose, with a project we need composer.json file which tells the composer about which dependencies are needed for the project, we need to create the composer.json file manually in the project folder.
Also make a note that, when you add the dependency to the project we need to run the required command with the composer, which will add the dependencies automatically – No needed to add them manually.
The Composer uses the process to install the packages for the project in the following steps –
It identifies what kind of libraries needed for the application.
It will search for the suitable library in the Packagist.org which is an official repository for the composer.
We can choose the right dependencies which are required for the project.
Running the composer required a command to include the dependencies in the composer.json and install the package.
Create a folder for the project and change to that project in the user parent directory.
$ cd ~$ $ mkdir demoporj $ cd demoproj
Run the below command to create the composer.json file.
$ composer require cocur/slugify Output: 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 The php.ini used by your command-line PHP is: /etc/php/7.0/cli/php.ini Now trying to download from source- Installing cocur/slugify (v2.4): Cloning f11f22d4e6 from cache Writing lock file Generating autoload files
Include the Autoload Script
The composer has a autoload script, which we can include in the project to get the autoloading which makes the dependencies and defines the own namespace. We just need to include the vendor/autoload.php in the PHP scripts before any class libraries are loaded.
For example, we need to include this which will load the autoload script and update the dependencies –.
$ vi load.php <?php require __DIR__ . '/vendor/autoload.php'; echo ('Hello World, this is a demo sentence');
Just run the below command to check the autoloader script works –.
$ php load.php Output: Hello World, this is a demo sentence
In the above article, we have learnt – how to install the Composer from the Official repository, and how to create the composer.json file and how to auto update the dependencies automatically when any new dependencies are installed.
- How to set up openvpn on ubuntu 16 04
- How to Install and Configure Nginx on Ubuntu 16.04
- How To Configure and Install Redis on Ubuntu Linux
- How to Configure and Install ownCloud on Ubuntu 16.04
- How To Install and Configure Webmin on Ubuntu 16.04
- How to install and configure puppet 4 on ubuntu 16.04
- How To Install and Configure “R” on Ubuntu 16.04
- How to Install and Configure MS SQL (Beta) on Ubuntu 16.04
- Install and Configure Elasticsearch in Ubuntu -14.04
- How To Install and Configure Sysdig to Monitor your Ubuntu 16.04
- How to Setup and Configure Redis on Ubuntu 16.04
- How to Setup and Configure Postfix on Ubuntu 16.04
- How To Configure and Setup Ghost on Ubuntu 16.04
- How to Install Bower on Ubuntu
- How to install Doxygen on Ubuntu