Composer install in Ubuntu

Composer is the popular dependency manager tool that installs and updates dependencies in your PHP project easily. It is similar to NPM for NodeJs and bundle for Ruby.

In this article, we’ll show you how to install and use Composer on an Ubuntu 18.04 machine.

Prerequisites:

You need to have non-root user logged in with sudo privileges to set up on your server. You also have PHP 5.3.2 or higher installed in your server.

Step 1. Update package repository and install required packages.

First run bellow command to update package repository by running following command:

sudo apt-get update

After that, install necessary packages that will need to install Composer.

sudo apt-get install curl php5-cli git

Step 2. Downloading and installation of Composer

Now run this command in the Terminal

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

This will download and install Composer globally command named composer, under /usr/local/bin. The output should look like this:

All settings correct for using Composer
Downloading...

Composer (version 1.9.0) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

Step 3. Verify installation

To test your installation, run the command:

composer

The command will display as bellow:

Step 4. Use of Composer

Now composer has been installed. You can install PHP packages with bellow command.

composer require <vendor/package>

This command will create composer.json file in the root directory of your project. It contains all dependencies of your project. Another composer.lock file is also created which contains all dependencies with exact versions that is downloaded. The vendor directory is also created that stores all downloaded dependencies files. There is a file /vendor/autoload.php. All you have to do is include file and it will load all vendor classes.

If you want to update all of the project dependencies to latest versions, run the update command:

composer update

You can also update specific dependencies by running command:

composer update <vendor/package>

If you want to remove any dependencies, run command:

composer remove <vendor/package>

To find out more information on Composer, go to official documentation

Tags: