Getting Started with Composer for Laravel Development

Getting Started with Composer for Laravel Development

When you are developing a project in Laravel, you will need to install and know how to use the composer to manage your third-party dependencies.

Dependencies are essential for any kind of modern web development. You may need some sort of functionalities for your web application can easily be included into your project using third party libraries.

There are many third-party libraries which we can use to make our life easy by simply adding them to our project. Composer is a dependency manager for PHP libraries. You can find a vast selection of compatible libraries or packages on Packagist website.

Most popular frameworks like Laravel can be found on Packagist, as Laravel itself is a collection of different packages.

This post will be a dead simple guide to get you started with using Composer and manage dependencies for Laravel.

Installing Composer

First thing first, you have to install Composer, before anything else. Follow below steps to install Composer on your computer.

Installing on Mac

If you are using a Mac, then we recommend you to install Composer using Homebrew. Homebrew is an open source software package manager for Mac. If you need to install the Homebrew use below command to install it first.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

If you need more help, You can use the Homebrew installation guide.

Once you have installed the Homebrew on your machine, you can install the composer using below command.

brew install composer

Installing on Windows

To install on windows, you can download the windows executables from Composer Download page and follow the simple installation process.

Once you have installed the composer on your machine, simply type composer and hit enter. You will be presented with a list of all composer commands.

Creating a new project using Composer

As we mentioned above, the composer is a dependencies management for PHP. You can declare all your dependencies in a single file called composer.json by adding system requirements and requiring the packages.

You can create a new Laravel project using composer create-project command. Firstly you need to clone a project from an existing package or framework using below command.

composer create-project package-name destination-folder version
  • package name: e.g. laravel/laravel
  • desctination folder: e.g. app, any folder or project name
  • version: e.g. 5.7.* (optional)

For example, if you want to create a new project of Laravel framework named Blog, you run below command.

composer create-project laravel/laravel blog

Above command will create a fresh clone of Laravel framework into a folder called blog.

In most cases, you clone a project from a Github or Bitbucket repository. After cloning you can install all predefined dependencies using below command.

composer install

Using Composer during Laravel Development

During the development of your project, you might want to install extra packages to add extra functionality to your project.

You can install a new package by running below command.

composer require  package-name:version

For example,

composer require intervention/image

Above command will add intervention/image package to your project.

If you want to update all packages to the latest version, you can run below command.

composer update

If you want to remove a package, you can run below command.

composer remove package-name

Using Composer during the Deployment

Once you have your application ready for deployment, you need to install all dependencies on your server. Composer keeps records of all packages required for your application in a separate file called composer.lock

Once you have pushed your project codebase to the server, then you can run composer install command to install all dependencies. This command will install all dependencies listed in composer.lock file.

Wrapup

Composer dependencies management make a developer’s life easy to add and remove extra packages to your project. If you have any suggestion or you point out any mistake, please let us know using our contact us page.

Your little help will keep this site alive and help us to produce quality content for you.