Skip to content
justgreatwebsites.com
User Avatar
April 2nd, 2023
·
8 min read

A Guide to Setting Up PHP and Composer in Ubuntu

Abstract illustration of orchestra musicians on stage.

This post will guide you step-by-step through the process of setting up PHP and Composer on Ubuntu. For those new to PHP, Composer is its widely-used dependency manager, akin to how NPM manages JavaScript dependencies.

While my setup involves Ubuntu on Windows via WSL2, these instructions are equally applicable to traditional Linux environments. By the conclusion of this guide, you'll have PHP, PHP Curl, and Composer installed and primed for your development projects.

Additionally, I'll offer some bonus instructions on installing specific PHP packages for those interested in developing with Laravel and Statamic. Let's get started!

Update Package List

Updating the package list on our Ubuntu system ensures that we have the latest information on available packages and their dependencies, which helps prevent potential conflicts or installation issues.

sudo apt-get update

Install PHP

Next, we'll install PHP, the popular scripting language that powers many websites and applications (including Laravel and Statamic).

sudo apt-get install -y php8.1
php -v

Install PHP Curl

In this step, we'll install PHP Curl, a PHP extension that facilitates interaction with web services and APIs in web applications. PHP Curl provides a flexible way to handle HTTP requests and responses, allowing your PHP applications to communicate with remote servers, fetch data, and perform API calls seamlessly. As a commonly required extension by PHP frameworks and libraries, having PHP Curl installed in your development environment is essential.

To install PHP Curl, run the following command:

sudo apt-get install php-curl

Install Composer

Next, we'll install Composer, the dependency manager for PHP projects. Composer streamlines the process of managing packages and their versions, making it easier to develop and maintain PHP applications. For further reading on the installation process, you can visit the official site. From the official site, we can copy the following commands:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Install Composer Globally

To make Composer available system-wide, we'll move the Composer binary to the /usr/local/bin directory. This allows us to call Composer from any location within your system, simplifying its usage.

sudo mv composer.phar /usr/local/bin/composer

Test Composer Installation

Now that Composer is installed, let's test it to ensure everything is working as expected.

composer

After running the composer command in the terminal, we should see output similar to the following:

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.1.9 2021-11-23 20:34:46

Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as the 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

Available commands:
  about                Shows information about Composer
  archive              Creates an archive of this composer package
  browse               Opens the package's repository URL or homepage in your browser
  check-platform-reqs  Check that platform requirements are satisfied.
  ...

Configure Composer for Global Access

To easily access and execute global Composer packages from any location within our system, we'll add Composer's binary directory to our PATH.

First, let's explore the global configuration settings for Composer by running the following command:

composer config --list --global

This command displays the global settings for Composer, such as process timeout, repositories, and cache directories. It provides useful context for understanding how Composer is configured on our system.

Next, find the Composer binary directory path by running:

echo $(composer config --global home)/vendor/bin

This command will return the path to the binary directory, which is typically located at ~/.config/composer/vendor/bin on Linux systems.

Next, let’s open our .bashrc file using a text editor, such as Nano or VIM:

nano ~/.bashrc

At the end of the file, add the following line, replacing PATH_TO_COMPOSER_BIN with the path you found in the previous step:

export PATH=${PATH}:PATH_TO_COMPOSER_BIN

For example, if your Composer binary directory is ~/.config/composer/vendor/bin, the line should look like this:

export PATH=${PATH}:~/.config/composer/vendor/bin

Save and close the file. To apply the changes immediately, execute the contents the .bashrc file with the source command:

source ~/.bashrc

Now, we can execute global Composer packages from any location within your system.

Optional — Additional Setup for Statamic Applications

If you plan on using the excellent Laravel-based Statamic CMS, you'll also need to install some additional PHP extensions and packages to ensure compatibility:

sudo apt-get upgrade
sudo apt install php-common php-fpm php-json php-mbstring zip unzip php-zip php-cli php-xml php-tokenizer -y

Wrapping Up

To summarise, we began by updating the package list, then proceeded to install PHP, PHP Curl, and Composer. We also installed Composer globally and configured it for ease-of-use. With PHP and Composer installed, you're now well-equipped to manage and develop PHP projects with greater efficiency. I hope this guide has been helpful in setting up your development environment. Good luck with your projects, and happy coding!