To set up Laravel in a fresh Ubuntu, follow these steps:
Step 1: Update and Upgrade System Packages
Run the following commands to update your system's package list and upgrade installed packages:
sudo apt update
sudo apt upgrade
Step 2: Install Apache (or Nginx)
For a web server, you can choose either Apache or Nginx. Here, we'll go with Apache:
sudo apt install apache2
Start Apache and enable it to run on startup:
sudo systemctl start apache2
sudo systemctl enable apache2
To check Apache status:
sudo systemctl status apache2
Step 3: Install PHP and Extensions
Laravel requires PHP, so install PHP along with necessary extensions:
sudo apt install php php-cli php-mbstring php-xml php-bcmath php-tokenizer php-json php-curl php-zip php-mysql libapache2-mod-php
Check PHP version:
php -v
Step 4: Install Composer
Composer is required for managing Laravel dependencies. To install it, run:
sudo apt install curl
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
Check if Composer is installed:
composer -v
Step 5: Install MySQL (or PostgreSQL)
Laravel can work with different databases. We'll go with MySQL:
sudo apt install mysql-server
Secure the MySQL installation by running:
sudo mysql_secure_installation
Step 6: Install Node.js and NPM
Laravel uses Node.js for compiling front-end assets. Install it with NPM:
sudo apt install nodejs npm
Check versions:
node -v
npm -v
Step 7: Set Up Virtual Host (for Apache)
Create a virtual host for your Laravel project. First, navigate to the default Apache site directory:
cd /var/www/
sudo mkdir your-laravel-app
Assign correct permissions:
sudo chown -R $USER:$USER /var/www/your-laravel-app
Next, create a new virtual host file:
sudo nano /etc/apache2/sites-available/your-laravel-app.conf
Add the following configuration inside the file:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName your-laravel-app.local
DocumentRoot /var/www/your-laravel-app/public
<Directory /var/www/your-laravel-app>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enable the site and mod_rewrite:
sudo a2ensite your-laravel-app.conf
sudo a2enmod rewrite
Restart Apache:
sudo systemctl restart apache2
Step 8: Install Laravel
Navigate to the /var/www/your-laravel-app
directory and install Laravel using Composer:
cd /var/www/your-laravel-app
composer create-project --prefer-dist laravel/laravel .
Step 9: Configure Database Connection
Open .env
file in your Laravel project and update the database configuration:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password
Step 10: Set Permissions
Set the correct permissions for the storage and bootstrap/cache directories:
sudo chown -R www-data:www-data /var/www/your-laravel-app
sudo chmod -R 775 /var/www/your-laravel-app/storage
sudo chmod -R 775 /var/www/your-laravel-app/bootstrap/cache
Step 11: Update Hosts File
Add your virtual host name to the hosts file:
sudo nano /etc/hosts
Add the following line:
127.0.0.1 your-laravel-app.local
Step 12: Run Laravel Project
Access your Laravel project in the browser by visiting http://your-laravel-app.local
.
This will give you a working Laravel environment on your Ubuntu system!