Composer is a dependency management tool for PHP. We at vpsfix.com often come across it when building servers for our customers. So, we’ve decided to document the process for our readers. This tutorial is an extension to our Virtualmin – Nginx server setup guide. Let’s install Composer on Ubuntu 20.04 VPS.
Although we’re posting this tutorial as an extension to our Virtualmin server setup, Virtualmin is not a requirement for Composer. This tutorial will work with or without Virtualmin on your server. And no special configuration is required to make Composer work with Virtualmin. But you need to have PHP installed.
What is Composer and why we need it?
Composer is a tool to manage dependencies for PHP projects. Developers can declare the libraries their project depends on and Composer will install them per project basis. Meaning that these libraries or ‘packages’ are not installed on the server globally. Instead, there are installed in vender directory inside the project.
Composer is an open-source software developed by Nils Adermann and Jordi Boggiano. It is an essential software you have when you want to use hundreds of PHP utilities out there. And if you’re running a hosting business, Composer is a nice feature to offer as most hosting giants are yet to support it on their platforms.
Install Composer on Ubuntu 20.04
As I mentioned earlier, PHP is an important requirement for Composer. Minimum system requirements for Composer show PHP v5.3.2 as the minimum version of PHP supported. But if you’ve followed our Virtualmin Nginx server setup guide, you should already have PHP v8.0 installed on your server. So, you can go ahead and install Composer with following commands.
curl -sS https://getcomposer.org/installer -o composer-setup.php php composer-setup.php --install-dir=/usr/local/bin --filename=composer
That should do it. Composer should be installed and ready to use on your Ubuntu 20.04 VPS now. Your terminal console will display following message. Note the Composer path as you might need it later on the special scenario, we talk about on next section.
If all goes well, you can go ahead and install Composer managed PHP application on your domain.
Use Composer with Different PHP Versions
Although we have PHP v8.0 as our default PHP version, there may be times when you need to run Composer with different PHP versions. These types of complications are common in server management. Some applications just can’t keep up with PHP development. This is not a problem for us since we can run multiple php version on our Ubuntu 20.04 VPS. The scenario we’re going to discuss here is when the composer managed application you’re trying to install doesn’t support PHP v8.0.
Downgrading the default PHP version to an older version isn’t ideal when you have multiple websites already online with PHP v8.0. We can get around this by enforcing Composer to use a different PHP version.
Let’s look at an example where you need to leave the default PHP version of the server to PHP v8.0 and run Composer with PHP 7.4 on a single domain. You would first install PHP v7.4 on the server following our instructions on using multiple php versions. And then when you want to run Composer commands, you would manipulate the command to enforce PHP 7.4 like below.
So, the command composer becomes php7.4 /usr/local/bin/composer. You need to use the direct path to Composer which I ask you to make note of earlier. Let’s say our tutorial wants you to run following Composer command,
You would change it like below to enforce PHP v7.4,
php7.4 /usr/local/bin/composer install
That’s it, you’re using PHP v7.4 with Composer now while your global PHP version is still PHP v8.0. But remember to manipulate the command every time you run composer on this domain.