To install Composer in my shared host, I ran from my home directory:
$ curl -sS https://getcomposer.org/installer | php -- --install-dir=bin
Then I was told to run: $ php bin/composer.phar
And following that I was able to use Composer, confirmed by running:
$ composer.phar -V
Composer version 1.0-dev
Can someone please explain the command php bin/composer.phar?
Is it simply telling php to unpack the .phar file? If so, where does it unpack it to? I see bin/bin/composer.phar* after having run it. Then I have to run composer using the entire path to the phar file, unless I'm in the home directory.
A .phar file is a ZIP-like file that contains all the files that make up a PHP application, and some initial code in the file header that sets up some stuff and then transfers execution to the first zipped file.
The command php bin/composer.phar is calling PHP and gives the phar file as a parameter to make PHP execute that file. This will make PHP read that file, find the header with the bootstrapping code and execute that. PHP will initialize PHAR execution and go further into that compressed file.
The command composer.phar will only work if that file has been marked with the x file flag to make it executable, and it is placed in a directory that is mentioned in the $PATH environment variable. Part of the PHAR file is the first line of the file header that is called "shebang". This line is used in all script files for the command line to specify the interpreter that should be used to execute the file. In case of composer.phar, this interpreter is PHP. So in essence, the same thing happens as with the first call: PHP is "running" the PHAR file, but with less typing on the command line.
Related
I'm using a PHP library that has a cli script file with shebang on top:
#!/usr/bin/env php
<?php
This file is meant to be used by cli so it's defined as a bin on composer config as expected.
When I do a composer install, the composer generates a link to this file on vendor/bin folder like this:
#!/usr/bin/env sh
dir=$(d=${0%[/\\]*}; cd "$d" > /dev/null; cd "../lib-owner/lib" && pwd)
dir=$(echo $dir | sed 's/ /\ /g')
"${dir}/lib" "$#"
The problem is that with this setup, every call to vendor/bin/the-library is just forwarded to the php script file, and it lets the shebang resolve the PHP version, but I need this script to be called with a specific php binary.
I can resolve it by calling the script file directly: php73 /vendor/lib-owner/lib/lib/the-script.
Or by cloning the script into my root folder and calling it there in the place of the vendor: php73 the-script.
The problem is that both ways has some drawbacks and I would prefer something more stable and less hacky.
Is there anyway?
You could do this:
env PATH="/path/to/folder/where/desired/php/resides:$PATH" vendor/bin/the-library
What it does is alter the PATH environment variable just for the duration of that command, by prepending the path to the desired PHP interpreter.
This way, /usr/bin/env will use that PHP version instead of the default one (and the PATH environment variable will revert back to its previous value as soon as the script is finished).
Successfully install composer with their instruction but I can't check composer version. I also add path in environment. Which things left that I need to do?
User Variables
System Variables
Command line
Check if using this command works for you:
php C:\ProgramData\ComposerSetup\bin\composer.phar -v
If this is working and there is a missing composer.bat file in C:\ProgramData\ComposerSetup\bin you'll have to create it manually with this content:
#echo off
php "%~dp0composer.phar" %*
Then you'll be able to use composer -v instead of php C:\ProgramData\ComposerSetup\bin\composer.phar -v in your Command Prompt.
I'm assuming that you could execute php scripts and you already added the correct path to your system, because if composer.bat is not missing and you didn't add the path to your php executables, then this is the reason for this composer issue in the first place.
I m new to php and rabbitmq in debian(Linux). I have installed xampp, rabbitmq and also installed composer.phar in project directory using below command
/opt/lampp/htdocs/rabbitmq_demo# curl -s https://getcomposer.org/installer | /opt/lampp/bin/php
Now I use Composer to install the dependencies of the project using below command
composer.phar install
but it thrown an error as below
bash: php: command not found
I have preferred the link https://getcomposer.org/doc/00-intro.md
I want to prepare autoload.php
Please help me to create autoload under vendor directory.
You do not have a php cli program installed on your computer or it is not in your current $PATH variable. Please install PHP first or correct your $PATH environment variable.
Once you have this, run the composer.phar install again. This will download all dependencies listed in your composer.json file. Once the program completes, you will have a file ``vendor/autoload.php`. You can just require this file at the beginning of your own script and everything will be taken care of.
You need to add the path to the PHP command line (CLI) in the XAMPP install, to your bash environment. (You'd think the installer would do this!)
The XAMPP PHP CLI on Debian is at /opt/lampp/bin/php
So you need to add /opt/lampp/bin to your $PATH environment variable.
See this answer for the various options in modifying your path depending on who you want to be able to run PHP.
/etc/login.defs
/etc/environment
/etc/profile
~/.bashrc
In one of those files, you append to the path thus:
PATH=$PATH:/opt/lampp/bin
and re-login.
I'm currently experimenting with WP-CLI (http://wp-cli.org) - I have finally managed to get it to run on my basic command line by downloading/moving it to a PATH variable directory (i.e C:\xampp\php) and then updating my PATHSPEC to include .PHAR.
I have renamed the original file from wp-cli.phar to wp.phar in order to be able to reference it as wp in my cmd.exe.
Prior to this method I installed wp-cli using CURL and chmod in my Git Bash installation then renaming the file to wp (without an extension) and adding the path containing file to the PATH variable. This caused the .PHAR file to work in Git Bash but not to work on the command line.
MY ISSUE:
Whenever I try to use my wp.phar in the native CLI I get a php error report - it does recognise the command and does show a list of suggestions (which is usually given when syntax is incomplete or incorrect).
How do I even start to figure this out?
1 - I've attempted to look for a git batch file in my Git Bash directory to see if I can find a dependency I'm missing but no dice.
2 - My Git Bash is now not recognizing the wp command and I now need to refer to wp.pharand then add any sub-commands/arguments after. However using the Git Bash CLI wp.phar doesn't cause errors
After a few months I found a solution.
Make sure that your PHP root folder path and the folder path containing your phar files are added to the Windows PATH Environment Variable (to allow us to just use the filename to refer to them).
CMD Line Options (Run as Administrator):
assoc .php=phpfile and assoc .phar=pharfile
Next is adding the program that opens this file. Adding an ftype command allows us to open .php/.phar files without using the php.
ftype pharfile=php.exe %1 %* (The %1 is a placeholder for the file called and %* refers to any other arguments that many be input).
FINALLY
The key issue was getting the .phar scripts to execute in the command line as windows ends up trying to open these files in it's Open With Dialog
So what I did was wrap my .phar in a batch file with this command (in the same folder as the phar
echo php "%~dp0wp-cli.phar" %* > [name of file].cmd
I then run this file as [name of file] with any arguments and it works as usual.
is there a way to change the default version of PHP that is called on the command line?
I am trying to install Laravel using composer the problem is composer is calling php (which is version 4.4) when it needs to call php5.4, i have changed the binary in the header from #!/usr/bin/env php to #!/usr/bin/env php5.4 but this throws an error!
here is my php set-up
php: /usr/bin/php /usr/bin/php5.4 /usr/bin/php5.5 /usr/lib/php /usr/lib/php.ini-nourl /usr/lib/php.ini /usr/lib/php5.4 /usr/lib/php5.5 /usr/local/bin/php /usr/local/bin/php5.4 /usr/local/bin/php5.5 /usr/local/lib/php.ini-nourl /usr/include/php /usr/include/php5.4 /usr/include/php5.5 /usr/local/php /usr/share/php
There are a couple options here, but really this is not php specific, this is about basic execution path rules.
The #!/usr/bin/env php header means that when you run composer alone it runs it with php, so if you manually run it like php5.4 /path/to/composer you can bypass that header.
The other option is to define a symlink in /usr/local/bin/php or something that:
points to /usr/bin/php5.4
is in a directory that has precedence over /usr/bin inside your PATH environment variable.