Home >Development Tools >composer >Four steps to teach you how to install and use Composer on Debian11!
This article is written by the tutorial column of composer to introduce to you step by step how to install and use Composer on Debian11. It is very detailed ~ I hope it will be helpful to friends who need it. !
Composer is a popular PHP dependency management tool, mainly to facilitate the installation and update of project dependencies. Composer works by checking what other packages a particular project depends on and then installing them for you with the appropriate versions based on the project requirements. Composer is also commonly used to bootstrap new projects based on popular PHP frameworks such as Symfony and Laravel.
In this guide, you will install and use Composer on a Debian 11 server.
To follow this tutorial, you need to set up a Debian 11 server, include a non-root user sudo
and enable the firewall. You can do this by following our Debian 11 initial server setup guide.
In addition to the dependencies that may already be included on your Debian 11 system, Composer requires php-cli
to run the command line to execute the PHP script, and unzip
to extract the compressed file.
First update the package manager's cache.
sudo apt update
Next, install the dependencies. You will need curl
to download Composer and php-cli
to install and run it. The php-mbstring
package is required to provide functionality for a library you will be using in this tutorial. git
is used by Composer to download project dependencies, while unzip
is used to extract compressed packages. Everything can be installed using the following commands.
sudo apt install curl php-cli php-mbstring git unzip
After installing all the dependencies, you can now install Composer.
Composer provides an installer written in PHP. You'll download it, verify that it's not corrupt, and then use it to install Composer.
First, make sure you are in your home directory.
cd ~
Then, use curl
to retrieve the installer.
curl -sS https://getcomposer.org/installer -o composer-setup.php
Next, verify that the installer matches the SHA-384 hash of the latest installer on the Composer Public Key/Signature page. To facilitate the verification step, you can use the following command to programmatically get the latest hash value from the composer page and store it in a shell variable.
HASH=`curl -sS https://composer.github.io/installer.sig`
To output the obtained values, run.
echo $HASH
Output55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae复制代码
Now execute the following PHP code, as provided in the Composer download page, to verify that the installation script can run safely.
php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Your output will be the following.
OutputInstaller verified
If you receive the message Installer corrupt
, you need to download the installation script again and verify that you are using the correct hash. Then run the command to verify the installer again. Once you have a verified installer, you are ready to proceed.
To install composer
globally, use the following command to download and install Composer as a system-wide command named composer
in /usr/ local/bin
.
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Your output will be the following.
OutputAll settings correct for using Composer Downloading... Composer (version 2.3.10) successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composer
Test your installation by running this command.
composer
Then your output will show the Composer version and parameters, similar to the following.
Output ______ / ____/___ ____ ___ ____ ____ ________ _____ / / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/ / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/ /_/ Composer version 2.3.10 2022-07-13 15:48:23Usage: command [options] [arguments] Options: -h, --help Display help for the given command. When no command is given display help for the list command -q, --quiet Do not output any message -V, --version Display this application version --ansi|--no-ansi Force (or disable --no-ansi) ANSI output -n, --no-interaction Do not ask any interactive question --profile Display timing and memory usage information --no-plugins Whether to disable plugins. --no-scripts Skips the execution of all scripts defined in composer.json file. -d, --working-dir=WORKING-DIR If specified, use the given directory as working directory. --no-cache Prevent use of the cache -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug. . .
This verifies that Composer has been successfully installed on your system and is available throughout the system.
**Note:** If you prefer to have a separate Composer executable for each project you host on this server, you can install it locally on a per-project basis. Users of NPM will be familiar with this approach. This method is also useful when your system user does not have permission to install software system-wide.
To do this, use the command php composer-setup.php
. This will generate a composer.phar
file in your current directory. You can use the ./composer.phar command
.
Now that you have Composer installed, follow the In one step you'll learn how to use Composer to manage PHP dependencies.
PHP projects often rely on external libraries, and managing these dependencies and their versions can be tricky. Composer solves this problem by tracking your dependencies and making it easier for others to install them.
In order to use Composer in your project, you need a composer.json
file. The composer.json
file tells Composer the dependencies it needs to download for your project, and the versions of each package it is allowed to install. This is important to maintain consistency in your project and avoid installing an unstable version that may cause backwards compatibility issues.
You do not need to create this file manually as doing so is error-prone and may result in syntax errors. When you use the composer require
command to add a dependency to your project, Composer automatically generates the composer.json
file. You can add additional dependencies using the same method without manually editing this file.
使用Composer在项目中安装一个包作为依赖的过程包括以下步骤。
composer require
,在composer.json
文件中包含该依赖性,并安装该软件包。你可以用一个演示应用程序来测试一下。
这个应用程序的目标是将一个给定的句子转换成一个叫做slug的URL友好字符串。slug通常用于将页面标题转换为URL路径,例如本教程中URL的最后部分。
首先导航到你的主目录。
cd ~
然后创建一个目录。将其称为slugify
。
mkdir slugify
创建完毕后,换到该目录中。
cd slugify
现在是时候在Packagist.org上搜索一个可以帮助生成蛞蝓的软件包。如果你在Packagist上搜索 "slug "这个词,你会得到一个与下面类似的结果。
列表中每个软件包的右侧会有两个数字。箭头朝下的图标旁边的数字代表该软件包被安装了多少次,而带星形图标的数字代表软件包在GitHub上被加了多少次星。你还可以根据这些数字对搜索结果重新排序。一般来说,安装次数多、星级多的软件包往往更稳定,因为有很多人在使用它们。检查软件包的描述是否相关也很重要,以确保它是你所需要的。
对于这个教程,你需要一个字符串到蛞蝓的转换器。从搜索结果来看,软件包cocur/slugify
是一个很好的匹配,有合理数量的安装和星级。
Packagist上的软件包有一个供应商名称和一个软件包名称。每个包都有一个唯一的标识符(命名空间),其格式与GitHub用于其存储库的格式相同,即 vendor/package
.你要安装的库使用命名空间cocur/slugify
。你需要这个命名空间,以便在你的项目中需要该包。
现在你知道了你要安装的软件包,运行composer require
,把它作为一个依赖项包括进去,同时为项目生成composer.json
文件。
composer require cocur/slugify
当Composer下载依赖关系时,你的输出将返回如下。
OutputUsing version ^4.1 for cocur/slugify ./composer.json has been created Running composer update cocur/slugify Loading composer repositories with package information Updating dependencies Lock file operations: 1 install, 0 updates, 0 removals - Locking cocur/slugify (v4.1.0) Writing lock file Installing dependencies from lock file (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Downloading cocur/slugify (v4.1.0) - Installing cocur/slugify (v4.1.0): Extracting archive Generating autoload files
正如这个输出所表明的,Composer会自动决定使用哪个版本的软件包。如果你现在检查你的项目目录,它将包含两个新文件:composer.json
和composer.lock
,以及一个vendor
目录。
ls -l
Outputtotal 12-rw-r--r-- 1 sammy sammy 59 Aug 2 22:19 composer.json-rw-r--r-- 1 sammy sammy 3462 Aug 2 22:19 composer.lockdrwxr-xr-x 4 sammy sammy 4096 Aug 2 22:19 vendor
composer.lock
文件是用来存储每个已安装软件包的版本信息的。它也确保在别人克隆你的项目并安装其依赖项时使用相同的版本。vendor
目录是项目依赖项所在的地方。vendor
文件夹不需要提交到版本控制中,你只需要包括composer.json
和composer.lock
文件。
当安装一个已经包含composer.json
文件的项目时,运行composer install
,以便下载项目的依赖项。
接下来,查看版本约束。如果你检查你的composer.json
文件的内容,你会有类似下面的内容。
cat composer.json
Output{ "require": { "cocur/slugify": "^4.1" }}
你可能会注意到在composer.json
的版本号之前有一个特殊的字符^
。Composer支持几种不同的约束和格式来定义所需的软件包版本,以提供灵活性,同时也保持你的项目稳定。自动生成的composer.json
文件所使用的圆点(^
)运算符是推荐的运算符,以实现最大的互操作性,遵循语义版本学。在这种情况下,它将4.1定义为最小兼容版本,并允许更新到5.0以下的任何未来版本。
一般来说,你不需要在你的composer.json
文件中篡改版本约束。然而,有些情况可能需要你手动编辑约束。例如,当你所需要的库的一个主要新版本发布时,你想要升级,或者当你想要使用的库不遵循语义版本划分时。
下面是一些例子,让你更好地了解Composer版本约束的工作方式。
约束 | 意义 | 允许的版本示例 |
---|---|---|
^1.0 | >= 1.0 < 2.0 | 1.0, 1.2.3, 1.9.9 |
^1.1.0 | >= 1.1.0 < 2.0 | 1.1.0, 1.5.6, 1.9.9 |
~1.0 | >= 1.0 < 2.0.0 | 1.0, 1.4.1, 1.9.9 |
~1.0.0 | >= 1.0.0 < 1.1 | 1.0.0, 1.0.4, 1.0.9 |
1.2.1 | 1.2.1 | 1.2.1 |
1.* | >= 1.0 < 2.0 | 1.0.0, 1.4.5, 1.9.9 |
1.2.* | >= 1.2 < 1.3 | 1.2.0, 1.2.3, 1.2.9 |
要想更深入地了解Composer的版本约束,请查阅官方文档。
接下来,你将学习如何用Composer自动加载依赖项。
由于PHP本身不会自动加载类,Composer提供了一个自动加载脚本,你可以把它包含在你的项目中,让自动加载发挥作用。这在处理你的依赖关系时很有帮助。
你唯一需要做的是在任何类实例化之前在你的PHP脚本中包含vendor/autoload.php
文件。这个文件在你添加第一个依赖项时由Composer自动生成。
你可以在你的应用程序中测试它。创建该文件test.php
,并在你喜欢的文本编辑器中打开它。这里使用的是nano
。
nano test.php
添加以下代码,引入vendor/autoload.php
文件,加载cocur/slugify
依赖关系,并创建一个slug。
test.php
<?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!');<p>保存该文件并退出你的编辑器。如果你使用的是<code>nano</code> ,你可以按<code>CTRL + X</code> ,然后按<code>Y</code> 和<code>ENTER</code> 来完成。</p> <p>现在运行该脚本。</p> <pre class="brush:php;toolbar:false">php test.php
这将产生以下输出。
Outputhello-world-this-is-a-long-sentence-and-i-need-to-make-a-slug-from-it
当新版本出来时,依赖关系需要更新,所以你将在最后一步中学习如何处理这个问题。
每当你想把你的项目依赖关系更新到最新的版本时,运行update
命令。
composer update
这将检查你在项目中作为需求添加的库的较新版本。如果发现一个新的版本,并且它与composer.json
文件中定义的版本约束兼容,Composer将替换之前安装的版本。composer.lock
文件将被更新以反映这些变化。
你也可以像下面这样指定一个或多个特定的库来更新它们。
composer update vendor/package vendor2/package2
请确保在你更新你的依赖关系后提交你的composer.json
和composer.lock
文件的变化,这样无论谁在项目中工作,都可以访问相同的软件包版本。
Composer是每个PHP开发者都应该拥有的强大工具。在本教程中,你在 Debian 11 上安装了 Composer 并在一个项目中使用了它。你现在知道如何安装和更新依赖关系了。
除了提供一个可靠的方法来管理项目的依赖关系外,Composer 还建立了一个新的标准来分享和发现由社区创建的 PHP 包。
The above is the detailed content of Four steps to teach you how to install and use Composer on Debian11!. For more information, please follow other related articles on the PHP Chinese website!