Installing a package in Laravel using composer

Laravel is a great framework at least for me. as you probably know we can make use of already existing libraries to make software crafting a lot easier. Laravel and composer together gives us an intuitive way to install and remove packages for our laravel application.

Composer is the default package manager for PHP. Read more about composer here. If you are into PHP and laravel for some time then you will have a copy of composer for sure if not get composer from here.

The very important part of composer is the composer.json file located inside the root directory of your laravel installation. Here is my composer.json file in the middle of development of an application.

Where you need to look is the require and require-dev sections. require section contains all the packages needed for your application in production and require-dev contains all the packages needed while developing the application such as for unit testing etc…

We can install a package using composer in different ways.

  1. By directly editing composer.json
  2. By using composer require command

what I am gonna explain here is the second way. To install a package you need to find the package first. Let’s go to packagist.org and find the package you want. and get the package name and version from the description.

go to the root directory of your laravel application and open up the terminal. issue the command composer require
then type in your package name, Composer will look up the packagist repository and gives you all possible packages which match your query ordered by number. then input the number in which you want to install the package. By default, the packages will be installed in the vendor directory.

issue the composer dump-autoload command to reload the autoloading classes in your application. You can use the newly installed classes by the use keyword in anywhere in our application.

 

Programmer, Hacker, Trainor, Speaker, and Blogger. Opensource Lover. Built LeHacker.com.

One comment: On Installing a package in Laravel using composer

Leave a reply:

Your email address will not be published.

Site Footer