How to Install and Use Composer on Ubuntu 16.04

Prerequisites

  1. You have an Alibaba Cloud Elastic Compute Service (ECS) instance. If you are a new user or new to ECS, you can get Free Credits in your Alibaba Cloud account. If you don’t know about how to setup your ECS instance, you can refer to this tutorial or check quick-start guide.
  2. You should setup your server’s hostname.
  3. You have access to the VNC console in your Alibaba Cloud or SSH client installed in your PC.

Procedure

Follow the steps in the next sections to learn how to install and use Composer on an Alibaba Cloud ECS instance installed with Ubuntu version 16.04.

Logging on to Your Console

Log on as root user with your root username and password through the SSH client (which is Putty ) or through the VNC console available in your Alibaba Cloud account dashboard.

Installing the Dependencies

Before we download and install Composer, we need to make sure our server has all dependencies installed.

# sudo apt-get update# sudo apt-get upgrade
sudo apt-get install curl php-cli php-mbstring git unzip

Installing PHP

Composer requires PHP version 5.3.0 or later. To install PHP on Ubuntu, run the sudo apt-get install phpcommand. Alternatively, you can install and specific version of PHP using this command: sudo apt-get install php7.2. Note that you can change the number value to install other versions, too, such as 7.1 or 7.3.

Installing Composer

Composer provides an installer, written in PHP. Now that PHP is installed, you can run the following command to install composer:

cd ~curl –sS https://getcomposer.org/installer -o composer-setup.php
php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Output Installer verified
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
OutputAll settings correct for using ComposerDownloading 1.1.1...Composer successfully installed to: /usr/local/bin/composerUse it: php /usr/local/bin/composer
Output ______ / ____/___  ____ ___  ____  ____  ________  _____ / /  / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___// /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/ /_/Composer version 1.1.1 2016-05-17 12:25:44Usage: command [options] [arguments]Options: -h, --help  Display this help message -q, --quiet  Do not output any message -V, --version  Display this application version --ansi  Force ANSI output --no-ansi  Disable ANSI output -n, --no-interaction  Do not ask any interactive question --profile  Display timing and memory usage information --no-plugins  Whether to disable plugins.. . .

Generating the Composer.json File

You need to have composer.json file in order to use the composer in your project. The composer.json file is the one which tells composer to download dependencies which are required by the project.

  • Find a matching open-source library on packagist.org, which is the official repository for Composer.
  • Select the package that you require.
  • Run the composer require the command to include the dependency in the composer.json file and then install the package.
cd ~ mkdir slugify cd slugify

Finding the Package from the Package List

Go to the Packagist and search for the package which helps us in generating the slug. Search for “slug” in the search bar. All the relevant packages will be listed here. In each description after the name, you can see the numbers.

Adding the Package

To add the package, use the composer require command. Then generate the composer.json file.

$ composer require cocur/slugify
Output Using version ^2.1 for cocur/slugify ./composer.json has been created Loading composer repositories with package information Updating dependencies (including require-dev) - Installing cocur/slugify (v2.1.1) Downloading: 100%  Writing lock file Generating autoload files
$ ls -l
Output total 12 -rw-rw-r-- 1 gqadir gqadir  59 Jan 17 10:24 composer.json -rw-rw-r-- 1 gqadir gqadir 2894 Jan 17 10:24 composer.lock drwxrwxr-x 4 gqadir gqadir 4096 Jan 17 10:24 vendor

Updating the Project Dependencies

Updating the project dependencies is very easy. To update the dependencies, just use the composer updatecommand. What this command does is it looks for the recent version of the dependencies.

composer update vendor/package vendor2/package2

Summary

This tutorial covered the essentials for getting started with using Composer on an ECS instance installed with Ubuntu 16.04. Composer is a necessary tool for the PHP developer as it makes ease of their job, and should have its place in their utility belt.

Original Source

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store