
How To Install and Use Composer on Ubuntu 24.04 and Newer
Welcome to the Greenhost.cloud blog! Today, we’re diving into the world of PHP dependency management by exploring how to install and use Composer on Ubuntu 24.04 and newer versions. Composer is an essential tool for PHP developers, allowing you to manage libraries and project dependencies effortlessly. Whether you’re a seasoned developer or just getting started, this guide will walk you through the installation process and basic usage of Composer.
What is Composer?
Composer is a dependency manager for PHP, enabling you to declare the libraries your project depends on and managing the installation and updates of those libraries. With Composer, you can easily integrate third-party packages into your projects, ensuring that you have the right versions and reducing the hassle of manual installations.
Prerequisites
Before we begin, ensure you have the following:
- Ubuntu 24.04 or newer: Ensure your system is up-to-date by running:
sudo apt update && sudo apt upgrade
- PHP Installed: Composer requires PHP to function. You can check if PHP is installed by running:
php -v
If PHP is not installed, you can install it with:
sudo apt install php php-cli unzip
Step 1: Install Composer
Method 1: Installing Composer Globally
- Download Composer Installer: Open your terminal and download the Composer installer script using the following command:
curl -sS https://getcomposer.org/installer -o composer-setup.php
- Verify the Installer: It’s a good practice to verify the installer’s SHA-384. You can find the latest hash on the Composer Public Keys / Signatures page. Then run the following command to verify:
HASH="$(curl -sS https://composer.github.io/installer.sig)"
echo "$HASH composer-setup.php" | sha384sum --check
If the output reads composer-setup.php: OK
, you’re good to go!
- Install Composer: Run the following command to install Composer globally:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
- Remove the Installer: Clean up by removing the installer script:
rm composer-setup.php
- Verify the Installation: Check if Composer is installed correctly by running:
composer -V
Method 2: Installing Composer Locally
If you prefer to install Composer locally for a specific project, navigate to your project directory and run:
curl -sS https://getcomposer.org/installer | php
This will create a composer.phar
file in your project directory. You can run Composer commands by executing:
php composer.phar <command>
Step 2: Using Composer
Now that Composer is installed, let’s look at some basic commands and usage.
Creating a New Project
To create a new project with Composer, use the create-project
command. For instance, to create a new Laravel project, run:
composer create-project --prefer-dist laravel/laravel my-laravel-app
Replace my-laravel-app
with your desired project name.
Managing Dependencies
- Adding a Dependency: To add a package to your project, use:
composer require vendor/package-name
For example, to add Guzzle, run:
composer require guzzlehttp/guzzle
- Removing a Dependency: To remove a package, use:
composer remove vendor/package-name
- Updating Dependencies: To update all your project’s dependencies, simply run:
composer update
- Installing Dependencies: If you clone a project that uses Composer, navigate to the project directory and run:
composer install
This will install all the dependencies listed in the composer.json
file.
Autoloading Classes
Composer provides an autoloader that can be included in your PHP files. Just add this line to your PHP script:
require 'vendor/autoload.php';
This will automatically load the classes from your installed packages, making it easier to manage your code.
Conclusion
Congratulations! You’ve successfully installed and learned how to use Composer on Ubuntu 24.04 and newer. With Composer, managing your PHP project dependencies is now a breeze. As you continue your development journey, you’ll find Composer to be an invaluable tool in your toolkit.
Stay tuned for more posts on PHP tips, tools, and best practices here at Greenhost.cloud!