Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Install and Use PHP Composer on Ubuntu 20.04 - DesignLinux

designlinux 0 Comments

Composer is a dependency manager for PHP (similar to npm for Node.js or pip for Python).

With Composer, you can specify the PHP libraries your project depends on, and it will pull and manage all libraries and dependencies for you. Composer is used in all modern PHP frameworks and platforms such as Laravel, Symfony, Drupal, and Magento.

This tutorial explains how to install and use Composer on Ubuntu 20.04.

Installing PHP Composer on Ubuntu #

Before installing Composer, ensure that you have all the necessary requirements installed on your system:

sudo apt updatesudo apt install wget php-cli php-zip unzip

Composer offers an installer written in PHP that we’ll use to install Composer. Use wget to download the installer:

wget -O composer-setup.php https://getcomposer.org/installer

The command above will save the file as composer-setup.php in the current working directory.

Composer is a single file CLI application and can be installed either globally or as part of the project. The global installation requires sudo privileges.

  • To install Composer globally as a system-wide command that will be available for all users, simply place the file in a directory that is in the system PATH. Run the following command to install Composer in the /usr/local/bin directory:

    sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
    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

    You can now use Composer by running composer in your terminal

  • To install composer locally enter:

    sudo php composer-setup.php --install-dir=/path/to/project

    This will download a file named composer.phar in your project root directory. To use Composer navigate to the project directory and run php composer.phar

When a new Composer version is available, you can update your installation using the following command:

sudo composer self-update  

Getting Started with Composer #

Now that Composer is installed on your Ubuntu system, let’s see how to create a PHP project with Composer.

The first step is to create the project root directory and navigate to it:

mkdir ~/my-first-composer-projectcd ~/my-first-composer-project

In this example, we’ll use a PHP package called carbon to create a sample application that prints the current time.

Run the following command to initialize a new Composer project and install the carbon package:

composer require nesbot/carbon
Using version ^2.35 for nesbot/carbon
./composer.json has been created
Loading composer repositories with package 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 shown in the output, Composer creates the composer.json file and downloads and installs carbon and all its dependencies.

If you list your project’s directory, you will see that it contains two files composer.json and composer.lock, and a vendor directory.

ls -l
-rw-rw-r--. 1 vagrant vagrant    60 Mar 27 18:05 composer.json
-rw-rw-r--. 1 vagrant vagrant 11135 Mar 27 18:06 composer.lock
drwxrwxr-x. 6 vagrant vagrant    82 Mar 27 18:06 vendor
  • vendor is the directory where the project dependencies are stored.
  • composer.lock is a file that keeps information about all installed packages and their versions, locking the project to the specific versions.
  • composer.json is the file that describes your PHP project, including the PHP dependencies and other metadata.
All PHP packages installable with Composer are listed at Packagist.

Composer has autoload capabilities which allow us to use PHP classes without the need to require or include the files.

Create a 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 autoload all of the libraries.

Next line creates alias Carbon and the last line prints the current time using the Carbon now method.

Run the script by typing:

php testing.php

The output should look something like this:

Now: 2020-06-17 20:41:04

Later, if you need to update the project packages, enter:

composer update

The command above will check for newer versions of the installed packages, and if a newer version is found and the version constraint match with the one specified in the composer.json, Composer will update the package.

Conclusion #

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, visit the official documentation page.

If you have any questions, please leave a comment below.

composer php ubuntu

Related

Tags: composer, php, ubuntu

How to Enable HTTP/2.0 in Nginx

Prev Post

How to Cache Content in NGINX

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 2
  • 515
  • 1,055,287

DesignLinux.com © All rights reserved

Go to mobile version