How to Install and Use Composer on Ubuntu 22.04: Step-by-Step Guide

To install and use Composer on Ubuntu 22.04, follow these detailed steps:


Step 1: Update Your System

Before installing Composer, update your package list to ensure that you have the latest versions of all your packages.

sudo apt update
sudo apt upgrade -y

Step 2: Install PHP and Dependencies

Composer requires PHP to run. Install PHP along with other necessary dependencies.

sudo apt install php-cli unzip curl -y

Step 3: Download Composer Installer

Use curl to download the Composer installer.

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

Step 4: Verify the Installer (Optional but Recommended)

Before running the installer, it’s recommended to verify the installer’s integrity to make sure it’s secure.

  1. Get the latest installer signature:

    HASH="$(curl -sS https://composer.github.io/installer.sig)"

  2. Verify the installer:

    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 the output says Installer verified, you can proceed to the next step. If it says Installer corrupt, don’t continue, and download the installer again.

Step 5: Install Composer

Run the installer to install Composer globally on your system:

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

This will move Composer to /usr/local/bin so that it can be accessed globally using the composer command.


Step 6: Clean Up

Remove the composer-setup.php file, as it’s no longer needed.

rm composer-setup.php

Step 7: Verify Composer Installation

Check if Composer has been installed successfully by running:

composer --version

This should output the version of Composer that you installed, such as:

Composer version 2.x.x 2024-xx-xx

Step 8: Using Composer

Now that Composer is installed, you can use it to manage PHP project dependencies. Here are some basic Composer commands:

  1. Install Dependencies (for an existing project)
    Run this in your project directory where the composer.json file is located:

    composer install
  2. Create a New Project
    You can create a new Laravel project, for example:

    composer create-project --prefer-dist laravel/laravel my-laravel-app

  3. Add a Package to Your Project
    To add a new package to your project, use:

    composer require vendor/package

  4. Update Dependencies
    To update all the dependencies listed in your composer.json file:

    composer update

  5. Check Installed Packages
    To list all installed packages:

    composer show


Composer is now installed and ready to use on your Ubuntu 22.04 system!

Let me know if you need help with anything else related to Composer.

Download Now No episodes found.Posted in UbuntuTagged , , , , , ,

Leave a Reply

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