banner



How To Check Composer Is Installed Or Not In Ubuntu

Introduction

PHP Composer is an application that tracks dependencies for a project. It does non supercede system parcel managers similar apt oryum.

Composer allows yous to specify a ready of libraries for a specific projection then identifies the versions and dependencies and installs them.

This guide will evidence you how to install and become started with PHP Composer in Ubuntu 18.04.

tutorial on installing and using php composer on ubuntu

Prerequisites

  • An Ubuntu 18.04 Linux system
  • PHP 5.iii.2 or later on installed on Ubuntu
  • A user account withsudo privileges
  • Access to a command line/final window (Ctrl+Alt+T)

Steps For Installing PHP Composer on Ubuntu

Stride 1: Update Local Repository

Showtime by updating the local repository lists by enter the following in a command line:

          sudo apt-get update        

Step two: Download the Composer Installer

To download the Composer installer, use the command:

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

Step 3: Verify Integrity of the Download

1. Visit the Composer Public Keys folio. Copy the Installer Signature (SHA-384).

2. Set the code shell variable:

          COMPOSER=48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5        

3. Run the script below to compare the official hash against the one you lot downloaded:

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

The script will either tell you the download is verified, or that it has been corrupted. If it's corrupted, re-download the file.

Step iv: Install PHP Composer

1. Installing PHP Composer requires ringlet, unzip, and a few other utilities. Install them by entering the following:

          sudo apt-get install curl php-cli php-mbstring git unzip        
install supporting software for composer

Install Composer as a command attainable from the whole system.

2. To install to /usr/local/bin. enter:

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

The installer should output:

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

3. Once the installer finishes, verify the installation:

          composer --version        

The organisation should output the version number installed, for example:

          Composer version i.8.6 2022-6-11 15:03:05        

4. To remove the installer:

          php -r "unlink('composer-setup.php');"        

Basic Composer Usage

Composer is designed to runway dependencies on a per-projection basis. This makes information technology easier for other users to create the same environment. Composer uses a composer.json file to go on track of required software and allowed versions.

Information technology besides uses acomposer.lock file to maintain consistency if someone copies the directory. These files are automatically generated using the require  control.

i. Open a terminal, and enter the following:

          mkdir c_sample cd c_sample        

two. Next, you'll need to choose a package to load. The website packagist.orghas a broad range of different PHP packages for download. In this example, let's use the monolog/monolog package. You can follow the instructions, or search the website for monolog.

3. In the concluding window, enter:

          composer require monolog/monolog        

The system will download the software and create the composer.json andcomposer.lock files.

Annotation: Monolog is a packet for managing logfiles. The name before the slash is the vendor, and the name afterwards the slash is the package proper noun.

4. Once the process completes, listing the contents of the directory:

          ls -50        

You should see thecomposer.json andcomposer.lock files, forth with avendor directory.

v. To view the contents of the composer.json file:

          cat composer.json        

The arrangement will show y'all that it has added the monolog software. The carat ^ sign beside the version number indicates the minimum version of the software.

Setting Up Autoloading

PHP doesn't automatically load classes. Nonetheless, you can configure Composer to autoload classes for you. This makes working with dependencies much easier.

1. Create a new file using your favorite text editor:

          sudo nano composer_sample.php        

2. Enter the following into the file:

          <?php  require __DIR__ . '/vendor/autoload.php';  use Monolog\Logger;  utilize Monolog\Handler\StreamHandler;     // create a log channel  $log = new Logger('proper name');  $log->pushHandler(new StreamHandler('/~/c_sample/text.log', Logger::Warning));     // add together records to the log  $log->warning('Foo');  $log->mistake('Bar');        
example of autoloading classes

3. Write the file ( Ctrl+O ), and exit ( Ctrl+X ).

4. Now y'all can run the command to autoload monolog:

          php composer_sample.php        

Updating Dependencies

To update all the dependencies in your composer.json file enter the control:

          composer update        

This will update all dependencies according to the version specified in the file.

To update one (or more) dependencies individually:

          composer update vendor/package vendor_b/package_b        

Conclusion

Now you have a expert understanding of how to install and start using PHP Composer. We also covered setting upwardly autoloading and updating dependencies.

Was this article helpful?

Yep No

Source: https://phoenixnap.com/kb/how-to-install-composer-ubuntu-18-04

Posted by: mirandareliblese47.blogspot.com

0 Response to "How To Check Composer Is Installed Or Not In Ubuntu"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel