Introduction
Composer allows you to obtain all required PHP packages your project depends on and manages these packages for you. This is why it is a great application for tracking dependencies of a project.
With Composer, you can define a set of libraries required by a particular project. After establishing the libraries, the software identifies the versions and dependencies, installing them to the corresponding project.
In this tutorial, learn how to install and use Composer on Ubuntu 16.04.
Prerequisites
- An Ubuntu 16.04 Linux system, Note Ubuntu 16.04 end of life is April 2021
- PHP 5.3.2 or later installed on your Ubuntu System
- A user account with sudo privileges
- Access to a command line/terminal window (Ctrl+Alt+T)
Install Composer on Ubuntu 16.04
Step 1: Updating Local Repository
As before any installation, make sure to update the local repository to avoid downloading outdated packages.
Use the following command to ensure packages are up-to-date:
sudo apt-get update
Step 2: Installing Software Dependencies
Start by installing the supporting software needed for Composer to work correctly. You may have some of these programs on the system already. Still, we recommend running the entire command line for installing all software dependencies, just in case:
sudo apt-get install curl php-cli php-mbstring git unzip
If you already have the required dependencies installed, make sure they are the latest versions of their respective software packages.
Step 3: Downloading Composer Installer Script
Next, download the installer script.
The following command downloads the file in the directory you are currently in:
curl -sS https://getcomposer.org/installer -o composer-setup.php
To verify the download was successful, use the dir
command to list files and folders in the working directory. You should see the composer-setup.php
file, confirming the successful download.
Note: If you want to move to the home directory before downloading the installer, use the command: cd
Step 4: Verifying the Integrity of the Download
Next, check if the Installer Signature (SHA-384) found on the official Composer Public Keys page matches the SHA-384 hash.
To do so, use the command:
php -r "if (hash_file('sha384', 'composer-setup.php') === 'a5c698ffe4b8e849a443b120cd5ba38043260d5c4023dbf93e1558871f1f07f58274fc6f4c93bcfd858c6bd0775cd8d1') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
If the two signatures match, you should see the following message: Installer verified.
If the script finds differences between the two signatures, the output displayed reads: Installer corrupt.
To resolve this issue, re-download the Composer Installer.
Step 5: Installing Composer
Once the installation script has been verified, proceed to installing Composer.
Make sure to install Composer in the /usr/local/bin directory so it is accessible from the entire system.
1. To globally install Composer on Ubuntu 16.04, use the command:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Once the installation has been initialized, the following message will appear:
All settings correct for using Composer
Downloading...
Composer (version 1.9.0) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer
2. Verify Composer is running correctly with the command:
composer
The system should display the running version, along with its syntax and available options:
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ // __ `__ // __ // __ // ___/ _ // ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
/____//____/_/ /_/ /_/ .___//____/____//___/_/
/_/
Composer version 1.9.0 2019-08-28 11:45:23
Usage:
command [options] [arguments]
Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--profile Display timing and memory usage information
--no-plugins Whether to disable plugins.
3. Finally, as you will not need the installer anymore, delete it with the command:
php –r "unlink('composer-setup.php');"
Basic Composer Usage Tutorial
Composer accounts for the required software and tracks dependencies on a per-project basis. Also, it can manage multiple versions by using a composer.json file allowing other users to create an identical environment.
If a directory is copied through the composer.lock files, they are automatically generated using the require
command, maintaining consistency.
Below you will find an example of how to utilize Composer in Ubuntu 16.04 when starting a new PHP project.
1. First, create a project root directory for a file with the project description, its dependencies, and other additional information (the composer.json file):
mkdir c_sample
2. Next, move in to the new directory:
cd c_sample
3. Once the project directory has been created, you can load packages to it. To see a list of available packages, visit the packagist.org website.
For instance, we can download and use the monolog/monolog package as a test project. The information you see after the slash is the package name, while the text before the slash is the vendor.
Your Ubuntu 16.04 system will automatically create the composer.json file and the composer.lock file while downloading the package with this command:
composer require monolog/monolog
4. Verify if all the files are present by listing the content of the directory:
dir
In the output, you should see the composer.json and composer.lock files, along with a vendor directory.
5. Next, open the composer.json file:
cat composer.json
In the file, there should be the newly added monolog software with a caret sign (^) beside the version number, indicating the minimum version of the software.
How to Set Up Autoloading PHP Classes with Composer
By configuring Composer to autoload classes, you can simplify working with dependencies.
1. With any Linux text editor, create a new file (in this example, it will be under the name composer_sample
):
nano composer_sample.php
2. Add the following into the file:
<?php
require __DIR__ . '/vendor/autoload.php';
use Monolog/Logger;
use Monolog/Handler/StreamHandler;
// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('/~/c_sample/text.log', Logger::WARNING));
// add records to the log
$log->warning('Foo');
$log->error('Bar');
3. Then, save and exit the file.
4. Now, you can use the following command to autoload monolog:
php composer_sample.php
Updating Dependencies
Composer lets you update all the dependencies in the composer.json file with a single command:
composer update
In accordance with the specified version in the file, the command above updates the dependencies.
If you do not want to update all of them, but rather one or a few individually, type in the following:
composer update vendor/package vendor_2/package_2
Conclusion
In this tutorial, you have seen how easy it is to install Composer on an Ubuntu 16.04 Linux system. Additionally, you’ve learned basic Composer usage, how to set up autoloading, and updating dependencies.
原创文章,作者:kirin,如若转载,请注明出处:https://blog.ytso.com/222986.html