search
HomeDevelopment ToolscomposerInstallation and use of Composer under Windows/Linux (detailed explanation)

This article is provided by the composer tutorial column to introduce how to install and use Composer on Windows and Linux. I hope it will be helpful to friends in need!

Installation and use of Composer on Windows and Linux

1: Install Composer

1. First, please confirm that it is correct After installing PHP, open a command line window in Windows/open a terminal in Linux and execute php -v to check whether the version number is correctly output.

windows

PS E:\repository\work> php -v
PHP 7.0.32 (cli) (built: Sep 12 2018 15:54:08) ( ZTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies

centos

[root@jmsiteos7 ~]# php -v
PHP 7.2.13 (cli) (built: Dec  6 2018 23:18:37) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.13, Copyright (c) 1999-2018, by Zend Technologies

If php is installed but the environment variable is not set, you need to set the environment variable

windows

Installation and use of Composer under Windows/Linux (detailed explanation)

windows set environment variables

Exit and reopen the command line window and enter again php -vCheck the version number. If it is still abnormal, please check whether the php directory is correct

centos

vi /etc/profile
#文件地步输入如下两行
PATH=$PATH:/usr/local/php72/bin/:/usr/local/php72/sbin/
export PATH
#立即生效
source /etc/profile

Enter againphp -vCheck the version number. If it is still abnormal, please check whether the php directory is correct

2. Download Composer

php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"

3. Install Composer

php composer-setup.php

4. Delete the installer

php -r "unlink('composer-setup.php');"

5. Partial installation

The above steps to download Composer are completed correctly After that, you can copy the composer.phar file to any directory (such as the project root directory), and then use the php composer.phar command to use Composer!

6. Global installation

centos

Open the terminal and execute the following command to install the previously downloaded composer. phar Move the file to the /usr/local/bin/ directory:

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

windows

Enter the PHP installation directory and change composer.phar Copy to the PHP installation directory, create a new composer.bat file in the PHP installation directory, and save the following code into this file

@php "%~dp0composer.phar" %*

Reopen a command line window and try executing composer --version to see if the version number is correctly output

##windows

PS E:\repository\work> composer --version
Composer version 1.8.0 2018-12-03 10:31:16

centos

[root@jmsite ~]# composer --version
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Composer version 1.8.0 2018-12-03 10:31:16

This is just a demonstration. Composer cannot be executed as root. The official explanation is: Some Composer commands, including exec, install and update, allow third-party code to run in your executed on the system.

This comes from its "plugin" and "script" capabilities. Plugins and scripts have full access to the user account running Composer. Therefore, it is highly recommended to avoid running Composer as superuser/root.

7. Upgrade Composer

composer selfupdate

8. Set composer domestic source

composer config -g repo.packagist composer https://packagist.phpcomposer.com


2: Using Composer

##1. Create a

composer in the project directory based on composer.json

. json

file describing the dependencies of the project. <pre class='brush:php;toolbar:false;'>{ &quot;require&quot;: { &quot;monolog/monolog&quot;: &quot;1.2.*&quot; } }</pre> means you need to download any version of monolog starting from 1.2. Use the following command to install the code library

composer install

2. Based on the require command

If you find it troublesome to create the composer.json file, you can also use the require command during installation.

composer require monolog/monolog

composer will look for the code base information and automatically create the

composer.json

file and composer.lockFile

3. Update all code libraries in the project, or some specified code libraries

# 更新所有代码库
composer update
# 更新指定的代码库
composer update monolog/monolog
# 更新指定的多个代码库
composer update monolog/monolog phpaes/phpaes
# 还可以通过通配符匹配代码库
composer update monolog/monolog phpaes/*

4. Removing a code base cannot be removed if it is used by another code base

composer remove monolog/monolog

5. Search the source for the code base

composer search monolog

6. View code base information

# 显示已经安装的代码库
composer show
# 通过通配符进行筛选
composer show monolog/*
# 具体某个代码库的信息
composer show monolog/monolog

Automatic loading

In addition to downloading the code base, Composer also prepares an automatic loading file, which can load Composer All class files in the downloaded code base. To use it, you just need to add the following line of code to your project's bootstrap file

require &#39;vendor/autoload.php&#39;;
#
$log = new Monolog\Logger(&#39;name&#39;);
$log->pushHandler(new Monolog\Handler\StreamHandler(&#39;app.log&#39;, Monolog\Logger::WARNING));
$log->addWarning(&#39;Foo&#39;);

The above is the detailed content of Installation and use of Composer under Windows/Linux (detailed explanation). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:juejin. If there is any infringement, please contact admin@php.cn delete
Composer: A Comprehensive Guide to PHP Package ManagementComposer: A Comprehensive Guide to PHP Package ManagementMay 11, 2025 am 12:17 AM

Composer is a PHP dependency management tool that manages project dependencies through composer.json file. 1. Use composerinit to initialize the project. 2. Add dependencies such as composerrequireguzzlehttp/guzzle. 3. Advanced usage includes configuring private repositories and using script hooks. 4. Common errors such as dependency conflicts can be debugged through the composerwhy-not command. 5. Performance optimization is recommended to use composerinstall-prefer-dist and periodically update dependencies.

Composer in Action: Automating Dependency ResolutionComposer in Action: Automating Dependency ResolutionMay 10, 2025 am 12:22 AM

Composer simplifies dependency management of PHP projects through automated dependency parsing. 1) Read composer.json to resolve dependency requirements; 2) Build a dependency tree to handle version conflicts; 3) Download and install dependencies from Packagist to the vendor directory; 4) Generate composer.lock to ensure dependency consistency, thereby improving development efficiency.

AI and Composer: Enhancing Code Quality and DevelopmentAI and Composer: Enhancing Code Quality and DevelopmentMay 09, 2025 am 12:20 AM

In Composer, AI mainly improves development efficiency and code quality through dependency recommendation, dependency conflict resolution and code quality improvement. 1. AI can recommend appropriate dependency packages according to project needs. 2. AI provides intelligent solutions to deal with dependency conflicts. 3. AI reviews code and provides optimization suggestions to improve code quality. Through these functions, developers can focus more on the implementation of business logic.

What Defines a Composer: Understanding the RoleWhat Defines a Composer: Understanding the RoleMay 08, 2025 am 12:07 AM

Composers are people who express emotions, tell stories or express ideas by creating musical works, whose roles include conceiving musical structures, choosing instrument combinations, and working with other musicians. 1) Composers will go through four stages of conception, creation, modification and improvement during the creative process; 2) They need to master musical theories, such as harmony, counterpoint and melody, to ensure the quality and effect of the work.

Composer: Installing, Updating, and Managing DependenciesComposer: Installing, Updating, and Managing DependenciesMay 07, 2025 am 12:07 AM

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json and composer.lock files. 1. Install Composer: Run a specific command and move composer.phar to the system path. 2. Update Composer: Use composelself-update command. 3. Dependency management: add dependencies through the composerrequire command, automatically update relevant files and download packages.

AI-Powered Composer: Code Generation and AnalysisAI-Powered Composer: Code Generation and AnalysisMay 06, 2025 am 12:11 AM

AI plays an important role in code generation and analysis: 1) generate code through machine learning and NLP, such as GitHub’s Copilot; 2) perform code analysis, identify bugs and optimization suggestions, such as SonarQube; 3) improve development efficiency and code quality, but requires manual review and optimization.

Composer: AI-Powered Tools and IntegrationsComposer: AI-Powered Tools and IntegrationsMay 05, 2025 am 12:11 AM

Composer itself does not include AI capabilities, but can be enhanced by AI tools. 1) AI can analyze composer.json files, and it is recommended to optimize dependencies and predict version conflicts. 2) AI-driven platforms such as GitHubCopilot can provide real-time code suggestions to improve development efficiency. When using AI tools, you need to verify and adjust them in combination with actual situations.

The Attributes of a Successful ComposerThe Attributes of a Successful ComposerMay 04, 2025 am 12:13 AM

The key traits of a successful composer include: 1) rich creativity and imagination, 2) solid mastery of technical skills and tools. These traits are similar to creative and structured thinking in programming, helping composers realize creativity and optimize their work in music creation.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function