How to install and use Composer on Ubuntu 20.04

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.

1. Install PHP Composer on Ubuntu

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.

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  

2. Getting Started with Composer##

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

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.

Three, summary##

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

How to install and use Composer on Ubuntu 18.04
How to install and use Composer on Ubuntu 20.04
How to install and use Docker on Ubuntu 20.04
How to install and use Curl on Ubuntu 18.04
How to install and use Wine on Ubuntu 18.04
How to install and use Composer on CentOS 8
How to install and use BaasBox on Ubuntu 14.04
How to install and use PostgreSQL on Ubuntu 16.04
How to install and use Docker on Ubuntu 16.04
How to install and use MySQL Workbench on Ubuntu 18.04
How to install Pycharm and Ipython on Ubuntu 16.04/18.04
How to install and configure NATS on Ubuntu 16.04
How to install and configure Gogs on Ubuntu 18.04
How to install and use Docker on CentOS 7
How to install and configure Cyberpanel on Ubuntu 18.04
How to install and secure phpMyAdmin on Ubuntu 16.04
How to install and configure ownCloud on Ubuntu 16.04
How to install and configure ownCloud on Ubuntu 16.04
How to install and configure GitLab on Ubuntu 18.04
How to install and configure Ansible on Ubuntu 18.04
How to install and secure phpMyAdmin on Ubuntu 16.04
How to install and configure Elasticsearch on Ubuntu 16.04
How to install and configure PostGIS on Ubuntu 14.04
How to install and configure VNC on Ubuntu 18.04
How to install and configure Sphinx on Ubuntu 16.04
How to install and configure OrientDB on Ubuntu 14.04
How to install and use Curl on CentOS 8
How to install and configure AppScale on Ubuntu 12.04
How to install and configure PostGIS on Ubuntu 14.04
How to install Ruby on Ubuntu 20.04
How to install Memcached on Ubuntu 20.04
How to install Java on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Nginx on Ubuntu 20.04
How to install Apache on Ubuntu 20.04
How to install Git on Ubuntu 20.04
How to install Node.js on Ubuntu 16.04
How to install MySQL on Ubuntu 20.04
How to install Vagrant on Ubuntu 20.04
How to install PostgreSQL on Ubuntu 16.04
How to install Git on Ubuntu 20.04
How to install Memcached on Ubuntu 18.04
How to install Jenkins on Ubuntu 16.04
How to install MemSQL on Ubuntu 14.04
How to install MongoDB on Ubuntu 16.04
How to install Mailpile on Ubuntu 14.04
How to install Skype on Ubuntu 20.04
How to install Jenkins on Ubuntu 20.04
How to install Python 3.8 on Ubuntu 18.04
How to install KVM on Ubuntu 18.04
How to install KVM on Ubuntu 20.04
How to install opencv3.0.0 on ubuntu14.04
How to install Anaconda on Ubuntu 20.04
How to install Jenkins on Ubuntu 18.04
How to install Apache on Ubuntu 20.04
How to install R on Ubuntu 20.04
How to install Moodle on Ubuntu 16.04
How to install Solr 5.2.1 on Ubuntu 14.04
How to install Teamviewer on Ubuntu 16.04