How to Install Composer on Ubuntu 22.04

11

Composer is a dependency control device for PHP that permits you to claim the libraries your venture is dependent upon and installs them for you. In this text, we can quilt the stairs for putting in Composer on Ubuntu 22.04.

Prerequisites

  • Shell get right of entry to to a operating Ubuntu formula.
  • Install PHP 5.3 or upper edition.
  • PHP’s package deal supervisor, `php-cli`, should be put in and to be had on your `PATH`

Installing PHP Composer on Ubuntu

The Composer’s authentic crew supplies a script to put in PHP composer on Linux methods. You can obtain this script the use of the `curl` or `wget` command-line software. Also, you’ll be able to obtain it without delay the use of the PHP script command line.

  1. To obtain the `composer-setup` script, run the next command in a terminal:
    php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" 
  2. Then execute the downloaded PHP script to put in the composer in your Ubuntu formula on the desired location. Use --install-dir to set the binary location and --filename to set the binary title. You can select one of the most underneath choice:
    • Installing PHP composer system-wide: This will set up composer in /uer/native/bin listing, this is available to all customers:
      sudo php composer-setup.php --install-dir=/usr/native/bin --filename=composer sudo chmod +x /usr/native/bin/composer
    • Installing PHP composer for explicit applicaiton: Sometimes you dont’ have permission to put in it globally, like shared internet hosting account. Then you’ll be able to configure this underneath you utility as smartly. To set up composer in the community kind:
      cd /trail/to/php-application && mkdir -p bin  php composer-setup.php --install-dir=bin --filename=composer chmod +x bin/composer

      Make certain to exchange /trail/to/php-application along with your utility listing.

  3. To see the put in composer edition execute binary with -v command parameter.
    composer --version

    Output:

    Composer edition 2.3.7 2022-06-06 16:43:28
    

Upgrade PHP Composer

The PHP composer has the power to self-upgrade to the most recent variations. If the composer is already put in in your formula, simply kind the underneath command to improve the PHP composer to the most recent edition.

composer self-update

In my case, I have already got newest edition of composer. So obtain the next message on terminal:

Output:

You are already the use of the most recent to be had Composer edition 2.3.7 (solid channel).

Working with PHP Composer

You have already put in and configured the composer in your formula. The composer will mean you can to regulate modules on your utility. For instance, to put in a brand new module on your utility.

Switch to the PHP utility.

cd /trail/to/php-application 

Run the next command to put in psr/log module within the utility.

composer require psr/log

Output:

Using edition ^3.0 for psr/log
./composer.json has been created
Running composer replace psr/log
Loading composer repositories with package deal data
Updating dependencies
Lock record operations: 1 set up, 0 updates, 0 removals
  - Locking psr/log (3.0.0)
Writing lock record
Installing dependencies from lock record (together with require-dev)
Package operations: 1 set up, 0 updates, 0 removals
  - Downloading psr/log (3.0.0)
  - Installing psr/log (3.0.0): Extracting archive
Generating autoload recordsdata

Composer will routinely create or replace composer.json record at utility root listing. Now, the appliance can use the capability equipped via the module.

The above command will set up the most recent edition of the module. You too can outline the module edition you wish to have to put in on your utility. If the module is already put in, it’s going to routinely downgrade/improve the package deal to the required edition.

composer require psr/log=1.0

The module now not required will also be got rid of with the next command.

composer take away psr/log

All the above instructions additionally replace composer.json record accordingly.

Conclusion

In this text, we now have coated the stairs for putting in Composer on Ubuntu 22.04. By the use of Composer, you’ll be able to simply organize the dependencies of your PHP tasks and make sure that all essential libraries are put in and up to the moment. Whether you might be new to PHP construction or an skilled developer, Composer mean you can streamline your workflow and give a boost to the standard of your tasks.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More