How to Installing Composer on Ubuntu: A Comprehensive Guide
What is Composer?
Composer is a dependency manager for PHP. It allows developers to declare the libraries and packages their project depends on and automates the installation process, ensuring that all dependencies are correctly downloaded and configured. With Composer, you can easily add, update, and remove packages in your project, simplifying PHP development significantly.
Composer is a powerful dependency manager for PHP that simplifies package management and makes it easier to integrate external libraries into your PHP projects. Whether you’re developing web applications, web services, or any other PHP-based project, Composer is an essential tool in your toolkit. In this guide, we’ll walk you through the step-by-step process of installing Composer on an Ubuntu system.
In a previous post, we discussed how to install PHP on Ubuntu, a crucial step for developers working on diverse projects. Continuing in our quest to empower developers, we’ll now delve into another essential tool for PHP development: Composer. Composer is a dependency management tool that simplifies the process of managing and installing PHP packages and libraries. This guide will walk you through the step-by-step process of installing Composer on your Ubuntu system.
Before we proceed, make sure you have PHP installed on your Ubuntu system by following our previous guide here: [link to the previous post].
Prerequisites:
Before you start installing Composer on your Ubuntu system, you need to ensure you have the following prerequisites in place:
- A working Ubuntu system (version 18.04 or later).
- Terminal access with superuser (sudo) privileges.
- PHP installed on your system.
Installing Composer:
Here’s a step-by-step guide to installing Composer on your Ubuntu system:
Step 1: Update Package Repository
Before installing any software, it’s a good practice to ensure your package repository is up to date. Open your terminal and run the following command:
sudo apt update && sudo apt upgrade
Step 2: Install Composer Dependencies
Composer requires some packages to be installed on your system. Use the following command to install these prerequisites:
sudo apt install curl php-cli php-mbstring git unzip
Step 3: Install Composer
Make sure you’re in your home directory, then retrieve the installer using cURL:
cd ~
Now, you can download Composer and verify Hash Signature and Install composer globally by ruining one command:
For Curl User:
EXPECTED_SIGNATURE=`sudo curl -sS https://composer.github.io/installer.sig`
sudo php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_SIGNATURE="$(php -r 'echo hash_file("sha384", "composer-setup.php");')"
if [ "$EXPECTED_SIGNATURE" = "$ACTUAL_SIGNATURE" ]; then
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
else
>&2 echo 'ERROR: Invalid installer signature'
sudo rm composer-setup.php
exit 1
fi
N.B: If you use wget you can run blow command instend of use curl command:
For wget User:
EXPECTED_SIGNATURE="$(sudo wget -q -O - https://composer.github.io/installer.sig)"
sudo php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_SIGNATURE="$(sudo php -r 'echo hash_file("sha384", "composer-setup.php");')"
if [ "$EXPECTED_SIGNATURE" = "$ACTUAL_SIGNATURE" ]; then
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
else
>&2 echo 'ERROR: Invalid installer signature'
sudo rm composer-setup.php
exit 1
fi
If the command asks for your sudo password, enter your sudo password and press Enter. After entering the password, the command will execute and install Composer while configuring the necessary settings for you.
Bash Output:
All settings correct for using Composer
Downloading...
Composer (version 2.6.5) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer
Step 4: Adding Composer Environment Path
If your environment uses Bash, follow these steps:
Go to your home directory:
cd ~
Open the .bashrc
file for editing using a text editor (e.g., nano):
nano .bashrc
Add the following line to the bottom of your .bashrc
file:
export PATH="$PATH:$HOME/.composer/vendor/bin"
Save the changes in nano by pressing Ctrl+X
, then confirming with “Y” and pressing Enter.
If you want to automatically set the PATH variable in Bash, you can also add the line using the following command:
echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bashrc
To apply the changes immediately without logging out and logging in, run:
source ~/.bashrc
If your environment uses Zsh, follow these steps:
Go to your home directory:
cd ~
Open the .zshrc
file for editing using a text editor (e.g., nano):
nano .zshrc
Add the following line to the bottom of your .zshrc
file:
export PATH="$PATH:$HOME/.composer/vendor/bin"
Save the changes in nano by pressing Ctrl+X
, then confirming with “Y” and pressing Enter.
If you want to automatically set the PATH variable in Zsh, you can also add the line using the following command:
echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.zshrc
To apply the changes immediately without logging out and logging in, run:
source ~/.zshrc
After following these steps, Composer will be added to your PATH, making it globally accessible in your Bash or Zsh environment. These instructions are accurate and should work as intended.
Step 5: Verify Installation
To verify that Composer is correctly installed, run:
composer --version
You should see the version number displayed in the terminal, confirming that Composer is now installed on your Ubuntu system.
Bash Output:
Composer version 2.6.5 2023-10-06 10:11:52
Conclusion
Congratulations! You’ve successfully installed Composer on your Ubuntu system. With Composer in your toolkit, you’re well-equipped to manage dependencies in your PHP projects efficiently. Composer simplifies the installation and management of packages and libraries, making your PHP development workflow smoother and more productive.
Now that you have Composer and multiple PHP versions at your disposal, you’re ready to take on a wide range of PHP projects. Stay tuned for more guides and tutorials to enhance your development skills. Happy coding!
In our next post, we’ll explore how to create a basic PHP project and use Composer to manage its dependencies effectively. Stay tuned for more exciting insights into the world of web development!
If you wish to uninstall Composer, ensure you’ve read our newest guide on Uninstalling Composer in Ubuntu: A Step-by-Step Guide. You can find that guide Uninstalling Composer in Ubuntu: A Step-by-Step Guide.
Happy Coding 😉