Install Composer on Ubuntu 14.04

迅800發表於2017-07-19

Step 1 — Installing the Dependencie

First, update the package manager cache by running:

$sudo apt-get update

Now, let’s install the dependencies. We’ll need curl in order to download Composer and php5-cli for installing and running it. git is used by Composer for downloading project dependencies. Everything can be installed with the following command:

$sudo apt-get install curl php5-cli git

Step 2 — Downloading and Installing Composer

Composer installation is really simple and can be done with a single command:

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

To test your installation, run:

$composer

Step 3 — Generating the composer.json File

Requiring a Package

$composer require cocur/slugify

Output
Using version ^1.3 for cocur/slugify
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing cocur/slugify (v1.3)
    Downloading: 100%         
Writing lock file
Generating autoload files

As you can see from the output, Composer automatically decided which version of the package should be used. If you check your project’s directory now, it will contain two new files: composer.json and composer.lock, and a vendor directory:

When installing a project that already contains a composer.json file, you need to run composer install in order to download the project’s dependencies.

Step 4 — Including the Autoload Script

Composer also provides an autoload script that you can include in your project to get autoloading for free. This makes it much easier to work with your dependencies and define your own namespaces.

The only thing you need to do is include the vendor/autoload.php file in your PHP scripts, before any class instantiation.

<?php
require __DIR__ . '/vendor/autoload.php';

use Cocur\Slugify\Slugify;

$slugify = new Slugify();

echo $slugify->slugify('Hello World, this is a long sentence and I need to make a slug from it!');

Step 5 — Updating the Project Dependencies

composerupdate

composer update
composer update vendor/package vendor2/package2


參考:
https://www.digitalocean.com/community/tutorials/how-to-install-and-use-composer-on-ubuntu-14-04

相關文章