You can search the Composer repository Packagist for PHP packages.
Installing PHP Composer on Ubuntu Focal Fossa (20.04 LTS)
- PHP-Composer
- Ubuntu
- Focal-Fossa
- symfony
PHP Composer is a package dependency management tool for PHP similar to NPM for Node.js and a bundle for Ruby. Composer facilitates the installation and updates for project dependencies. In short, Composer will pull in all the required PHP packages your project depends on and manage them for you.
Before you start
To complete the actions presented below, you must have:
- A Scaleway account logged into the console
- Owner status or IAM permissions allowing you to perform actions in the intended Organization
- An SSH key
- An Instance running on Ubuntu Focal Fossa
- Installed PHP 7.4 or higher on your Instance
Installing Composer
-
Update the packages index and install the necessary requirements:
apt updateapt install wget php7.4-cli php7.4-zip unzip -y -
Download the composer installer. The command below downloads the
composer-setup.php
file in the current working directory:php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" -
Verify the data integrity of the script by comparing the script SHA-384 hash with the latest installer hash found on the Composer Public Keys/Signatures page. Download the expected signature of the latest Composer installer from the Composer’s GitHub page and store it in a variable named
HASH
.HASH="$(wget -q -O - https://composer.github.io/installer.sig)" -
Verify that the installation script is not corrupted:
php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"If the hashes match, you will see the following output:
Installer verified
.If it does not match, download the Composer installation script again and double-check the value of the $HASH variable with
echo $HASH
. Once the installer is verified, you can continue with the next step. -
Install Composer in the
/usr/local/bin
directory:sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer -
Verify the installation:
composerYou should see the following output:
root@composer:~# composerDo not run Composer as root/super user! See https://getcomposer.org/root for details______/ ____/___ ____ ___ ____ ____ ________ _____/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___// /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /\____/\____/_/ /_/ /_/ .___/\____/____/\___/_//_/Composer version 2.2.4 2022-01-08 12:30:42[...]
Using Composer PHP project
-
Create a directory which will be the project root directory and which will hold the
composer.json
file. This file describes your PHP project including the PHP dependencies and other metadata.mkdir ~/composer-project1cd composer-project1 -
Initialize a new
composer.json
file using the composer require<package name>
command, and specify the package to download.In this example, we will create a sample application that will print the current time using a package called carbon. Carbon can help make dealing with date/time in PHP much easier and more semantic so that our code can become more readable and maintainable.
Note -
Run the following command to initialize a new
composer.json
file, and install the carbon package:composer require nesbot/carbonUsing version ^2.55 for nesbot/carbon./composer.json has been createdRunning composer update nesbot/carbonLoading composer repositories with package informationUpdating dependenciesLock file operations: 6 installs, 0 updates, 0 removals- Locking nesbot/carbon (2.55.2)- Locking symfony/deprecation-contracts (v2.5.0)- Locking symfony/polyfill-mbstring (v1.24.0)- Locking symfony/polyfill-php80 (v1.24.0)- Locking symfony/translation (v5.4.2)- Locking symfony/translation-contracts (v2.5.0)Writing lock fileInstalling dependencies from lock file (including require-dev)Package operations: 6 installs, 0 updates, 0 removals- Downloading symfony/translation-contracts (v2.5.0)- Downloading symfony/polyfill-php80 (v1.24.0)- Downloading symfony/polyfill-mbstring (v1.24.0)- Downloading symfony/deprecation-contracts (v2.5.0)- Downloading symfony/translation (v5.4.2)- Downloading nesbot/carbon (2.55.2)- Installing symfony/translation-contracts (v2.5.0): Extracting archive- Installing symfony/polyfill-php80 (v1.24.0): Extracting archive- Installing symfony/polyfill-mbstring (v1.24.0): Extracting archive- Installing symfony/deprecation-contracts (v2.5.0): Extracting archive- Installing symfony/translation (v5.4.2): Extracting archive- Installing nesbot/carbon (2.55.2): Extracting archive3 package suggestions were added by new dependencies, use `composer suggest` to see details.Generating autoload files6 packages you are using are looking for funding.Use the `composer fund` command to find out more!Composer provides autoload capabilities which allows us to use PHP classes without the need to
require
orinclude
the files.Composer creates the
composer.json
. It also downloads and installs carbon and all its dependencies. If you list your project’s directory, it contains two files and a directory:- The
vendor
directory is the directory where the project dependencies are stored. - The
composer.lock
contains a list of all installed packages including the exact package version. - The
composer.json
describes the PHP project and all PHP dependencies.
- The
-
Create a file named
testing.php
and open it in a text editor (for example:nano
):touch testing.phpnano testing.phpThen, add the following code to the file, save it, and exit the text editor:
<?phprequire __DIR__ . '/vendor/autoload.php';use Carbon\Carbon;printf("Now: %s", Carbon::now());?>In the first line after the opening php tag, we include the
vendor/autoload.php
, automatically generated by Composer. It will autoload all the libraries.Then, we alias Carbon\Carbon as Carbon, and with the last line we are printing the current time using the Carbon now method.
-
Run the script:
php testing.phpNow: 2020-09-01 09:19:23
If you want to update your PHP packages you can simply run composer update
.
To learn more about Composer visit the official documentation.