This tutorial will show you how to install and use ** Composer ** on a ** Alibaba Cloud ECS ** instance installed on Ubuntu 16.04.
By Ghulam Qadir, author of Alibaba Cloud Community Blog.
Composer depends on the package of PHP The relationship management tool can be used by you and other developers alike to manage PHP-based projects and applications.
You've seen many PHP applications asking you if you have Composer installed on your system in order to install and run the package. That's how important and popular Composer is now.
This means that composer is often used to make it easier to install and update PHP applications. The Composer Dependency Tool makes it easy to install and manage packages. Composer is a list of required packages in a JSON file called composer.json Helps you manage.
Composer is compatible with Alibaba Cloud Elastic Compute Service (ECS) instances and works well. As a developer, I prefer to use Composer on Alibaba Cloud ECS instances with Ubuntu 16.04 installed. Alibaba Elastic Compute (ECS) is flexible and allows you to upgrade your hardware resources whenever traffic increases.
Follow the steps in the next section to learn how to install and use Composer on your Alibaba Cloud ECS instance installed with Ubuntu version 16.04.
Enter your root username and password from the SSH client (Putty) or the VNC console available on the Alibaba Cloud account dashboard. Log on as the root user.
Before we start this tutorial, again, we need the following:
-The Alibaba Cloud ECS instance is installed with Ubuntu version 16.04, has at least 2GB of RAM, has private networks enabled, and is not the root user. This can be set by following the initial ECS instance server configured in Ubuntu 16.04.
Before you download and install Composer, you need to make sure that all the dependencies are installed on your server.
First, run the following two commands to update the package manager cache.
# sudo apt-get update
# sudo apt-get upgrade
Next, you have to install the dependencies (curl, php-cli, php-mbstring, git, unzip). You can install everything with the following command.
sudo apt-get install curl php-cli php-mbstring git unzip
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 use the following command to install a specific version of PHP: sudo apt-get install
php7.2. If you are installing another version, such as 7.1 or 7.3, the numeric value. You can also change.
Composer provides an installer written in PHP. Since PHP is installed, execute the following command to install composer.
cd ~
curl -sS https://getcomposer.org/installer -o composer-setup.php
Then run a short PHP script to run the latest installer SHA on the Composer Public Keys / Signatures (https://composer.github.io/pubkeys.html?spm=a2c65.11461447.0.0.4b9d58c9bmcghQ) page. -384 Make sure it matches the hash. You need to assign the latest hash to 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; “
Should be the output.
Output
Installer verified
To install composer globally:
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 looks 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 the installation, run the composer
command and you should get output like 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.
. . .
Composer is now installed on your system.
Now, if you want to install a PHP-based application that requires Composer, just refer to this page to learn how to install Composer. Then continue to get the PHP application on Ubuntu.
You can also upgrade Composer by running the sudo composer self-update
command. This will upgrade all composer related packages. You can also install it by downloading and verifying the installation script as above, and then doing the following: php composer-setup.php
. It will generate a composer.phar file in your current directory and you can run it with the ./composer.phar
command.
You need the composer.json file to use composer in your project. The composer.json file is a file that tells composer to download the dependencies needed for your project.
Also specify the version to install in the project. This avoids backwards compatibility issues while running the project due to unstable package versions.
You don't have to create this project manually. Also, if you actually create it, you may get a syntax error. composer automatically creates the composer.json file while you install the project dependencies using the require command.
You can use this command to add dependencies without having to edit the file. This section describes the process of adding a package as a dependency to your project.
--Find the library requirements for your application. --Search for application library requirements. Search the official Composer repository, packagist.org, for a matching open source library. --Select the required package. --Run the composer require command to include the dependencies in the composer.json file and install the package.
Let's see how it works in real time using the demo application.
Here we need to give a sentence and convert it to a slug (that is, a URL-friendly string). This technique is used to convert page titles to URLs. First, change to your home directory.
First, let's create a directory for the project. I'll call it slugify here.
cd ~
mkdir slugify
cd slugify
Go to Packagist and search for packages that will help you generate slugs. Search for "slug" in the search bar. All related packages are listed here. Numbers are displayed in each description after the name.
The down arrow indicates the number of downloads, and the star indicates how many times the package was featured in the composer. Select the package that you download most often. Packages that are downloaded by many people will be stable. You also have to find a package that matches your expectations.
Here, slugify matches our requirements. You can see what a package looks like by reading the package description. The packages are listed by package name and vendor name. The package name is also listed.
Here we use cocur / slugify
.
To add a package, use the composer require command. Then generate a 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 selects the version of the package to add to the output. There are also two files, composer.json and composer.lock.
You can also check the 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've already seen how to use the composer.json file. However, this time I will show you how to use the composer.lock file. This file contains version information for each package.
If you copied the project, this file will help you install the correct version of Composer. The vendor directory has dependencies for all projects.
Do not commit vendor folders to version control. Just include the composer.json and composer.lock files in your vendor folder.
If you tend to install the project and it contains the composer.json file, you must use the composer installation to download the project dependencies.
Updating project dependencies is very easy. To update the dependencies, just use the composer update
command. What this command does is look for the latest version of the dependency.
In other words, if you have the latest version available and it is compatible with the composer.json file, the old dependencies will be replaced with the new ones. The composer.lock file is also updated to take advantage of the changes.
Next is the command to update a specific dependency.
composer update vendor/package vendor2/package2
This tutorial has shown you what you need to get started with Composer on an ECS instance installed with Ubuntu 16.04. Composer is a must-have tool for PHP developers.
Not only does it provide an easy and reliable way to manage project dependencies, but it also establishes a new standard for sharing and discovering PHP packages created by the community.
Recommended Posts