Now Reading
How To Install Composer On Ubuntu 20.04 LTS
Dark Light

How To Install Composer On Ubuntu 20.04 LTS

gold and silver round decor

Dependency Management is one of the important aspects of programming software using re-usable packages, libraries, and modules. Almost all the popular programming languages are supported by dependency management tools to manage project dependencies. We can manage the project dependencies in PHP using Composer. It resolves all the project dependencies and installs the dependencies using the appropriate version based on the version specified by the project configuration. This tutorial provides all the steps required to install Composer on Ubuntu 20.04 LTS. The steps should be similar on other versions of Ubuntu and Linux systems. Prerequisites It assumes that PHP is already installed on the system. You can also follow How To Install PHP 7 On Ubuntu 20.04 LTS. Install Composer This section provides the steps to download the PHP script required to install Composer on Ubuntu. It also shows how to verify this script and install Composer on Ubuntu 20.04. # Refresh Packagessudo apt update# Install Required packagessudo apt install php-cli php-zip zip unzip# Install CURLsudo apt install curl Now download the installer script as shown below. # Setups or Home Directory# Download installer scriptcurl -sS -o composer.php We will also verify the installer script before installing the Composer as shown below. # Get the latest HASHHASH=`curl -sS`# Check the HASHecho $HASH# Outpute0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a# Verify installer scriptphp -r “if (hash_file(‘SHA384’, ‘composer.php’) === ‘$HASH’) { echo ‘Installer verified’; } else { echo ‘Installer corrupt’; unlink(‘composer-setup.php’); } echo PHP_EOL;”# OutputInstaller verified This completes the verification of the installer script. Now we can continue with the installation of the Composer on Ubuntu. In case the verification steps generate the output – Installer corrupt, we have to download the new installation script and verify it as shown above. Now install Composer globally as shown below. # Install Composer – Globallysudo php composer.php –install-dir=/usr/local/bin –filename=composer# OutputAll settings correct for using ComposerDownloading…Composer (version 1.10.7) successfully installed to: /usr/local/bin/composerUse it: php /usr/local/bin/composer Also, verify the installation as shown below. # Verify Installationcomposer# Output ______ / ____/___ ____ ___ ____ ____ ________ _____ / / / __ / __ `__ / __ / __ / ___/ _ / ___/ / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / ____/____/_/ /_/ /_/ .___/____/____/___/_/ /_/ Composer version 1.10.7 2020-06-03 10:03:56 Usage: command [options] [arguments] Options: -h, –help Display this help message———-# Check Versioncomposer –version# OutputComposer version 1.10.7 2020-06-03 10:03:56 The Composer version 1.10.7 was installed while writing this tutorial. Now we can use the command composer to add and manage project dependencies. Using Composer In this step, we will learn using the composer in our projects. The composer specific configuration for each project can be managed using the composer.json file placed at the root of the project. We can either generate this file manually or it will be generated on adding the first dependency. I have added the PHPMailer as the first dependency to my project using the command as shown below. # Install PHPMailer as project dependencycomposer require phpmailer/phpmailer# OutputUsing version ^6.1 for phpmailer/phpmailer./composer.json has been createdLoading composer repositories with package informationUpdating dependencies (including require-dev)Package operations: 1 install, 0 updates, 0 removals – Installing phpmailer/phpmailer (v6.1.4): Downloading (100%)phpmailer/phpmailer suggests installing psr/log (For optional PSR-3 debug logging)phpmailer/phpmailer suggests installing league/oauth2-google (Needed for Google XOAUTH2 authentication)phpmailer/phpmailer suggests installing hayageek/oauth2-yahoo (Needed for Yahoo XOAUTH2 authentication)phpmailer/phpmailer suggests installing stevenmaguire/oauth2-microsoft (Needed for Microsoft XOAUTH2 authentication)phpmailer/phpmailer suggests installing symfony/polyfill-mbstring (To support UTF-8 if the Mbstring PHP extension is not enabled (^1.2))Writing lock fileGenerating autoload files It generates the composer.json, composer.lock files at the project root directory. It also creates the vendor directory having the PHPMailer as the dependency. It also creates the autoload.php file within the vendor directory. The composer.json file content is shown below. { “require”: { “phpmailer/phpmailer”: “^6.1” }} We can also manually update this file to add further dependencies or simply run the command composer require to add the required dependencies. In case we update this file manually, we must run the command composer update to apply the changes. We must add the autoload.php file within the project’s PHP files to use the dependencies. It can be done as shown below: How To Install PHP For Nginx On Ubuntu 20.04 LTS

Touch The Stars

0 / 5

Your page rank:

What's Your Reaction?
In Love
Like It
Not Sure
View Comments (0)

Leave a Reply

Your email address will not be published.

Copyrights © 2021 Lucid.Hive.Systems. All Rights Reserved.