How to Install Composer on Ubuntu, Package Manager for PHP Projects
Composer is a package manager tools standard used for the PHP programming language. By using Composer, you can easily install ormanage dependencies or library what you need on project what you’re working on.
Although a package manager, Composer is different from common package managers like Apt and Yum. Apt by default does not install dependencies globally on the system, but will only install them individually local on the directory of project what you’re working on, just like the concept package manager NPM on Nodes.
You can use Composer to help you install new dependencies or update dependencies or library existing ones that you will use in your project, such as installing framework Laravel, Symfony and so on.
How to Install Composer on Ubuntu
To install Composer on Ubuntu, make sure you have PHP (or PHP CLI) installed on the Ubuntu you are using. If you haven’t already installed it, you can install it using a command like the following:
sudo apt-get update sudo apt-get install php
Then the system will do updates and install PHP on your Ubuntu. by default The installed PHP is the latest version of PHP that is available on repositories, this will probably depend on the version of Ubuntu you are using. To check the PHP version installed on your device, run the command as shown in the following image:
Make sure the PHP installed on your device is PHP version 3.5.2 and above because Composer requires PHP version 5.3.2+ to work.
After successfully installing PHP on your device, now you can run PHP as a command built-in on command line interface on your Ubuntu. That way you can already use it to install Composer.
To install Composer, you can use and run the following commands one by one:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
Wait for the process of the commands one by one and make sure there are no notifications error. Then Composer will be installed on your Ubuntu and you can use it to install and update the dependencies you need on your Ubuntu. project what you do.
However, default the method used above will only install Composer automatically locally and to run it you have to do it using a command like the following:
php composer.phar
Doing the installation locally means Composer can only be accessed by the user you are currently using. So that Composer can be accessed by all users – the term globally – You can move the “composer.phar” file to the “/usr/local/bin/” directory by using the following command:
sudo mv composer.phar /usr/local/bin/composer
Then the “composer.phar” file will be moved to the “/usr/local/bin/” directory so you can run Composer just by using the command composer, no longer an order composer.phar as previously.
If at the time of executing the command as above, you receive a message error like “usr/local/bin/composer: No such file or directory”. That means on Ubuntu you don’t have a directory “usr/local/bin/”. For that you have to create the directory first by using the following command:
sudo mkdir -p /usr/local/bin
Then the directory “/usr/local/bin” will be created on your Ubuntu. Then repeat the file move command as before and make sure it doesn’t happen anymore error as previously. If not available error means the file “composer.phar” has been successfully moved to that directory.