Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Install and Use PHP Composer on Ubuntu 20.04 - DesignLinux

May 29 2020
designlinux 0 Comments
How to Install and Use PHP Composer on Ubuntu 20.04

Composer is most popular dependency manager for PHP. Composer can install, update and pull in all the required PHP packages to your project directory. At the time of installing package, composer will check for dependencies of package and if dependent package are there then it will also install dependencies. This guide explains how to install and use Composer on Ubuntu 20.04 system.

Prerequisites#

  • Before you start to install Composer, You should logged in as non-root user account with sudo privileges.
  • Have PHP installed on you Ubuntu 20.04 system.

Installing PHP Composer#

Follow the below steps to install Composer on your Debian 10 Buster:

01. Update the packages index and install other dependencies:

sudo apt update
sudo apt install curl php-cli php-mbstring git unzip

02. Download the composer installer file:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

File composer-setup.php will be downloaded to current working directory.

03. Verify the data integrity.

Check the data integrity of the script by comparing the script SHA-384 hash on the Composer Signatures page.

Use wget command to download the signature of the latest Composer installer and will store it in HASH variable as given below:

HASH="$(wget -q -O - https://composer.github.io/installer.sig)"

Now execute the following PHP code, as provided in the Composer download page, to verify that the installation script is safe to run:

php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

You will get following output if the hashes match:

Installer verified

If the hashes don’t match you will see output like Installer corrupt. Again, you have to download composer installation script and check hash value until you get Installer Verified output.

04. Next, issue the following command to install Composer globally inside /usr/local/bin directory:

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

It will show you output as below:

All settings correct for using Composer
Downloading…
Composer (version 1.10.5) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

That’s it. Composer is installed as a system-wide and it will be available for all users.

05. Verify the installation by typing:

composer

The above command will print details as following:

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.10.5 2020-05-23 11:44:22

Usage:
  command [options] [arguments]

Use Composer in PHP Project#

Now the Composer is installed globally on your Debian 10 system and we will show how to use in your php project.

First of all, you have to create a directory which should project root directory. Create directory my-project as a root directory of your project.

sudo mkdir my-project
cd my-project

The next step is to initialize a new composer.json using the composer require command and specify the package we want to download.

In this example, we will create a sample application to print the current time using carbon package.

Execute the below command to initialize a new composer.json and install the carbon package:

composer require nesbot/carbon

After complete the installation you can see that Composer created two files composer.json and composer.lock, and a vendor directory.

ls -l
-rw-rw-r--. 1 tecnstuff tecnstuff 60 	May 23 14:15 composer.json
-rw-rw-r--. 1 tecnstuff tecnstuff 11135 May 23 14:16 composer.lock
drwxrwxr-x. 6 tecnstuff tecnstuff 82 	May 23 14:16 vendor
  • The vendor directory contains project dependencies.
  • The composer.lock contains a list of all installed packages including the version of the packages.
  • The composer.json describes the PHP project and all PHP dependencies.

Now, you have installed carbon package, create a new file named test.php and paste the following code inside the file. It will prints the current time.

<?php

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

use Carbon\Carbon;

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

Run above script by typing:

php test.php

The output should look like this:

Now: 2020-05-23 18:38:52

Whenever you want to update your project dependencies to more recent versions, run the update command:

composer update

Conclusion#

Composer is a powerful tool that can greatly facilitate the work of managing dependencies in PHP projects. We shows you how to install Composer on your Ubuntu 20.04 system.

We have also described you how to use Composer to create a simple PHP project. You can get more information about Composer visit the official Composer documentation page.

If you have any question or suggestion please leave comment below.

Related

Tags: composer, php, ubuntu

How to Install R on CentOS 8

Prev Post

How to Install VirtualBox Guest Additions on Ubuntu 20.04

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 1
  • 423
  • 609,783

DesignLinux.com © All rights reserved

Go to mobile version