Install PHP Composer on Ubuntu 22.04

In this guide, you will learn to Install and Use PHP Composer on Ubuntu 22.04.

According to the Composer website, Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage, so install and update, those libraries for you.

Steps To Install PHP Composer on Ubuntu 22.04

To complete this guide, you must log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can follow our guide on Initial Server Setup with Ubuntu 22.04.

Install Required Packages

To install PHP and its dependencies on Ubuntu 22.04, first, update your local package index with the following command:

sudo apt update

Now install the required packages with the following command:

sudo apt install php-cli unzip

When you are done, you can start to download and install Composer on Ubuntu 22.04.

Get Composer Installer on Ubuntu 22.04

The composer comes with an installer script written in PHP.

To download it, be sure you are in your home directory and run the following command to get the installer:

# cd ~
# curl -sS -o composer-setup.php

Here you need to get the latest hash from the composer page and store it in a shell variable with the following command:

HASH=`curl -sS`

You can use the following command to verify it:

echo $HASH

In your output you will see:


Then, you need to verify that the installation script is safe to run. For this, you can use the following command:

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

You should see this in your output:

Installer verified

Note: If in your output, you see the Installer corrupt, you need to download the installation script again and check that you are using the correct hash.

Then, repeat the verification process. When you have a verified installer, you can continue.

Download and Install Composer on Ubuntu 22.04

Now you can download and install Composer globally on Ubuntu 22.04 with the following command:

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

In your output, you will see something similar to this:

All settings correct for using Composer

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

You can test your installation with the following command:

  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
Composer version 2.5.1 2022-12-22 15:33:54

  command [options] [arguments]

  -h, --help                     Display help for the given command. When no command is given display help for the list command
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi|--no-ansi           Force (or disable --no-ansi) ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
      --no-scripts               Skips the execution of all scripts defined in composer.json file.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
      --no-cache                 Prevent use of the cache

It means that the Composer was successfully installed on Ubuntu 22.04 and is available system-wide.

Note: If you want to have separate Composer executables for each project you host on this server, you can install them locally.

This method is also useful when your system user doesn’t have permission to install software system-wide.

To do this you can use the “php composer-setup.php” command.

Let’s see how to use Composer on Ubuntu 22.04.

Use PHP Composer on Ubuntu 22.04

To use Composer in your project, you will need a “composer.json” file.

This file tells Composer which dependencies it needs to download for your project, and which versions of each package are allowed to be installed. You don’t need to create this file manually. Composer offers an interactive way to create a new composer.json file based on the user’s input.

It is a good choice if you plan on sharing your project later as a public package on Packagist.

The purpose of this application is to transform a given sentence into a URL-friendly string – a slug.

First, create a directory for your project. here we call it slugify:

# cd ~
# mkdir slugify
# cd slugify

It is not required, but you could now run a Composer init command on Ubuntu 22.04 to create a detailed composer.json file for your project.

Our project’s only objective is to show how to install dependencies with Composer. We will use a simpler composer.json file that will be auto-generated when we require our first package.

Now you can search for the term slug on Packagist which can help you generate the slug. You will see results similar to this:

Packagist-use Composer on Ubuntu 22.04

You’ll see two numbers on the right side of each package in the list.

The number on the top shows how many times the package was installed via Composer. And the number on the bottom shows how many times a package was starred on GitHub.

Packages with more installations and more stars tend to be more stable.

You need a string-to-slug converter. As you can see in your search result, the cocur/slugify package is a good match.

Use Composer Require command

Now you know which package you want to install. You can run the following command to include it as a dependency and also generate the composer.json file for your project:

composer require cocur/slugify
Using version ^3.0 for cocur/slugify
./composer.json has been created
Running composer update cocur/slugify
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
- Locking cocur/slugify (v3.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Downloading cocur/slugify (v3.0)
- Installing cocur/slugify (v3.0): Extracting archive
Generating autoload files

As you can see in your output, Composer automatically decided which version of the package to use.

Now you can check your project with the following command:

You will see that it contains two files the composer.json and the composer.lock, and a vendor directory.

ls -l
total 12
-rw-r--r-- 1 root root   59 Jan 16 08:58 composer.json
-rw-r--r-- 1 root root 3114 Jan 16 08:58 composer.lock
drwxr-xr-x 4 root root 4096 Jan 16 08:58 vendor

The composer.lock file is used to store information about which versions of each package are installed.

Also, it ensures the same versions are used if someone else clones your project and installs its dependencies.

The vendor directory is where the project dependencies are located.

You can check the version restriction. If you check the composer.json file on Ubuntu 22.04 you will see:

cat composer.json
    "require": {
        "cocur/slugify": "^3.0"

The caret (^) before the version number used by the auto-generated composer.json file is the recommended operator for maximum interoperability.

Here, it defines 3.0 as the minimum compatible version and allows updates to any future version under 4.0.

For more information about Composer version restrictions, you can visit the official documentation.

Let’s see how to load dependencies automatically with Composer.

Load Dependencies Automatically with Composer

Composer provides an autoload script that you can include in your project to get autoloading working for your project.

This file is automatically generated by Composer when you add your first dependency.

You only need to include the vendor/autoload.php file in your PHP scripts before any class instantiation.

Now open a new file named test.php with your favorite text editor, here we use vi editor:

vi test.php

Add the following code to the file:

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

use Cocur\Slugify\Slugify;

$slugify = new Slugify();

echo $slugify->slugify('Hello World, this is a long sentence and I need to make a slug from it!');

When you are done, save and close the file.

Now you can run the script with the following command:

php test.php

In your output you will see:


Dependencies need an update when new versions come out.

Use the Composer update command on Ubuntu 22.04 to update your project dependencies to more recent versions:

composer update

If a new version is found and it’s compatible with the version restriction defined in the composer.json file, the Composer will replace the previous version installed.

Also, you can update one or more specific libraries with the following command:

composer update vendor/package vendor2/package2

Note: After you update your dependencies be sure to check the composer.json and the composer.lock files within your version control system. So that others can install these newer versions too.


At this point, you have learned to Install and Use PHP Composer on Ubuntu 22.04.

Hope you enjoy it. You may be like these guides on the Orcacore website:

How To Install Podman on Ubuntu 22.04

Install Wekan Server on Ubuntu 22.04

Install and Configure Jekyll on Ubuntu 22.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *

Stay informed and not overwhelmed, subscribe now!