×
Community Blog How to Install and Use Composer on Ubuntu 16.04

How to Install and Use Composer on Ubuntu 16.04

This tutorial covers how to install and use Composer on an Alibaba Cloud ECS instance installed with Ubuntu 16.04.

By Ghulam Qadir, Alibaba Cloud Community Blog author.

Composer is a package dependency management tool for PHP that you and other developers alike can use to manage PHP-based projects and applications.

You've probably seen, even on multiple occasions, some PHP applications asking if you have composer installed on the system, so that the application can get the package installed and working. That's how important and common Composer is nowadays.

So, in other words, composer is frequently used to facilitate the installation and updates of PHP applications. Through using the composer dependencies tool, packages can easily be installed and managed. Composer helps to maintain a list of required packages in a JSON file called composer.json.

Composer is compatible and works well with Alibaba Cloud Elastic Compute Service (ECS) instances. As a developer myself, I prefer to use Composer on an Alibaba Cloud ECS instance that is installed with Ubuntu 16.04. Alibaba Elastic Compute (ECS) is highly flexible and you can upgrade the hardware resources anytime when you get more traffic.

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.

Before you begin this tutorial, again, you'll need the following:

  • An Alibaba Cloud ECS instance installed with Ubuntu version 16.04 with at least 2 GB of RAM, private networking enabled, and a non-root user. This can be set up by following the Initial ECS instance server set up with Ubuntu 16.04.

Installing the Dependencies

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

First, update the package manager cache by running the following two commands:

# sudo apt-get update

# sudo apt-get upgrade

Next we have to install the dependencies (curl, php-cli, php-mbstring, git and unzip). Everything can be installed with the following command:

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 php command. 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

Next, run a short PHP script to verify that the installer matches the SHA-384 hash for the latest installer found on the Composer Public Keys / Signatures page. You will need to make sure that you substitute the latest hash for the highlighted value below:

 php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

The output of which should be:

 Output

 Installer verified

To install composer globally, use the following:

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

This will download and install Composer as a system-wide command named composer, under /usr/local/bin. The output should look like this:

Output

All settings correct for using Composer

Downloading 1.1.1...

Composer successfully installed to: /usr/local/bin/composer

Use it: php /usr/local/bin/composer

To test your installation, run the composer command, and you should get an output similar to this:

Output

 ______

 / ____/___  ____ ___  ____  ____  ________  _____

 / /  / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/

/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /

\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/

 /_/

Composer version 1.1.1 2016-05-17 12:25:44

Usage:

 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.

. . .

All of this just means Composer was installed on your system.

Now, when you want to install a PHP based application that requires Composer, simply reference this page to learn how to install Compser. After that, continue with getting your PHP application on Ubuntu.

Composer can also be upgraded by running the sudo composer self-update command. This will upgrade all packages associated with Composer. Installation can also be done, after downloading and verifying the installation script as above, with the following:php composer-setup.php. Using this will generate a composer.phar file in your current directory, which can be executed with ./composer.phar command.

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.

Also it specifies the version to be installed for the project. So, that you can avoid backward incompatibility issues while doing the project due to the unstable version of packages.

There is no need for you to create this project manually. Also, there may be the chance of getting syntax errors if you do in fact. The composer will create a composer.json file automatically while installing a dependency for the project using the require command.

You can add more dependencies using this command without having to edit the file. Here is the process involved with adding a package as the dependency in the project:

  • Find the library requirements of applications.
  • 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.

Let us see how it works in real-time using demo applications.

Here, we will give a sentence and that has to be converted into the slug (that is, a URL friendly string). This technique is used for converting the Page title into URL. First, change to home directory.

Let's start by creating a directory for our project. We'll call it slugify:

 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.

The down arrow indicates the number of downloads and the star indicates that how many times that the package got featured on the composer. You have to choose the package that has been downloaded more times. Since the package is downloaded by many people, it will be the stable version. Also, you have to find the package which is matching your expectation.

Here the slugify matches our requirement. If you read the package description, you will get to know what the packages are about. The packages will be listed with package name and vendor name. Also, they will be listed with the package name.

Here, we are going to use cocur/slugify.

Adding the Package

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

 $ composer require cocur/slugify

You will get the following output.

 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

You can see that the composer automatically chooses the version of packages to be added to the output. Also two files composer.json and composer.lock in the file.

Also, you can see a vendor directory.

 $ ls -l

You will get the following output:

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

We have already seen the use of the composer.json file. But let me tell you the use of the composer.lock file. The file contains the version information about the each package.

It will help people. In case if they copy your project, the file will help them to install the appropriate version of Composer. In that vendor directory, all the project dependencies are located.

You should not commit the vendor folder into version control. All you have to do is just include the composer.json and composer.lock files in the vendor folder.

If you tend to install a project and it contains the composer.json file, you have to use the composer install to download the project dependencies.

Updating the Project Dependencies

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

In other words, if there is any recent version available and it is compatible with the composer.json file, then the old dependency will be replaced with the new one. Also, the composer.lock file will be updated to make use of the changes.

Next, here is the command to update any specific dependency.

 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.

Beyond providing an easy and reliable way for managing project dependencies, it also establishes a new standard for sharing and discovering PHP packages created by the community.

0 0 0
Share on

Alibaba Clouder

2,599 posts | 764 followers

You may also like

Comments