Laravel is one of the most popular PHP frameworks for building modern web applications. If you are using Ubuntu as your development environment or web server, you can install Laravel with PHP, Composer, the Laravel installer, and a frontend build tool such as Node.js and npm.
This guide explains how to install Laravel on Ubuntu from scratch and configure it with Apache. It also covers creating a Laravel application, configuring a virtual host, setting permissions, connecting a database, and troubleshooting common installation problems.
Whether you are setting up Laravel for local development or preparing an Ubuntu server for a Laravel website, the steps below provide a practical starting point.
What You Need Before Installing Laravel
Before installing Laravel, make sure you have:
- Ubuntu with sudo access
- PHP installed
- PHP CLI and required PHP extensions
- Composer
- Laravel installer
- Node.js and npm, or Bun, for frontend assets
- Apache if you want to serve Laravel through Apache
- MySQL, PostgreSQL, or another supported database if your application requires one
Laravel's current installation documentation recommends PHP, Composer, the Laravel installer, and Node/NPM or Bun for a complete development environment.
Step 1: Update Ubuntu Packages
Start by updating your Ubuntu package index:
sudo apt updatesudo apt upgrade -y
Updating the system first helps ensure that you are working with the latest packages available from your configured Ubuntu repositories.
Step 2: Install PHP
Laravel requires PHP. If PHP is not already installed, install PHP and the PHP CLI:
sudo apt install php php-cli php-common -y
For an Apache-based Laravel installation, also install the Apache PHP module:
sudo apt install libapache2-mod-php -y
Ubuntu's documentation also lists database-specific PHP extensions such as php-mysql and php-pgsql when those databases are used.
Check your PHP version:
php -v
If PHP is installed correctly, you should see the installed PHP version in the terminal.
Step 3: Install Common PHP Extensions
Laravel applications commonly require several PHP extensions. A practical Ubuntu setup can include:
sudo apt install php-mbstring php-xml php-curl php-zip php-bcmath php-intl php-mysql unzip -y
If you use PostgreSQL instead of MySQL, install the PostgreSQL PHP extension:
sudo apt install php-pgsql -y
You can check the installed extensions with:
php -m
Step 4: Install Composer
Composer is PHP's dependency manager and is required to install Laravel and its packages.
The old article used a one-line Composer installation command and an outdated Laravel installer version. For a new setup, use Composer's official installation procedure instead.
First, download the Composer installer:
php -r "copy('', 'composer-setup.php');"
Run the installer:
php composer-setup.php
Move Composer to a directory in your system PATH:
sudo mv composer.phar /usr/local/bin/composer
Verify the installation:
composer --version
Composer's official documentation recommends installing the Composer executable globally when you want to use the composer command from different directories.
Step 5: Install the Laravel Installer
Once Composer is available, install the Laravel installer:
composer global require laravel/installer
Unlike the old tutorial, you do not need to install the obsolete laravel/installer=~1.1 package.
Add Composer's Global Bin Directory to PATH
Depending on your Composer configuration, the global Composer binaries are commonly located under:
~/.config/composer/vendor/bin
Add it to your PATH:
echo 'export PATH="$HOME/.config/composer/vendor/bin:$PATH"' >> ~/.bashrcsource ~/.bashrc
If your Composer installation uses the older directory, check it with:
composer global config bin-dir --absolute
Then add the returned directory to your PATH.
Verify the Laravel installer:
laravel --version
Step 6: Install Node.js and npm
Modern Laravel applications often use Vite to compile CSS and JavaScript assets. Laravel's documentation recommends installing Node.js and npm or Bun alongside PHP and Composer.
If Node.js and npm are not installed, install them using your preferred supported Node.js setup. After installation, verify them with:
node -vnpm -v
For an existing project, you will normally run:
npm installnpm run build
Step 7: Create a New Laravel Application
Now you can create a new Laravel application.
laravel new my-app
Replace my-app with the name of your application.
Move into the project directory:
cd my-app
Install the frontend dependencies:
npm install
Build the frontend assets:
npm run build
Step 8: Run Laravel's Development Server
For local development, you do not need Apache. Laravel can run using its development workflow.
php artisan serve
You can then open:
For newer Laravel applications, you can also use the Composer development script after installing frontend dependencies:
composer run dev
This is useful when you want Laravel's development server and frontend development tooling running together.
Step 9: Configure the Laravel Environment File
Laravel stores environment-specific configuration in the .env file.
When you create a new Laravel application, Laravel normally creates the environment file for you. Check that it exists:
ls -la
You should see a file named:
.env
Never commit your production .env file to a public Git repository because it can contain database credentials, API keys, and other sensitive configuration.
Step 10: Generate the Laravel Application Key
Generate an application encryption key with:
php artisan key:generate
This updates the APP_KEY value in your environment configuration.
You can confirm the application configuration with:
php artisan about
Step 11: Configure a Database
If your Laravel application uses a database, configure the database connection in the .env file.
For example, a MySQL configuration may look like this:
DB_CONNECTION=mysqlDB_HOST=127.0.0.1DB_PORT=3306DB_DATABASE=my_appDB_USERNAME=my_app_userDB_PASSWORD=your_password
After creating the database and configuring the credentials, run Laravel's database migrations:
php artisan migrate
If the connection is configured correctly, Laravel will create the tables defined by your migrations.
Step 12: Install Apache
If you want to serve Laravel through Apache instead of Laravel's development server, install Apache:
sudo apt updatesudo apt install apache2 -y
Check Apache's status:
sudo systemctl status apache2
If Apache is running, you can test it by visiting your server's IP address in a browser.
Step 13: Enable Apache mod_rewrite
Laravel relies on URL rewriting so that requests can be handled by its front controller.
Enable Apache's rewrite module:
sudo a2enmod rewrite
Then restart Apache:
sudo systemctl restart apache2
You can verify that the module is enabled with:
sudo apache2ctl -M | grep rewrite
If rewrite_module appears in the output, the module is enabled.
Step 14: Configure an Apache Virtual Host for Laravel
For a production or server-based Laravel installation, you should configure an Apache virtual host instead of accessing the application through a URL such as:
The Apache document root should point directly to Laravel's public directory.
Create the Virtual Host File
Create a new Apache configuration file:
sudo nano /etc/apache2/sites-available/my-app.conf
Add the following configuration:
<VirtualHost *:80> ServerName my-app.com ServerAlias www.my-app.com DocumentRoot /var/www/my-app/public <Directory /var/www/my-app/public> AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/my-app-error.log CustomLog ${APACHE_LOG_DIR}/my-app-access.log combined</VirtualHost>
Replace my-app.com with your actual domain and update the Laravel project path if necessary.
Why Does the DocumentRoot Point to public?
This is an important Laravel security practice.
Your Apache document root should be:
/var/www/my-app/public
and not:
/var/www/my-app
The public directory is designed to be the web-accessible entry point of a Laravel application. Application code, configuration, storage files, and other internal files should not be directly exposed through the web server.
Step 15: Move or Clone Your Laravel Application
If your application is currently in your home directory and you want Apache to serve it from /var/www, you can move it:
sudo mv ~/my-app /var/www/my-app
Alternatively, for a Git-based deployment, clone your project directly into the desired directory.
Step 16: Set Laravel Directory Permissions
Laravel needs write access to the storage and bootstrap/cache directories.
For an Apache deployment, make sure the web server can write to these directories:
sudo chown -R www-data:www-data /var/www/my-app/storagesudo chown -R www-data:www-data /var/www/my-app/bootstrap/cache
You can also ensure that the directories have appropriate permissions:
sudo chmod -R 775 /var/www/my-app/storagesudo chmod -R 775 /var/www/my-app/bootstrap/cache
Avoid using chmod -R 777 as a quick fix. Broad write permissions can create unnecessary security risks.
Step 17: Enable the Laravel Virtual Host
Enable the site:
sudo a2ensite my-app.conf
If you do not need Apache's default website, you can disable it:
sudo a2dissite 000-default.conf
Check the Apache configuration before restarting:
sudo apache2ctl configtest
If the result is:
Syntax OK
restart Apache:
sudo systemctl restart apache2
Step 18: Configure the Hosts File for Local Development
If you are testing a domain locally rather than using a real DNS record, add the domain to your Ubuntu hosts file.
sudo nano /etc/hosts
Add an entry such as:
127.0.0.1 my-app.com
Save the file and open:
For a remote VPS, your DNS A record should point the domain to the server's public IP address instead of using the local hosts-file approach.
Step 19: Optimize Laravel for Production
Before deploying a Laravel application to production, install the application's PHP dependencies without development packages:
composer install --no-dev --optimize-autoloader
Build frontend assets:
npm installnpm run build
Then cache Laravel's configuration, routes, and views:
php artisan optimize
Make sure your production environment contains:
APP_ENV=productionAPP_DEBUG=false
Do not leave APP_DEBUG=true enabled on a public production website because detailed error information can expose application configuration and other sensitive information.
Step 20: Test Your Laravel Installation
At this point, test the application from your browser.
For local development:
For an Apache virtual host:
If you see Laravel's application page, your installation is working.
Useful Laravel Commands After Installation
Once Laravel is installed, these commands are useful during development:
# Check Laravel versionphp artisan --version# List Artisan commandsphp artisan list# Start the local development serverphp artisan serve# Run database migrationsphp artisan migrate# Create a migrationphp artisan make:migration create_products_table# Create a controllerphp artisan make:controller ProductController# Clear application cachesphp artisan optimize:clear# Clear configuration cachephp artisan config:clear# Clear route cachephp artisan route:clear# Clear compiled viewsphp artisan view:clear
Common Laravel Installation Problems
1. "laravel: command not found"
This usually means the directory containing the Laravel installer is not in your PATH.
Find Composer's global binary directory:
composer global config bin-dir --absolute
Add the returned directory to your shell's PATH and reload your shell.
source ~/.bashrc
Then test:
laravel --version
2. "Your requirements could not be resolved to an installable set of packages"
This usually indicates a dependency or PHP-version compatibility problem.
First check your PHP version:
php -v
Then check your Composer environment:
composer diagnose
Composer recommends composer diagnose as a first troubleshooting step when Composer problems occur.
3. Apache Shows a 403 Forbidden Error
Check that Apache has permission to access the Laravel application's public directory:
ls -la /var/www/my-app/public
Also verify that your virtual host contains:
<Directory /var/www/my-app/public> AllowOverride All Require all granted</Directory>
Then test and restart Apache:
sudo apache2ctl configtestsudo systemctl restart apache2
4. Laravel Returns a 500 Internal Server Error
Check the Laravel log:
tail -f /var/www/my-app/storage/logs/laravel.log
Also check Apache's error log:
sudo tail -f /var/log/apache2/error.log
Common causes include incorrect environment variables, missing PHP extensions, incorrect permissions, or a missing application key.
5. CSS or JavaScript Is Not Loading
If you are using Vite, install the frontend dependencies and build the assets:
npm installnpm run build
During local development, you can use the appropriate Vite development workflow instead.
6. "Storage" or "Bootstrap Cache" Permission Error
Make sure the Apache user can write to the required directories:
sudo chown -R www-data:www-data /var/www/my-app/storagesudo chown -R www-data:www-data /var/www/my-app/bootstrap/cache
7. Apache Does Not Rewrite Laravel URLs
Enable mod_rewrite:
sudo a2enmod rewritesudo systemctl restart apache2
Also confirm that AllowOverride All is enabled for Laravel's public directory.
Laravel Installation Checklist
Use this checklist before considering your Ubuntu Laravel setup complete:
- PHP is installed and compatible with your Laravel version.
- Required PHP extensions are installed.
- Composer is installed and accessible globally.
- The Laravel installer is installed.
- Node.js and npm or Bun are installed if frontend assets are required.
- The Laravel application was created successfully.
- The
.envfile is configured. - The application key has been generated.
- The database connection has been configured if required.
- Apache is installed if you are using Apache.
mod_rewriteis enabled.- Apache's document root points to Laravel's
publicdirectory. storageandbootstrap/cacheare writable.- Apache configuration passes
apache2ctl configtest. - Production applications have
APP_DEBUG=false.
Conclusion
Installing Laravel on Ubuntu is straightforward once the required PHP environment, Composer, Laravel installer, and frontend tooling are configured correctly. For local development, Laravel's development server provides a simple way to get started. For Apache-based deployments, configure a virtual host with Laravel's public directory as the document root and make sure the required directories have the correct permissions.
If you are deploying Laravel to production, take the additional steps of configuring your environment variables, database, HTTPS, file permissions, caching, backups, and server security before making the application publicly accessible.
With the setup complete, you can now start building your Laravel application and use Artisan, migrations, controllers, models, queues, and Laravel's other development tools.
Frequently Asked Questions About Installing Laravel on Ubuntu
What is the easiest way to install Laravel on Ubuntu?
The standard approach is to install PHP and Composer, install the Laravel installer with Composer, and then create your application with laravel new application-name. Laravel also provides an official one-command PHP environment installer for Linux through php.new.
Can I install Laravel without Apache?
Yes. Apache is not required for Laravel development. You can use Laravel's development server with:
php artisan serve
Apache or another production web server becomes relevant when you want to serve your application through a web server on a VPS or production machine.
Do I need Composer to install Laravel?
Composer is a core part of the standard Laravel PHP development workflow because Laravel and its PHP dependencies are distributed through Composer.
Which PHP version does Laravel require?
The required PHP version depends on the Laravel release you are installing. Always check the requirements for your specific Laravel version before installing PHP or deploying an existing application.
Where should I install Laravel on Ubuntu?
For local development, you can keep your project in your home directory. For an Apache server, a common location is /var/www/my-app, with Apache's DocumentRoot pointing to /var/www/my-app/public.
Why should Apache point to Laravel's public directory?
Laravel's public directory is intended to be the web-accessible entry point. Pointing Apache at the project root can expose application files that should remain outside the public web directory.
How do I check whether Laravel is installed?
If you installed the Laravel installer, run:
laravel --version
For an existing Laravel project, you can check the framework version with:
php artisan --version
How do I create a new Laravel project?
After installing the Laravel installer, run:
laravel new my-app
Then enter the project directory:
cd my-app
How do I run Laravel locally on Ubuntu?
Open your Laravel project directory and run:
php artisan serve
Then visit in your browser.
Why am I getting "laravel: command not found"?
The Laravel installer is probably installed, but Composer's global binary directory is not included in your PATH. Run composer global config bin-dir --absolute, add the returned directory to your PATH, and reload your shell.
How do I fix Laravel permission errors on Ubuntu?
Laravel needs write access to its storage and bootstrap/cache directories. For Apache deployments, make sure those directories are writable by the web server user, commonly www-data.
How do I connect Laravel to MySQL?
Create a MySQL database and configure the DB_* variables in Laravel's .env file. Then run:
php artisan migrate
Can Laravel run on an Ubuntu VPS?
Yes. A typical VPS deployment includes Ubuntu, PHP, Composer, the Laravel application, a database when required, and a web server such as Apache or Nginx. For production, configure the server carefully, use HTTPS, disable debug mode, and apply appropriate file permissions.
How do I update Laravel dependencies?
For an existing project, Composer manages Laravel's PHP dependencies. Before making major dependency updates on a production application, review the Laravel upgrade documentation and test the update in a staging environment.
Last updated: