Install Composer and Laravel on Ubuntu

Summary: Complete guide to install Composer and Laravel globally.


Laravel is one of the most popular PHP frameworks, and Composer is the de-facto dependency manager for PHP projects. Setting up both tools globally on Ubuntu streamlines your development workflow and ensures you're ready to build robust PHP applications. This guide walks you through the entire process, from prerequisites to verifying your installation.


Table of Contents


Prerequisites

  • An Ubuntu system (20.04, 22.04, 24.04, or similar)
  • Sudo privileges
  • Basic command line knowledge
  • Internet connection

Step 1: Update Your System

Start by ensuring your package list and installed packages are up-to-date:

sudo apt update
sudo apt upgrade -y

Step 2: Install PHP and Required Extensions

Laravel requires PHP 8.1 or higher. Install PHP and some commonly required extensions:

sudo apt install php php-cli php-mbstring php-xml php-bcmath php-zip php-curl unzip git -y

You can check your PHP version with:

php -v

Step 3: Install Composer Globally

Composer can be installed easily using the official installer script.

  1. Download and verify Composer installer:

    cd ~
    php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    HASH=$(wget -q -O - https://getcomposer.org/installer.sig)
    php -r "if (hash_file('sha384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
    
  2. Install Composer globally:

    sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
    
  3. Remove installer script:

    php -r "unlink('composer-setup.php');"
    

Check Composer version:

composer --version

Step 4: Install Laravel Installer Globally

With Composer installed, you can install the Laravel installer globally using Composer’s global command:

composer global require laravel/installer

After installation, make sure your global Composer vendor/bin directory is in your system's PATH.

Add this line to your ~/.bashrc or ~/.zshrc:

export PATH="$HOME/.config/composer/vendor/bin:$PATH"

If you use Zsh:

echo 'export PATH="$HOME/.config/composer/vendor/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

If you use Bash:

echo 'export PATH="$HOME/.config/composer/vendor/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Step 5: Verify Installations

Verify that both Composer and the Laravel installer are correctly installed:

composer --version
laravel --version

You should see version numbers for both tools.


Step 6: Create a New Laravel Project

Now you're ready to create new Laravel applications anywhere on your system.

laravel new myapp
cd myapp
php artisan serve
  • Open your browser at http://127.0.0.1:8000 to see your Laravel app running!

Troubleshooting Common Issues

  • Composer not found: Make sure /usr/local/bin is in your $PATH.
  • Laravel not found: Confirm ~/.config/composer/vendor/bin is added to your $PATH.
  • Permission denied: Use sudo only where required. Avoid using it with composer global commands.
  • PHP extensions missing: Double-check your installed PHP extensions if you get errors about missing extensions.

Conclusion

Setting up Composer and Laravel globally on Ubuntu is a straightforward process that primes your machine for modern PHP development. By following this guide, you can effortlessly create new Laravel projects and manage dependencies with Composer, streamlining your workflow for future projects. Happy coding!


Further Reading:


Let us know in the comments if you encounter any installation issues or have tips for other developers!