如何在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.
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
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.
- 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;" - Run the installer to place Composer in a system-wide bin directory and set up the global
vendorstructure:sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer - Clean up the installer script:
php -r "unlink('composer-setup.php');" - Confirm the installation worked:
composer --version
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
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:
- First, confirm the exact path of your global
vendor/bin(it’s usually~/.config/composer/vendor/binfor Debian/Ubuntu or~/.composer/vendor/binfor RHEL/Fedora):composer global config bin-dir --absolute - 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 - Reload your shell to apply the change:
# Bash source ~/.bashrc # Zsh source ~/.zshrc
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

