You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Linux安装带vendor/bin目录的Composer以按Laravel文档运行laravel new命令

Alright, let's walk through exactly how to get Composer set up on Linux so you can run laravel new as the official Laravel docs describe—this means getting the full setup with a vendor/bin directory (not just a standalone Composer binary) so all the necessary tools are accessible.

Step 1: Install PHP & Required Extensions

Composer and Laravel depend on PHP and several core extensions. Start by updating your package manager and installing these dependencies:

  • Debian/Ubuntu/Linux Mint:
    sudo apt update
    sudo apt install php-cli php-zip php-json php-mbstring php-tokenizer php-xml php-curl php-gd
    
  • RHEL/CentOS/Fedora/Rocky Linux:
    sudo dnf update
    sudo dnf install php-cli php-zip php-json php-mbstring php-tokenizer php-xml php-curl php-gd
    
Step 2: Install Composer Globally (With Global vendor Directory)

Instead of just grabbing a standalone composer.phar file, we'll set up Composer system-wide. This creates a global vendor directory where tools like the Laravel Installer will live, plus makes composer accessible from any terminal window.

  1. Download the official Composer installer script and verify its integrity:
    php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
    
  2. Run the installer to place Composer in a system-wide bin directory and set up the global vendor structure:
    sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
    
  3. Clean up the installer script:
    php -r "unlink('composer-setup.php');"
    
  4. Confirm the installation worked:
    composer --version
    
Step 3: Install the Laravel Installer (Adds laravel new Command)

The laravel new command comes from the Laravel Installer package. Install it globally via Composer, and it will be placed in your global vendor/bin directory:

composer global require laravel/installer
Step 4: Add Composer's Global vendor/bin to Your PATH

To run laravel new from any directory, you need to add the global vendor/bin path to your shell's configuration:

  1. First, confirm the exact path of your global vendor/bin (it’s usually ~/.config/composer/vendor/bin for Debian/Ubuntu or ~/.composer/vendor/bin for RHEL/Fedora):
    composer global config bin-dir --absolute
    
  2. Add this path to your shell config file. For Bash, edit ~/.bashrc; for Zsh, edit ~/.zshrc:
    # For Bash users
    echo 'export PATH="$PATH:~/.config/composer/vendor/bin"' >> ~/.bashrc
    # For Zsh users
    echo 'export PATH="$PATH:~/.config/composer/vendor/bin"' >> ~/.zshrc
    
  3. Reload your shell to apply the change:
    # Bash
    source ~/.bashrc
    # Zsh
    source ~/.zshrc
    
Step 5: Test the laravel new Command

You’re all set! Create your first Laravel project with:

laravel new my-first-laravel-app

This will generate a project directory, install all dependencies, and create a local vendor/bin folder inside your project (with tools like artisan included).

Alternative: Use composer create-project Directly

If you don’t want to install the Laravel Installer, you can still create a project directly via Composer (no laravel command required):

composer create-project laravel/laravel my-laravel-project

This will also generate the project’s vendor/bin directory automatically.


内容的提问来源于stack exchange,提问作者wadleo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:43:48