How to install Composer

How to install the Composer dependency manager on your operating system (Windows, Linux or MacOS), so that you can start using Composer in your PHP projects.

In the previous article we were able to , the PHP dependency manager that allows us to improve the workflow of PHP development. Composer is one of those tools that you need to have no matter what, if you develop with PHP and it covers more development areas every day: when we are using third-party libraries, when we create projects based on a framework and, of course, when we are doing maintenance. throughout the entire life of a PHP project.

In this article we are going to dedicate ourselves to explaining the installation process. Really, as you will see, it is such a simple process that it can be summarized in a command in the terminal, but we want to comment on some installation variants and problems that we may encounter, with their solutions.

Install Composer on any operating system

To have this wonder in our system, in order to manage our dependencies in PHP, you must install it first. The process is quite simple. If all goes well it boils down to these actions, which depend on your operating system.

Now we are going to indicate all the alternatives for installing Composer in each operating system that you might have

Install Composer on Windows:

If you are in Windows you will use a traditional installer. No secret. It’s a wizard and you go through several windows, next, next.

You will find the wizard to install in Windows as the first option in the . Only one detail, quite important, is that Composer requires you to have PHP installed on your computer! If not, you would have to install PHP first with one of the .

Install Composer on Windows with Chocolatey

An also very comfortable option to install Composer (and incidentally PHP if you don’t have it) is to use Chocolatey, which is a dependency manager for Windows. Chocolatey is a software that will make it very easy for you to install any program or language on your Windows system, so taking the trouble to install it will save you time in the future when it comes to managing the software on your computer. On this page you can find more information about .

See also  https://desarlowoweb.com/faq/how-to-restart-nginx

Once we have Chocolatey installed, to install Composer it is recommended to open a terminal in administrator mode (for this we right-click on the terminal icon and select “Run as administrator”. In the terminal we launch the command:

choco install composer

The good thing about Chocolatey is that it does all the work for you, to install the packages you ask for and all the necessary dependencies. In the case of installing Composer, as PHP is another required software, if you don’t already have it installed, it will install it on your computer at once.

Once the installation process is finished, simply restart your terminal window to start using it.

Install composer on Linux

If you’re on Linux or MacOS, you’ll use the command line to install Composer. On the download page are the commands you need to run to install Composer properly. We recommend that you access the and copy and paste the commands that you will find in a textarea yourself.

Another easy alternative to getting Composer is something as simple as running this statement:

curl -sS https://getcomposer.org/installer | php Note: This command requires Curl, if it fails you can try to do the same but with the PHP command. php -r “readfile(‘https://getcomposer.org/installer’);” | php

Ultimately you can also download composer.phar by hand and place it in your project folder where you want it to be available. http://getcomposer.org/composer.phar

This will download to your folder the file composer.phar which is a PHP executable. The .phar can be run on the command line like other terminal commands. That file is exactly the one that you invoke to load the dependencies or update them, we will see it soon.

Global installation or particular to a project

Here we must make an important clarification, which may interest you because depending on it there are slight differences in the use of Composer. It is about having a global installation of Composer or simply having it available in a specific project.

When you install it on Windows, Composer will be available anywhere on your system. It is a global installation, which means that you can invoke the Composer command from whatever directory you are in. I think it is the most comfortable, so if you are a Windows user you do not need to read more about this point.

See also  PHP authentication for multiple users using MySQL

However, with the instruction that we have indicated to install it on Linux/Mac, you will only have it available in the folder where you were when you launched the curl command. You would have to install it once for each project, which is not unreasonable either, but it can be improved.

In the case of having made a particular installation for a project, you will place the composer.phar in the root directory of your project, so that you can later invoke it to install the dependencies. However, if you want, you can also install it globally on Linux if you put composer.phar in another path.

mv composer.phar /usr/local/bin/composer

Note: If that command doesn’t work for you, just try doing it as root.

sudo mv composer.phar /usr/local/bin/composer

Install composer on Mac

The process that we have described to install Composer on Linux is the same that you can use on Mac. However, you could also use another, even simpler mechanism.

And if you’re on MacOS, the easiest way to have Composer in global mode is to install it via “homebrew”, the Mac systems package manager. It’s a somewhat longer process, but you’ll also benefit from having homebrew on your machine to install other software you may need. In the Composer documentation itself, they give us the commands that we should execute.

Note: You will need to install homebrew itself first, as explained on the brew update brew install composer page.

What to do if you can’t find PHP on your Mac

Composer requires PHP. You may have it already installed on your Mac, such as if you have Mamp installed on your computer. However, Homebrew may not find that PHP installation. In that specific case, the previous series of commands could report a problem saying that the Composer dependency with php53, php54, php55 or php56 was not there. In that case you can solve it by installing PHP also with Homebrew:

See also  How to refresh web pages

brew install php56

Of course, you will put the current PHP version, since we are on PHP 8 at the moment…

That installed php56 for me with brew and then everything worked fine when I did the command.

brew install composer

Note: I have to admit that I don’t know if simply in the “brew tap homebrew/php”, indicating the version of PHP that we want, it can be solved.

Possible problems in Windows and their solutions

The process of installing Composer on Windows is very simple. However, during the installation it can give you a common problem that is actually a bit scary when you see it, but it is very easy to solve. At least that problem has arisen for me in Windows in my PHP installation under Xamp.

The openssl extension is missing, which means that secure HTTPS transfers are impossible.

If possible you should enable it or recompile php with –with-openssl

Don’t worry because you don’t have to recompile anything! Just open the php.ini file and find the line:

;extension=php_openssl.dll

Remove the semicolon “;” that comes before it, because it causes that line to be considered as a comment. Then the openssl extension will be active and you can install composer normally.

Another problem that you can find in Windows, although not in the installation but in the use of Composer, is that the JSON file is malformed. If you check the syntax of your JSON and find that everything is fine, perhaps your problem is the encoding or character set. JSON files must be written in “UTF-8” encoding, also you must not use BOM. You can change the encoding with your preferred editor, each editor has the option of saving with a certain encoding in a different part.

conclusion

At the moment I think that it is all you need to know so that installing Composer is not a problem for you, whether you use Windows, Linux or Mac. In the following article we will focus on .

Loading Facebook Comments ...
Loading Disqus Comments ...