How to configure a composer package to be globally installed?
I’m trying to make a little CLI tool and package it up with composer. Below is an extremely simplified version of the program, but it’s enough to demonstrate the problem I’m encountering. The project has one dependency, and one «binary» file composer.json
$ composer global require alice/yamldump=dev-master
- ~/.composer/vendor/bin/yamldump -> ../alice/yamldump/bin/yamldump
- ~/.composer/vendor/alice/yamldump/
- ~/.composer/vendor/symfony/yaml/
This is a problem, because I did not intend to globally install symfony/yaml and my package’s vendor/autoload.php can no longer find the Yaml package in the proper location.
I don’t mind that symfony/yaml was installed globally, but it would make sense to me that composer global require would install the package like this:
- ~/.composer/vendor/bin/yamldump -> ../alice/yamldump/bin/yamldump
- ~/.composer/vendor/alice/yamldump/
- ~/.composer/vendor/alice/yamldump/vendor/symfony/yaml/
After all, what if I have Package A that depends on symfony/yaml=2.5.3 and Package B that requires symfony/yaml=2.6.x ?
If the composer global require installs dependencies to ~/.composer/vendor/* , each globally required package can’t maintain it’s own version requirement of its dependency.
I know this is sort of a convoluted problem, but I really don’t know how to begin fixing it.
$ composer global require alice/yamldump=dev-master $ yamldump sample.yml
$ yamldump sample.yml Warning: require_once(vendor/autoload.php): failed to open stream: No such file or directory in /Users/alice/.composer/vendor/alice/yamldump/bin/yamldump on line 8 Fatal error: require_once(): Failed opening required 'vendor/autoload.php' (include_path='.:') in /Users/alice/.composer/vendor/alice/yamldump/bin/yamldump on line 8
The question
Here it is in black & white:
How am I intended to write the require «vendor/autoload.php» line and have it work for both locally installed packages and globally installed packages?
Global installation of PHP tools with Composer
The Composer package manager along with the Packagist repository site is quickly becoming the defacto PHP package management system.
One feature I found out about recently is that you can install packages globally rather than locally into your project. I think that this is most useful for development tools, such as PHPUnit which are then available everywhere.
To install a composer package globally, you run the usual require command, but with the addition of the global modifier. So to install PHPUnit, you would run:
$ composer global require phpunit/phpunit $ composer global require phpunit/dbunit $ composer global require phing/phing $ composer global require phpdocumentor/phpdocumentor $ composer global require sebastian/phpcpd $ composer global require phploc/phploc $ composer global require phpmd/phpmd $ composer global require squizlabs/php_codesniffer
This will install PHPUnit and all its dependencies into the ~/.composer/vendor/ directory and, most importantly, the phpunit CLI tools are installed into ~/.composer/vendor/bin/.
Simply add this directory to your PATH in your ~/.bash_profile (or ~/.bashrc) like this:
export PATH=~/.composer/vendor/bin:$PATH
and phpunit is now available on your command line.
To keep your tools up to date, you simply do this:
To remove a package, you edit ~/.composer/composer.json and then run composer global update.
Global Installation of Composer (manual)
Other scripts from /usr/local/bin/ works, but composer gives: $php composer.phar update Could not open input file: composer.phar It works only if I enter absolute path to composer.phar . How to fix this?
8 Answers 8
As described on the Composer website:
curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer
Manual Method
I found an easier way to globally install composer than the manual proscribed in the github readme.md . It’s actually on the getcomposer.org website:
curl -s http://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/
And if you’re even more lazy, like me, you can create an alias:
alias composer='/usr/local/bin/composer.phar'
This way you can invoke composer with just composer
Nice. The alias is a sweet touch for obsessive compulsives too. Typing in file extensions? Pfeh! That’s for chumps
when I run $ sudo mv composer.phar /usr/local/bin/composer and then try to type $ composer I get the error: bash: /usr/local/bin/composer: Permission denied How can I change these permissions?
Don’t add php in the beginning. Just call composer.phar .
It works because composer is already an executable (it has u+x rights by default). So, no need to execute php, because shell will do it anyway.
And there is a special code on the first line of composer.phar, #!/usr/bin/env php which tells ubuntu that the file is a php file and should be run with the php program.
Actually, getcomposer.org now recommends a simpler method:
$ curl -sS https://getcomposer.org/installer | php $ sudo mv composer.phar /usr/local/bin/composer
Now you can just use composer without bothering with an alias or a separate sh script.
Another alternative to get a nice composer command instead of composer.phar :
$ curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin $ ln -s /usr/local/bin/composer.phar /usr/local/bin/composer
for easier execution I created /usr/local/bin/composer with content:
#!/bin/sh exec /usr/local/bin/composer.phar "$@"
dont forget about sudo chmod +x /usr/local/bin/composer.phar
Below are the steps to be followed to install composer globally: 1. Before installing Composer, make sure our server has all dependencies installed.
1st, update the package manager cache by running:
2. Now, let’s install the dependencies. We’ll need
- curl => to download Composer
- php5-cli => installing and running it
- git => used by Composer for downloading project dependencies
Everything can be installed with the following command:
$ sudo apt-get install curl php5-cli git
3. Composer installation with a single command:
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
4. This will download and install Composer as a system-wide command named composer, under /usr/local/bin. The output should look like this:
\#!/usr/bin/env php All settings correct for using Composer Downloading. Composer successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer
5. To test your installation, run:
Composer version 1.1.3 2016-06-26 15:42:08