Atlantic.Net Blog

How to Install and Use Composer on Ubuntu 20.04

Hitesh Jethva
by Atlantic.Net (272 posts) under Dedicated Server Hosting, Tutorials
0 Comments

Composer is a package management system and dependency manager for PHP. It checks your project requirements and installs all the required dependencies. Composer is a simple, reliable, and very useful tool that helps developers to manage and integrate packages and libraries into their PHP-based projects.

In this tutorial, we will show you how to install and use Composer on Ubuntu 20.04 server.

Prerequisites

  • A fresh Ubuntu 20.04 server on the Atlantic.Net Cloud Platform
  • A root password configured on your server

Step 1 – Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing Ubuntu 20.04 as the operating system with at least 2GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged in to your Ubuntu 20.04 server, run the following command to update your base system with the latest available packages.

apt-get update -y

Step 2 – Install PHP and Other Dependencies

Before installing Composer, you will need to install PHP and other required dependencies on your server.

You can install all of them with the following command:

apt-get install php php-cli php-mbstring curl gnupg2 git unzip -y

After installing all the packages, you can proceed to the next step.

Step 3 – Install Composer

Composer provides a PHP-based installation script to install the Composer to your system.

First, download the Composer installation script with the following command:

curl -sS https://getcomposer.org/installer -o composer-setup.php

Next, you will need to verify whether the downloaded script matches with the SHA-384 hash for the latest installer found on the Composer signature page.

Composer Hash Page

To do so, copy the hash from that page and store it in the shell variable.

HASH=756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3

Next, run the following command to verify the downloaded script:

php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

If everything is correct, you should get the following output:

Installer verified

Next, run the following script to install Composer globally:

php composer-setup.php --install-dir=/usr/local/bin --filename=composer

Once the installation has been completed, you should get the following output:

All settings correct for using Composer
Downloading...

Composer (version 2.1.3) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

You can now check the Composer version using the following command:

composer --version

You should see the following output:

Composer version 2.1.3 2021-06-09 16:31:20

Step 4 – How to Use Composer

At this point, Composer is installed on your server. Now we will show you how to create a project with Composer.

First, create a new directory for your project:

mkdir myproject

Next, change the directory to myproject:

cd myproject

Next, create a PHP-based application that prints the current time.

composer require nesbot/carbon

The above command will download and install carbon with all required dependencies and creates the composer.json file:

Installing dependencies from lock file (including require-dev)
Package operations: 6 installs, 0 updates, 0 removals
  - Downloading symfony/translation-contracts (v2.4.0)
  - Downloading symfony/polyfill-mbstring (v1.23.0)
  - Downloading symfony/translation (v5.3.3)
  - Downloading nesbot/carbon (2.50.0)
  - Installing symfony/translation-contracts (v2.4.0): Extracting archive
  - Installing symfony/polyfill-php80 (v1.23.0): Extracting archive
  - Installing symfony/polyfill-mbstring (v1.23.0): Extracting archive
  - Installing symfony/deprecation-contracts (v2.4.0): Extracting archive
  - Installing symfony/translation (v5.3.3): Extracting archive
  - Installing nesbot/carbon (2.50.0): Extracting archive
3 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files

Run the following command to verify all files created by Composer:

ls -l

You should see the following output:

-rw-r--r-- 1 root root    60 Jul 15 04:52 composer.json
-rw-r--r-- 1 root root 18155 Jul 15 04:52 composer.lock
drwxr-xr-x 6 root root  4096 Jul 15 04:52 vendor

Now, create a new file named app.php:

nano app.php

Add the following code:

<?php

require __DIR__ . '/vendor/autoload.php';

use Carbon\Carbon;

printf("Now: %s", Carbon::now());
?>

Save and close the file, then run the app.php with the following command:

php app.php

This will print the current time of your system:

Now: 2021-07-15 04:54:37

If you want to update the project, run the following command:

composer update

This will check for newer versions of the installed packages and match them with all packages inside composer.json then update the package.

Conclusion

In the above guide, you learned how to install Composer on Ubuntu 20.04. You also learned how to create a PHP-based project with Composer. Get started with Composer on your dedicated server from Atlantic.Net, and for more information, visit the Composer official documentation.

Get A Free To Use Cloud VPS

Free Tier Includes:
G3.2GB Cloud VPS Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


Looking for a Hosting Solution?

We Provide Cloud, Dedicated, & Colocation.

  • Seven Global Data Center Locations.
  • Flexible Private, Public, & Hybrid Hosting.
  • 24x7x365 Security, Support, & Monitoring.
Contact Us Now! Med Tech Award FTC
SOC Audit HIPAA Audit HITECH Audit

Recent Posts

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G3.2GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2323 Bryan Street,

Dallas, Texas 75201

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4

Canada

London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom

Resources