DEV Community

Cover image for Laravel installs in Ubuntu step by step.
Md Abu Musa
Md Abu Musa

Posted on

Laravel installs in Ubuntu step by step.

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 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Start Apache and enable it to run on startup:

sudo systemctl start apache2 sudo systemctl enable apache2 
Enter fullscreen mode Exit fullscreen mode

To check Apache status:

sudo systemctl status apache2 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Check PHP version:

php -v 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Check if Composer is installed:

composer -v 
Enter fullscreen mode Exit fullscreen mode

Step 5: Install MySQL (or PostgreSQL)

Laravel can work with different databases. We'll go with MySQL:

sudo apt install mysql-server 
Enter fullscreen mode Exit fullscreen mode

Secure the MySQL installation by running:

sudo mysql_secure_installation 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Check versions:

node -v npm -v 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Assign correct permissions:

sudo chown -R $USER:$USER /var/www/your-laravel-app 
Enter fullscreen mode Exit fullscreen mode

Next, create a new virtual host file:

sudo nano /etc/apache2/sites-available/your-laravel-app.conf 
Enter fullscreen mode Exit fullscreen mode

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> 
Enter fullscreen mode Exit fullscreen mode

Enable the site and mod_rewrite:

sudo a2ensite your-laravel-app.conf sudo a2enmod rewrite 
Enter fullscreen mode Exit fullscreen mode

Restart Apache:

sudo systemctl restart apache2 
Enter fullscreen mode Exit fullscreen mode

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 . 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Step 11: Update Hosts File

Add your virtual host name to the hosts file:

sudo nano /etc/hosts 
Enter fullscreen mode Exit fullscreen mode

Add the following line:

127.0.0.1 your-laravel-app.local 
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (0)