Guide on how to install Composer
11:07, 13.09.2021
If your project is based on PHP, then with all its advantages, it has at least one disadvantage: when installing new libraries you need to manually prescribe literally every dependency on other packages. But it won't be hard to avoid such painstaking work - the installation of Composer will help you with that. And we'll tell you how to do it quickly and smoothly!
What is Composer?
Composer is a special utility designed to automate work with dependencies in PHP packages. It can find libraries by name, download and install them, and later still update them. It was introduced in 2012 and has been improved many times: at the time of writing, for example, the current version is 2.1.6. This package manager is free and works with Laravel, Symfony, and Yii2. You can install Composer on hosting (regular VPS and a dedicated server), as well as on Linux, Windows, MacOS.
To manage Composer uses a command-line interface. The utility is very smart. For example, it creates files that contain third-party packages, dependencies, requirements for them, and installed versions - you can simply copy that file (composer.json and composer.lock) when you migrate your project. And no matter whether you're installing Composer on Ubuntu or another operating system, you can put packages in different folders and have your own versions of libraries for different projects at the same time. This is just a small part of the features of this application!
How do I connect Composer?
The installation of this package manager is quite simple, and a person with minimal skills will be able to cope with it. But it is important to understand that if you need Composer on other OS, installation in Windows and other environments will be slightly different. Therefore, each of these options is worth talking about separately.
Virtual hosting and Linux
To install the utility on servers and PCs on Linux, refer to the following step-by-step instruction:
- Connect to the server via SSH. Only one command is needed to establish a secure communication channel through such a protocol on Unix systems:
ssh root@nnn
Instead of nnnn you get the IP address of your server. In Windows, you need a program like PuTTY (we wrote about it in the article about setting up a VPS on Linux).
- 2. Download the installer. To download the Composer installer file, type at the command line:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
Here is the domain of the official website of the utility. And you need to check the SHA-384 signature of the installer with the command:
php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupted'; unlink('composer-setup.php'); } echo PHP_EOL;"
The long set of numbers in the command is the installer signature, which is changed in the new version of the manager. Look for it on the service website or GitHub.
- 3. Run the installer. You have two options for installing Composer. The first: when such a utility is installed in the current directory, and to access it you will first need to specify the path to it. If this suits you, enter:
php composer-setup.php
Alternatively, the dependency manager is placed in the usr/local/bin directory and can be enabled from anywhere in the system. This script is run with the command:
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
- 4. Uninstall the installer. The final step is to get rid of the downloaded and already unnecessary file. To solve such a problem, specify:
php -r "unlink('composer-setup.php');"
At this point, you're done with installing Composer on Ubuntu or another Unix OS, and you can turn on the utility itself. To do this, enter:
composer
If everything is done correctly, the command line will show the symbolic name of the manager!
Windows
The installation of the described utility on Microsoft operating systems is somewhat easier, although there are a few steps as well:
- Download the installer. To work with the manager designed for PHP, you must first put the language itself on your computer (select the assembly for your online project). When this is done, download the utility installer from the Composer website to install on Windows - and run it like any other program.
- Set the basic settings. In the appearing windows of the application installation wizard, pay attention to a number of nuances. First: you do not need to activate developer mode. Second: you must correctly specify the path to the command line of the previously installed PHP. Third: in the proxy settings, leave the checkbox unchecked.
- Turn on Composer. After all the windows is the Install button and installation itself. After that, open Run (Windows icon + the R key), in it enter cmd. This will start the standard command line of the operating system, where you have to specify the familiar command like composer. The program will start - and the installation of the Composer manager on your computer is complete!
However, you should understand: all this is only half of the battle. It is not only about working with the utility and the correct application of commands. It is important: no matter what means of automation in the handling of an online project you use, the basis of stability - it is a competent choice of VPS or dedicated server: stable and reliable. Therefore it is necessary to choose the hosting thoughtfully! We offer to pay attention to the services of HostZealot. With us you get a guaranteed quality rental VPS and "dedicated", a wide range of rates for different tasks and service, not afraid of such words, the highest quality - contact us!