How to Install Composer PHP Packages Without Composer

How do I install Composer PHP packages without Composer?

The composer.json file lists the dependencies. In your example:

"require": {
"php": ">=5.5.0",
"guzzlehttp/guzzle": "^6.0",
"psr/http-message": "^1.0",
"psr/log": "^1.0"
},

You must then find the corresponding packages in the packagist site. Repeat the same process for each dependency: find additional dependencies in their corresponding composer.json files and search again.

When you finally have a complete list of the required packages, you only need to install them all one by one. For the most part, it's just a matter of dropping the files somewhere in your project directory. But you must also ensure that PHP can find the needed classes. Since you aren't using Composer's auto-loader, you need to add them to your own custom autoloader. You can figure out the information from the respective composer.json files, e.g.:

"autoload": {
"psr-4": { "Coinbase\\Wallet\\": "src/" }
},

If you don't use a class auto-loader you'll need to figure out the individual require_once statements. You'll probably need a lot of trial and error because most library authors won't care documenting that.

Also, and just in case there's confusion about this:

  • Composer has an official GUI installer for Windows and a copy and paste command-line installation procedure for all platforms.
  • Composer can be run locally and its output just uploaded elsewhere. You don't need SSH in your shared hosting.
  • The command needed to install a library can be copied and pasted from the package web site—even if the package maintainer didn't care to document it, packagist.org generates it by default.

Composer is not perfect and it doesn't suit all use cases but, when it comes to installing a library that relies on it, it's undoubtedly the best alternative and it's a fairly decent one.


I've checked other answers that came after mine. They mostly fall in two categories:

  1. Install a library and write a custom download script with it
  2. Use an online web based interface for Composer

Unless I'm missing something, none of them address the complaints expressed by the OP:

  • Learning curve
  • Use of third-party software
  • Possibility to develop right on the server (using SSH, I presume)
  • Potentially deep dependency tree

how to Avoid composer for php packages?


The composer dependency

Yes, I ran into this 'composer' problem as well. Lots of developers seem to like it, for various reasons, but, if you want to try out a small package, or you only have FTP access, it just introduces another dependency. For big frameworks I can understand this, but for a small piece of code, with few classes, it makes no sense.

Example: I wanted to output an Excel file in ODS format, and found a relatively simple piece of code to do that:

https://github.com/Lapinator/odsPhpGenerator

Unfortunately the latest version depends on composer. No other way to use the code is provided. The developer probably sees this as an advantage? Well, I don't. So what to do? Be forced down the composer path, or hack the code a bit?

The latter can be a lot simpler than you might think. All we need is the content of the autoload.php file. This file will probably tell PHP where to find the classes of the package using SPL functions. You can try to write your own autoloader, but my package needs all its php files, so I simply wrote this:

require_once('../src/ods.php');
require_once('../src/odsDraw.php');
require_once('../src/odsFontFace.php');
require_once('../src/odsStyle.php');
require_once('../src/odsTable.php');
require_once('../src/odsTableCell.php');
require_once('../src/odsTableColumn.php');
require_once('../src/odsTableRow.php');

After adding that as the autoload.php to the package it worked without using composer.

In my opinion a developer should provide a way to try such a small package without having to go through the composer process. They probably don't do this because they want to do things 'the right way'. And, of course, once you're used to composer it is no big deal.

How To Install PHRets 2.0 Without Composer?

If you're unable to use Composer on a server, you should still be able to install Composer locally and run it to populate your vendor/ directory. With that populated, you can simply copy/FTP that to the server and you should be fine.

Installing Composer packages from a machine other than the one that the code will run on has a few risks but is usually OK. For example, if a certain package requires PHP 7+ which you have installed locally but your server only runs PHP 5.6, Composer may gather the PHP 7+ dependencies just fine, but you'd run into problems loading those on your server (whereas if you ran Composer on that server, you'd get errors or earlier versions that didn't require PHP 7+ that still satisfied your defined requirements).

There are no plans for PHRETS to include it's own autoload option simply because of the other PHP libraries it depends on. Having to gather PHRETS, Guzzle, some Illuminate packages, etc. and put them all together in a way that doesn't involve Composer and still works is an incredible amount of work.



Related Topics



Leave a reply



Submit