ChatGPT解决这个技术问题 Extra ChatGPT

How to run composer from anywhere?

I have just installed composer in my /usr/bin folder, so when from that folder I run php composer.phar I get the help info about composer. But, when I try to run the same from other folder I get Could not open input file: composer.phar.

How to call php composer.phar from every where without problems?


q
quantme

You can do a global installation (archived guide):

Since Composer works with the current working directory it is possible to install it in a system-wide way. Change into a directory in your path like cd /usr/local/bin Get Composer curl -sS https://getcomposer.org/installer | php Make the phar executable chmod a+x composer.phar Change into a project directory cd /path/to/my/project Use Composer as you normally would composer.phar install Optionally you can rename the composer.phar to composer to make it easier

Update: Sometimes you can't or don't want to download at /usr/local/bin (some have experienced user permissions issues or restricted access), in this case, you can try this

Open terminal curl -sS http://getcomposer.org/installer | php -- --filename=composer chmod a+x composer sudo mv composer /usr/local/bin/composer

Update 2: For Windows 10 and PHP 7 I recommend this tutorial (archived). Personally, I installed Visual C++ Redistributable for Visual Studio 2017 x64 before PHP 7.3 VC15 x64 Non Thread Safe version (check which versions of both in the PHP for Windows page, side menu). Read carefully and maybe the enable-extensions section could differ (extension=curl instead of extension=php_curl.dll). Works like a charm, good luck!


They do not note that you need to rename the file for it to be called with just composer - good note here.
@AndrewAtkinson Alternatively you can rename before download with curl -sS https://getcomposer.org/installer | php -- --filename=composer
Just want to say thanks for this. I've had trouble understanding the underlying logic, and getcomposer.org itself doesn't do a very good job at explaining this, so thanks! I got it working.
M
Maerlyn

composer.phar can be ran on its own, no need to prefix it with php. This should solve your problem (being in the difference of bash's $PATH and php's include_path).


You can also rename it to just composer and then you can simply do composer install.
my composer.phar is install. (I Chekced using php composer.phar) But, i'm not getting the location. How to find path of composer.phar location? I'm using UBUNTU.
If you can run php composer.phar, then it's probably in your current directory.
I think you want a global access to your composer.phar. Move it and make an alias : sudo mv composer.phar /usr/local/bin/ then alias composer='/usr/local/bin/composer.phar'.
I did a sudo mv composer.phar /usr/local/bin/composer. After that I can use just composer install anywhere.
C
Community

How to run Composer From Anywhere (on MacOS X) via Terminal

Background:

Actually in getComposer website it clearly states that, install the Composer by using the following curl command,

curl -sS https://getcomposer.org/installer |php

And it certainly does what it's intended to do. And then it says to move the composer.phar to the directory /usr/local/bin/composer and then composer will be available Globally, by using the following command line in terminal!

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

Question:

So the problem which leads me to Google over it is when I executed the above line in Mac OS X Terminal, it said that, Permission denied. Like as follows:

mv: rename composer.phar to /usr/local/bin/composer: Permission denied

Answer:

Following link led me to the solution like a charm and I'm thankful to that. The thing I just missed was sudo command before the above stated "Move" command line. Now my Move command is as follows:

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

It directly prompts you to authenticate yourself and see if you are authorized. So if you enter a valid password, then the Moving will be done, and you can just check if composer is globally installed, by using the following line.

composer about

I hope this answer helped you to broaden your view and finally resolve your problem.

Cheers!


> And then it says to move the composer.phar to the directory /usr/local/bin/composer /usr/local/bin/composer is not a directory; it is the new name for the file that would be called composer.phar prior to that.
Worked for me on macOS Sierra.
M
Mahendran Sakkarai

First install the composer like mentioned in the composer installation documentation. I just added here for reference.

curl -sS https://getcomposer.org/installer | php

and then move the file to '/usr/local/bin'.

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

Try to run composer -V. If you get a output like Composer version followed by the version number then the composer is installed successfully.

If you get any output like composer: command not found means use the following command to create a alias for the composer. So it will be executed globally.

alias composer='/usr/local/bin/composer'

Now if you run composer -V means you will get the output as Composer Version followed by the version number.

Hope this will help someone.


p
pacholik

Just move it to /usr/local/bin folder and remove the extension

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

Just What I Needed, Thank You
T
Tahir Yasin

Simply run this command for installing composer globally

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

This solve my issue with asking for sudoer password. Thanks
C
Chris Burton

For MAC and LINUX use the following procedure:

Add the directory where composer.phar is located to you PATH:

export PATH=$PATH:/yourdirectory

and then rename composer.phar to composer:

mv composer.phar composer

u
user3257693

You can do a simple global install to run it from anywhere

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

The https://getcomposer.org/doc/00-intro.md#globally website recommends this way. Worked well on Ubuntu 14.04 no problem. This way you don't need to do as an example php compomser.phar show , you just do composer show , in any directory you are working with.


M
Muhammad Raihan Muhaimin

For running it from other location you can use the composer program that come with the program. It is basically a bash script. If you don't have it you can create one by simply copying the following code into a text file

#!/bin/sh

dir=$(d=$(dirname "$0"); cd "$d" && pwd)

if command -v 'cygpath' >/dev/null 2>&1; then
  dir=$(cygpath -m $dir);
fi

dir=$(echo $dir | sed 's/ /\ /g')
php "${dir}/composer.phar" $*

Then save the file inside your bin folder and name it composer without any file extension. Then add the bin folder to your environment variable f


k
karolus

Some of this may be due to the OS your server is running. I recently did a migration to a new hosting environment running Ubuntu. Adding this alias alias composer="/path/to/your/composer" to .bashrc or .bash_aliases didn't work at first because of two reasons:

The server was running csh, not bash, by default. To check if this is an issue in your case, run echo $0. If the what is returned is -csh you will want to change it to bash, since some processes run by Composer will fail using csh/tcsh.

To change it, first check if bash is available on your server by running cat /etc/shells. If, in the list returned, you see bin/bash, you can change the default to bash by running chsh -s /bin/csh.

Now, at this point, you should be able to run Composer, but normally, on Ubuntu, you will have to load the script at every session by sourcing your Bash scripts by running source ~/.bashrc or source ~/.bash_profile. This is because, in most cases, Ubuntu won't load your Bash script, since it loads .profile as the default script.

To load your Bash scripts when you open a session, try adding this to your .profile (this is if your Bash script is .bashrc—modify accordingly if .bash_profile or other):

if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

To test, close your session and reload. If it's working properly, running composer -v or which composer should behave as expected.