centos系統下安裝使用composer教程

來源:互聯網
上載者:User

I. 安裝

cd path-to-your-project
curl -sS https://getcomposer.org/installer | php

# Composer successfully installed to: /tmp/composer.phar
# Use it: php composer.phar

如果在此提示,那是因為php沒有在系統的環境變數$PATH中。

command not found: php

解決方案:

為當前php的路徑建立一個軟串連

ln -s /usr/local/php/bin/php /usr/local/bin/php

仔細察看composer.phar的許可權,應該是755,即-rwxr-xr-x,說明這個檔案其他人都有執行許可權,所以如果我們將其移動至/usr/local/bin/中,即可全域可用!

mv composer.phar /usr/local/bin/composer

II. 使用

我們先使用-h查看協助

composer -h
Usage:
 help [--xml] [--format="..."] [--raw] [command_name]

Arguments:
 command               The command to execute
 command_name          The command name (default: "help")

Options:
 --xml                 To output help as XML
 --format              To output help in other formats (default: "txt")
 --raw                 To output raw command help
 --help (-h)           Display this help message
 --quiet (-q)          Do not output any message
 --verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
 --version (-V)        Display this application version
 --ansi                Force ANSI output
 --no-ansi             Disable ANSI output
 --no-interaction (-n) Do not ask any interactive question
 --profile             Display timing and memory usage information
 --working-dir (-d)    If specified, use the given directory as working directory.

Help:
 The help command displays help for a given command:

   php /usr/local/bin/composer help list

 You can also output the help in other formats by using the --format option:

   php /usr/local/bin/composer help --format=xml list

 To display the list of available commands, please use the list command.
看來想要找到所有的命令需要運行php /usr/local/bin/composer help list

我們試試吧

php /usr/local/bin/composer list
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.0-dev (4f80e7ff68466cab970c22b425725b8058c32970) 2015-06-09 00:03:48

Usage:
 command [options] [arguments]

Options:
 --help (-h)           Display this help message
 --quiet (-q)          Do not output any message
 --verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
 --version (-V)        Display this application version
 --ansi                Force ANSI output
 --no-ansi             Disable ANSI output
 --no-interaction (-n) Do not ask any interactive question
 --profile             Display timing and memory usage information
 --working-dir (-d)    If specified, use the given directory as working directory.

Available commands:
 about            Short information about Composer
 archive          Create an archive of this composer package
 browse           Opens the package's repository URL or homepage in your browser.
 clear-cache      Clears composer's internal package cache.
 clearcache       Clears composer's internal package cache.
 config           Set config options
 create-project   Create new project from a package into given directory.
 depends          Shows which packages depend on the given package
 diagnose         Diagnoses the system to identify common errors.
 dump-autoload    Dumps the autoloader
 dumpautoload     Dumps the autoloader
 global           Allows running commands in the global composer dir ($COMPOSER_HOME).
 help             Displays help for a command
 home             Opens the package's repository URL or homepage in your browser.
 info             Show information about packages
 init             Creates a basic composer.json file in current directory.
 install          Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
 licenses         Show information about licenses of dependencies
 list             Lists commands
 remove           Removes a package from the require or require-dev
 require          Adds required packages to your composer.json and installs them
 run-script       Run the scripts defined in composer.json.
 search           Search for packages
 self-update      Updates composer.phar to the latest version.
 selfupdate       Updates composer.phar to the latest version.
 show             Show information about packages
 status           Show a list of locally modified packages
 update           Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file.
 validate         Validates a composer.json

III. 國內鏡像

為了更好的使用composer,我們使用phpcomposer國內鏡像加速,具體使用方法很簡單,你可以參考官網的使用說明!

IV. 在項目中使用composer
比如我們的項目需要使用laravel與monolog,怎麼辦呢?

首先,在packagist.org中搜尋需要的包名,這樣我們得到了包名monolog/monolog,laravel同理。

 

其次,我們需要確定版本,我們看到官網的提示:

{
    "require": {
        "vendor/package": "1.3.2",   # 明確指定版本
        "vendor/package2": "1.*",    # 使用萬用字元,方便以後執行更新
        "vendor/package2": "~1.3",   # 是指 1.3<= X <2.0的版本
        "vendor/package3": ">=2.0.3", # 指定一個範圍
        "vendor/package3": ">=2.0.3-dev" # 預設composer會取穩定版,但是如果指明-dev,則會下載dev版
    }
}

更多使用方法請移步Package Versions

我們在項目根目錄下建立檔案 composer.json

{
    "require": {
        "monolog/monolog": "1.0.*"
    }
}

你還可以自動產生此composer.json,通過命令 composer init --require "monolog/monolog:1.0.*" -n

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.