How to Install and Use PHP Composer on Debian 10

[ad_1]

Composer
is a dependency manager for PHP (similar to npm for Node.js
or pip for Python
).

Composer will pull in all the required PHP packages your project depends on and manages them for you. It is used in all modern PHP frameworks and platforms such as Laravel, Symfony, Drupal, and Magento.

This article explains how to install Composer on Debian 10 systems. We will also cover how to use Composer to create and manage PHP projects.

Installing Composer on Debian #

Before installing Composer, ensure that you have all the necessary packages installed on your Debian system:

sudo apt updatesudo apt install wget php-cli php-zip unzip

Composer offers an installer written in PHP
that we’ll use to install Composer.

Download the installer with wget
:

wget -O composer-setup.php https://getcomposer.org/installer

The command above will save the file as composer-setup.php in the current working directory
.

Composer is a single file CLI application that can be installed either globally or as part of the project. The global installation requires sudo privileges
.

  • To install Composer globally as a system-wide command that will be available for all users, simply place the file in a directory that is in the system PATH
    . The following command installs Composer in the /usr/local/bin directory:

    sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
    All settings correct for using Composer
    Downloading...
    
    Composer (version 1.10.10) successfully installed to: /usr/local/bin/composer
    Use it: php /usr/local/bin/composer

    You can now use Composer by running composer in your terminal.

  • To install composer locally, download the file in your project root directory:

    sudo php composer-setup.php --install-dir=/path/to/project

    This will download a file named composer.phar. To use Composer navigate to the project directory and run php composer.phar

When a new Composer version is available, you can update the installation using the following command:

sudo composer self-update  

Getting Started with Composer #

Now that you have Composer installed on your Debian system, we will show you how to create a PHP project.

Start by creating a directory that will be the project root and hold the composer.json file. This file describes your PHP project, including the PHP dependencies and other metadata.

Run the following commands to create the project directory and switch to it with:

mkdir ~/my-first-composer-projectcd ~/my-first-composer-project

Next, we’ll initialize a new composer.json file using the composer require <package name> command and specify the package we want to download. In this example, we will create a sample application that will print the current time using a package named carbon
.

Run the following command to initialize a new composer.json file and install the carbon package:

composer require nesbot/carbon
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 5 installs, 0 updates, 0 removals
  - Installing symfony/translation-contracts (v2.1.3): Downloading (100%)         
  - Installing symfony/polyfill-php80 (v1.18.1): Downloading (100%)         
  - Installing symfony/polyfill-mbstring (v1.18.1): Downloading (100%)         
  - Installing symfony/translation (v5.1.3): Downloading (100%)         
  - Installing nesbot/carbon (2.38.0): Downloading (100%)         
symfony/polyfill-mbstring suggests installing ext-mbstring (For best performance)
symfony/translation suggests installing symfony/config
symfony/translation suggests installing symfony/yaml
symfony/translation suggests installing psr/log-implementation (To use logging capability in translator)
Writing lock file
Generating autoload files
5 packages you are using are looking for funding.
Use the `composer fund` command to find out more

As shown in the output, Composer creates the composer.json file and downloads and installs carbon and all its dependencies.

If you list your project’s directory with ls
, you’ll see that it contains two files composer.json and composer.lock, and a vendor directory.

ls -l
-rw-r--r-- 1 linuxize users   60 Aug 17 21:02 composer.json
-rw-r--r-- 1 linuxize users 6851 Aug 17 21:02 composer.lock
drwxr-xr-x 5 linuxize users 4096 Aug 17 21:02 vendor
  • vendor – the directory where the project dependencies are stored.
  • composer.lock – a file containing a list of all installed packages including the version of the packages.
  • composer.json – a file describing the PHP project and all PHP dependencies.
You can search the Composer repository Packagist
for PHP packages.

Create a file named testing.php and paste the following code:

<?php

require __DIR__ . '/vendor/autoload.php';

use CarbonCarbon;

printf("Now: %s", Carbon::now());

Let’s analyze the code line by line.

In the first line after the opening php tag we are including the vendor/autoload.php file that was automatically generated by Composer. This file will autoload all the required libraries.

Next, we are aliasing CarbonCarbon as Carbon, and in the last line, we are printing the current time using the Carbon now method.

Run the script by typing:

php testing.php

The output should look something like below:

Now: 2020-08-17 21:08:45

Later, if you need to update the project packages, enter:

composer update

The command above will check for newer versions of the installed packages, and if a newer version is found and the version constraint
match with the one specified in the composer.json, Composer will update the package.

Conclusion #

We have shown you how to install Composer on Debian 10 and how to use it to create a basic PHP project.

For more information about Composer, visit the official documentation
page.

If you have any questions, please leave a comment below.

[ad_2]

Source link

Related Posts

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.