How to Install and Use Composer on Ubuntu 16.04

Alibaba Cloud
Aug 13 · 7 min read

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:

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

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:

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:

The output of which should be:

To install composer globally, use the following:

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

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

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:

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.

You will get the following output.

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.

You will get the following output:

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 updatecommand. 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.

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.

Original Source

Alibaba Cloud

Written by

A global Cloud Computing and AI company, a subsidiary of Alibaba Group. Website: https://www.alibabacloud.com/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade