composer

Composer User Guide

Install

Useful Configurations

# Add composer global install location to PATH.
vim ~/.profile

# set PATH so it includes composer bin if it exists
if [ -d "$HOME/.config/composer/vendor/bin" ] ; then
    PATH="$HOME/.config/composer/vendor/bin:$PATH"
fi

# Let root run composer through rt
alias rt='composer --no-plugins --no-scripts'

Switch to China Repo

It's difficult to use global repository in China, try below configuraiton before execute any composer command:

# Switch to China repository mirror
composer config -g repo.packagist composer https://packagist.phpcomposer.com

Or manually edit file: ~/.config/composer/config.json, add below configurations: 

"repositories": {
    "packagist": {
        "type": "composer",
        "url": "https://packagist.phpcomposer.com"
    }
}

Local proxy

If China Repo could not help composer to manage the packages more efficiently, try to install proxychains, leverage command proxychina composer, through a proxy to boost management:

sudo apt-get install -y proxychains

vim /etc/proxychains.conf

socks5 127.0.0.1 3080

Useful Commands

Command Function
composer update Update libraries
composer selfupdate Update Composer itself

Useful Cases

Drupal 8

composer create-project drupal-composer/drupal-project:8.x-dev FOLDER_NAME --stability dev --no-interaction

Laravel

composer create-project laravel/laravel FOLDER_NAME
Author: njun
njun's picture
Updated: 2019/12/19