'How do I install Composer PHP packages without Composer?

I'm trying to install the Coinbase PHP API but it requires Composer:

https://github.com/coinbase/coinbase-php

I'm looking for a universal PHP solution (perhaps a function) to let me install composer packages directly onto my server, without having to use Composer.

I think the developers of Composer believe they are helping people, but actually there are thousands of beginner developers that are being locked out of learning web development by the 'Composer barrier'.

It would really help if there was a flexible solution or some approach where we could install without Composer? How can I do this?

Please don't respond with some sarcastic comment. There are people that don't want to use Composer and I don't see why we should be herded into a specific third-party software in order to do web development.



Solution 1:[1]

You can try https://php-download.com/ which can help you download all dependency most of the time along with vendor folder. It promises composer not required. Tried it myself. It finds and creates all required folders and zips it for download. Works perfectly !!

Solution 2:[2]

I'm using shared hosting for a website and can't execute commands there. Aside from running composer via php script request that I request via browser, I usually use this workflow:

  • Make sure you have php installed locally.
  • Make directory on desktop.
  • download composer.phar from https://getcomposer.org/download/ (under header *Manual Download) and place it in the directory.
  • make a file composer.json paste in it the following contents

     {
         "require": {
             "coinbase/coinbase": "~2.0"
         }
     }
    
  • Browse to the directory with the shell of your choice(bash, git-bash, cmd, windows bash)

  • type php composer.phar update screenshot of bash output
  • Upload the vendor directory to your webserver via ftp or whatever mechanic you use.
  • include in your php project where you load your libraries(modify path to where you uploaded the vendor dir so it will include that autoload file)

    require_once('vendor/autoload.php');
    

This way you get the benefit of dependency management and you don't have to include manually all the gazillion of files and download all the dependencies manually, and updating them is just as easy as typing php composer.phar update and then replacing the vendor dir on your server with the new one.

Solution 3:[3]

An alternative solution that worked for me (since php-download was down) can be done by making your own little local composer downloader.

  1. Download and install XAMPP locally: https://www.apachefriends.org/index.html
  2. Download and install composer locally: https://getcomposer.org/download/
  3. Open commandprompt, navigate to say c:\temp and and simply type the composer dependancy, for example: composer require league/oauth2-client
  4. Copy the files from your c:\temp folder to your web host using an FTP program
  5. Add this to the top of your php: require("vendor/autoload.php");

Solution 4:[4]

I had to do this for an FTP server I didn't have SSH access to. The site listed in here worked, then I realized you can just do a composer install on your own server (using your target's PHP version), then copy all the files over.

Solution 5:[5]

This is not the ultimate solution but for me it was a big help for most of the cases: https://github.com/Wilkins/composer-file-loader

Allow you to load composer.json file just as composer would do it. This allow you to load composer.json file without composer (so theorically PHP 5.2 is enough)

I know the question is old but I hope it will help someone.

Solution 6:[6]

Analizing the problem

The problem in installing the dependencies without Composer is the autoloading system.

Composer use a homemade autoloader based on an array map, this is a de-facto standard. But this autoloading system, "fortunally" in this case, is not PSR-4 compliant.

PSR-4 is the de-iure standard for autoload a class in PHP, so you can't escape from autoloading. You must use one of them.

Solution Proposal

In this case, this brilliant PSR-4 autoloader is capable to be manually configured to autoload a VendorClass in a VendorNamespace anywhere in your code, as long as you require the custom autoload.php file early in your source code.

Real life example

Let's look at this example: I have a legacy project who can't and won't use Composer never and never even if God allow this with a miracle. This project can be speed up in development with this fantastic package for command line scripts. This is my project directory structure:

 - src
 - tests
 - vendor (not the Composer's one)

This package has this directory structure:

 - examples
 - src
   - Commando
 - tests

The only thing I need is the src folder. Placing this folder in my vendor folder would be fine. So my custom autoloader would be like this:

// Constants
$base_path = "path\to\my\project";
$autoloader_class = '\vendor\MarcoConsiglio-Wichee\PSR-4-Autoloading\Psr4AutoloaderClass.php';
define("BASE_PATH", str_replace("\\", DIRECTORY_SEPARATOR, $base_path));

// Autoloader
require_once BASE_PATH.'\vendor\MarcoConsiglio-Wichee\PSR-4-Autoloading\Psr4AutoloaderClass.php';

// Init the autoloader.
$package = [
    "nategood\commando" => [
        "namespace" => "Commando",
        "path" => str_replace("\\", DIRECTORY_SEPARATOR, '\vendor\nategood\commando\src\Commando')
    ],
    "kevinlebrun\colors.php" => [
        "namespace" => "Colors",
        "path" => str_replace("\\", DIRECTORY_SEPARATOR, '\vendor\kevinlebrun\colors.php\src\Colors')
    ]
];

// Register namespaces.
$loader = new \PSR4\Psr4AutoloaderClass;
$loader->register();
                      // Namespace                                      // Path to source
$loader->addNamespace($package["nategood\commando"]["namespace"],       BASE_PATH.$package["nategood\commando"]["path"]);
$loader->addNamespace($package["nategood\commando"]["namespace"],       BASE_PATH.$package["nategood\commando"]["path"]."\Util");
$loader->addNamespace($package["kevinlebrun\colors.php"]["namespace"],  BASE_PATH.$package["kevinlebrun\colors.php"]["path"]);

Now I can use the command package anywhere in my project!

Pros & Cons

This solution allow you to:

  • Easely and manually build your own custom autoloader (you only need to specify the VendorNamespace and the folder(s) where search for VendorClasses in the VendorNamespace.
  • Freely organize your composer dependency anywhere in your project folder (and why not, outside it)
  • Import a composer package as is in your project (either downloading locally with Composer or cloning the package repository) or a relevant part of it (i.e removing composer.json file or files that require the composer autoloader).

Cons:

  • Manually build your custom autoloader means to work on every required dependency of your project (i hope not a lot).
  • Mistakes in package source paths can be tedious and frustrating.
  • Works only with PSR-4 compliant file names (i.e. can't use a A.class.php file name)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Tschallacka
Solution 3
Solution 4
Solution 5 helderk
Solution 6 Marco Consiglio