This article was first published in: https://www.itcoder.tech/posts/how-to-install-and-use-composer-on-ubuntu-20-04/
Composer is a PHP dependency manager (similar to Node.js's npm, or Python's pip).
Composer will pull all the PHP packages your project depends on and manage them for you. It is used by all modern PHP frameworks and platforms, such as Laravel, Symfony, Drupal, and Magento 2.
This guide explains how to install and use Composer on Ubuntu 20.04.
Before installing Composer, make sure you have installed the necessary dependencies on your system:
sudo apt update
sudo apt install wget php-cli php-zip unzip
Composer provides an installer written in PHP that we can use to install Composer. Use wget
to download the installer:
wget -O composer-setup.php https://getcomposer.org/installer
The above command will save the file as composer-setup.php
in the current folder.
Composer is a simple CLI application file, and it can be installed globally or as part of a project. Global installation requires sudo permissions.
/usr/local/bin
directory:sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Output:
All settings correct for using Composer
Downloading...Composer(version 1.10.7) successfully installed to:/usr/local/bin/composer
Use it: php /usr/local/bin/composer
Now you can use Composer by running composer
in your terminal.
sudo php composer-setup.php --install-dir=/path/to/project
This will download a file named composer.phar
in the root directory of your project. To use Composer, switch to the project directory and run php composer.phar
When a new Composer version is available, you can use the following command to upgrade:
sudo composer self-update
Now that Composer has been installed on your Ubuntu system, let's take a look at how to use Composer to create a PHP project.
The first step is to create the project root directory and switch to this directory:
mkdir ~/my-first-composer-project
cd ~/my-first-composer-project
In this example, we will use a PHP package called carbon
to create a sample application and print the current time.
Run the following command to initialize a new Composer project and install the carbon package:
composer require nesbot/carbon
Output:
Using version ^2.35for nesbot/carbon
. /composer.json has been created
Loading composer repositories withpackage information
Updating dependencies(including require-dev)
Package operations:5 installs,0 updates,0 removals
- Installing symfony/translation-contracts(v2.1.2):Downloading(100%)- Installing symfony/polyfill-php80(v1.17.0):Downloading(100%)- Installing symfony/polyfill-mbstring(v1.17.0):Downloading(100%)- Installing symfony/translation(v5.1.2):Downloading(100%)- Installing nesbot/carbon(2.35.0):Downloading(100%)
Writing lock file
Generating autoload files
5 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
As the output shows, Composer created the composer.json
file, downloaded, and installed carbon and all dependent packages.
If you list your project directory, you will see that it contains two files composer.json
and composer.lock
, and there is a vendor
directory.
ls -l
- rw-rw-r--.1 vagrant vagrant 60 Mar 2718:05 composer.json
- rw-rw-r--.1 vagrant vagrant 11135 Mar 2718:06 composer.lock
drwxrwxr-x.6 vagrant vagrant 82 Mar 2718:06 vendor
vendor
is the directory where the project dependent software packages are stored
composer.lock
is a file that retains all installed packages and their version numbers, and locks the project to the specified version.
composer.json
is a file used to describe PHP projects, including PHP dependencies and other metadata.
All PHP packages that can be installed via Composer are listed in Packagist.
Composer has the ability to load automatically, which allows you to use PHP classes without using require
and include
declarations.
Create a test file named testing.php
and add the following code:
<? php
require __DIR__ .'/vendor/autoload.php';
use Carbon\Carbon;printf("Now: %s", Carbon::now());
Let's analyze the code line by line.
The /vendor/autoload.php
file is automatically generated by Composer, and all libraries can be loaded automatically.
The next line creates the association of Carbon
, and finally uses Carbon's now
method to print out the current time.
Enter the following command to run the script:
php testing.php
The output will look like this:
Now:2020-06-1720:41:04
Later, if you want to upgrade your PHP package, you can simply run:
composer update
In the above command, we will check whether all the installed packages have newer versions, and if there are newer versions, Composer will upgrade the packages.
We have shown you how to install Composer on Ubuntu 20.04 and how to use it to create a basic PHP project.
For more information about Composer, please visit Composer Official Documentation Page.
If you have any questions, please contact us in the following ways:
WeChat:
WeChat group: add the above WeChat, remark the WeChat group
QQ: 3217680847
QQ Group: 82695646
Recommended Posts