How to Install and Use PHP Composer on Ubuntu 20.04

/ Category: Tutorials, Ubuntu / Comments: None

how to install and use php composer on ubuntu 20.04
Spread the love

Composer is a dependency manager tool for PHP especially designed to install and update project dependencies. It installs all required packages that are compatible with the PHP project. It allows you to specify the library that you will need for your project. It is used in all modern PHP-based applications including, Laravel, Drupal, Magento, and more.

In this post, we will show you how to install and use PHP Composer on Ubuntu 20.04.

Prerequisites

  • A Ubuntu 20.04 VPS with root access enabled or a user with sudo privileges.

Log in via SSH and Update your System

First, you will need to log in to your Ubuntu 20.04 VPS via SSH as the root user:

ssh [email protected]_ADDRESS -p PORT_NUMBER

Next, run the following commands to upgrade all installed packages on your VPS:

apt-get update -y

Once all the packages are updated, restart your system to apply the changes.

Install PHP and Additional Dependencies

Before installing Composer, you will need to install PHP and other required dependencies on your server. You can install them by running the following command:

apt-get install wget php-cli php-zip unzip -y

Once all the packages are installed, you can proceed to install PHP Composer.

Install PHP Composer

Composer provides an installer to install Composer on your system. First, use the wget command to download the Composer installer:

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

Once the installer is downloaded, run the following command to install Composer globally:

php composer-setup.php --install-dir=/usr/local/bin --filename=composer

You will get the following output:

All settings correct for using Composer
Downloading...

Composer (version 2.2.6) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

Once the installation is completed, verify the Composer version with the following command:

composer -V

You will get the following output:

Composer version 2.2.6 2022-02-04 17:00:38

If you want to install Composer locally for your project, run the following command:

php composer-setup.php --install-dir=/your-php-project

If you want to update Composer to the latest version, run the following command:

composer self-update

Output:

You are already using the latest available Composer version 2.2.6 (stable channel).

How to Use Composer

In order to use the Composer, first create a root directory for your new project:

mkdir project

Next, change the directory to the project directory:

cd project

Next, you will need to check for the libraries that are required for your project. In this section, we will install cakephp/chronos package. So, check for the libraries with the following command:

composer require cakephp/chronos

This command will install composer.json with additional packages:

Using version ^2.3 for cakephp/chronos
./composer.json has been created
Running composer update cakephp/chronos
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
  - Locking cakephp/chronos (2.3.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Downloading cakephp/chronos (2.3.0)
  - Installing cakephp/chronos (2.3.0): Extracting archive
Generating autoload files

You can view the content of the composer.json file using the following command:

cat composer.json

Output:

 
{
    "require": {
        "cakephp/chronos": "^2.3"
    }
}

Next, create a test.php file:

nano test.php

Add the following code:

<?php
require 'vendor/autoload.php';
use Cake\Chronos\Chronos;
printf("Now: %s \n", Chronos::now());
?>

Save and close the file then run the test.php file using the following command:

php test.php

You will get the following output:

Now: 2022-02-24 04:41:56 

If you want to update all packages for your project, run the following command:

composer update

Of course, if you are one of our Ubuntu Hosting customers, you don’t have to install Composer on your Ubuntu 20.04 VPS – simply ask our admins, sit back, and relax. Our admins will install Composer on Ubuntu 20.04 for you immediately.

PS. If you liked this post about how to install Composer on Ubuntu 20.04 VPS, please share it with your friends on the social networks using the buttons below, or simply leave a comment in the comments section. Thanks.

Be the first to write a comment.

Your feedback