How to run composer from anywhere?

前端 未结 10 1465
名媛妹妹
名媛妹妹 2020-12-04 07:51

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, whe

相关标签:
10条回答
  • 2020-12-04 07:56

    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.

    0 讨论(0)
  • 2020-12-04 07:57

    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.

    1. Change into a directory in your path like cd /usr/local/bin
    2. Get Composer curl -sS https://getcomposer.org/installer | php
    3. Make the phar executable chmod a+x composer.phar
    4. Change into a project directory cd /path/to/my/project
    5. Use Composer as you normally would composer.phar install
    6. 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

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

    Update 2: For Windows 10 and PHP 7 I recommend this tutorial. 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!

    0 讨论(0)
  • 2020-12-04 07:57

    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

    0 讨论(0)
  • 2020-12-04 08:06

    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.

    0 讨论(0)
  • 2020-12-04 08:11

    Simply run this command for installing composer globally

    curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
    
    0 讨论(0)
  • 2020-12-04 08:12

    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.

    0 讨论(0)
提交回复
热议问题