This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

How to Use Composer in Laravel 8

Updated on September 3, 2021

5 Min Read
how to use composer

PHP now has effective and mature frameworks in hand that can perform nearly all the basic processes. In fact, if you are smart enough, only then you will cherry pick the components from other frameworks like Zend, Laravel, Symfony, etc. This is the most hectic phase where you need to keep the dependencies of the required components this is where the composer in laravel comes in, or else you will create a huge mess.

Composer is an application-level bundle director for the PHP programming language that gives a standard arrangement for overseeing dependencies of PHP computer programs and required libraries. It was created by Nils Adermann and Jordi Boggiano, who proceeded to oversee the extent.

What’s Composer?

A Composer is a tool that incorporates all the dependencies and libraries. It helps the client to create a project with respect to its specified system and project. Third-party libraries can be installed in a project effortlessly utilizing composer. Composer is utilized to manage its conditions and the dependencies are noted in composer.

It is designed in such a way that it will not only pull the required component or package you want in your PHP application. It also keeps the track of the dependencies of those required components or packages as well as creates the class map for the downloaded libraries.

It automatically loads once and becomes available system-wide. It follows the DRY principle. DRY (Don’t Repeat Yourself) is a key principle in software engineering. It simply states that one should not reinvent the wheel while you are writing any application. This way, you save your precious time and your code stays beautiful. It is very useful if you have more than one developer on the project

Nothing as Easy as Deploying Laravel Apps on Cloud

With Cloudways, you can have your Laravel apps up and running on managed cloud servers in just a few minutes.

Prerequisites

For the purpose of this tutorial, I assume that you have a Laravel application installed on a web server. My setup is:

I have installed a Laravel app on a Cloudways managed server because it has everything I’ll need for this tutorial. If you do not have an account on Cloudways, sign up for free, and check out the following GIF to setup the server and application in just a few clicks.

Cloudways Installation

Install Composer

Laravel executes a composer for overseeing dependencies inside it. Thus, before the utilization of Laravel, it should check whether you have got a composer setup on your framework or not, In case you do not have Composer installed on your computer,

By using the composer command to check the composer is install or not already.

composer

Navigate to Vendor→ composer.json, composer.lock in public_html folder. It comes with Laravel already. ­

Open the composer.json file with $ nano composer.json, then you will see a json having different blocks in which you can use any required block.

If you want to add some URL shortener library in Laravel, go to Packagist and do a search for Laravel Debuger. Open the specific package, which looks like barryvdh/laravel-debugbar and run the following command on SSH Terminal.

composer require barryvdh/laravel-debugbar --dev

Installation

where
barryvdh is the vendor name
Laravel-debugbar is the package name
Follow the instructions while adding the package from packagist to install properly in Laravel.
The final execution result should be like the image below.

After the use of composer install, always use composer update because composer install creates the composer.lock file having all versions of the exact libraries needed to run the Composer. If using composer install, it ignores composer.json and reads only the frozen composer.lock file.

Composer install will only work if you delete the composer.lock file for some reason. Composer update ignores the composer.lock file and downloads new libraries added in composer.json. Composer update vendor/package will only update the given library instead of all of them in json file.

You can use composer require to find the required library and Composer will automatically update composer.json for you and execute the update command to download the library. The interesting thing is that it automatically downloads the dependent file needed for your required library so that you may use it without any dependency errors.

You might be thinking: How does Composer know where to download the code just by the name components/jquery?

Composer connects to an official repository named Packagist. You can search this repo to see the libraries that are available for management through Composer. You can even create your own packages and submit them to Packagist for making them available to others.

To remove any Package or Component

If you want to remove any package or component, just use the following code.

$ composer remove vendor/package

Project Portability

You just have to commit your code files with composer.json and composer.lock files to Git instead of vendor folder for the developers to get your code and start maintaining it. They just have to “Git clone YOUR_REPOSITORY” and execute composer install to get all dependent files of your project in vendor folder created by this command.

Conclusion

Composer in Laravel is used to manage their dependencies. Once Composer is installed, download the framework and extract its contents into a directory on your server. The primary task you should do after installing Laravel is to set your application key to a random string. If you have installed Laravel via Composer, there is a chance this key is already set by the key:generate command.

Q. Does Laravel use composer ?

A: Laravel uses Composer to manage its conditions. To initialize, download a duplicate of the composer. phar . Once you have got the PHAR archive, you’ll be able to keep it in your local projects directory or move to usr/local/bin to utilize it universally on your system.

Q. How do I know if laravel composer is installed ?

A: After you are done installing the Composer, cross-check whether it is installed or not by writing within the command prompt the composer command. You’ll be able to see the Composer screen in that CMD as it were.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Customer Review at

“Cloudways hosting has one of the best customer service and hosting speed”

Sanjit C [Website Developer]

Shahzeb Ahmed

Shahzeb is a Digital Marketer with a Software Engineering background, works as a Community Manager — PHP Community at Cloudways. He is growth ambitious and aims to learn & share information about PHP & Laravel Development through practice and experimentation. He loves to travel and explore new ideas whenever he finds time. Get in touch with him at [email protected]

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!