DEV Community

Cover image for Setting Up A Laravel Local Dev Environment On Ubuntu 🐘🐧
Andrew Brooks 👨‍💻
Andrew Brooks 👨‍💻

Posted on • Updated on

Setting Up A Laravel Local Dev Environment On Ubuntu 🐘🐧

Let's get a simple Laravel dev environment running on Ubuntu desktop 19.04 & 18.04 including a complete MySQL database connection.

This article assumes some familiarity with the Linux terminal.

Install PHP

Add and install the PHP 7.3 PPA

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php7.3

Verify the PHP version installed

php -v

Output:

PHP 7.3.8-1+ubuntu19.04.1+deb.sury.org+1 (cli) (built: Aug  7 2019 09:52:53) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.8, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.8-1+ubuntu19.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

Install MySQL database

sudo apt-get install php7.3-mysql
sudo apt-get install mysql-server
sudo mysql_secure_installation

Now you'll be presented with some prompts.

For a local dev environment I skip the 'Validate Password Plugin'.

You will be asked to enter a password for the root user.

From there remove the anonymous users, disallow the root user from remote access, and remove the test database.

When asked to reload privilege tables select yes.

Create a non-root user with root privilages

We will use this user when configuring our Laravel application later on.

Log into MySQL as root.

sudo mysql -u root -p

Run the following SQL to create a new user admin.

CREATE USER 'admin'@'localhost' IDENTIFIED BY '';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;

Enter exit to leave the MySQL CLI.

mysql> exit

Test out the new user by logging in as admin without using sudo.

mysql -u admin

You should see something like:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.27-0ubuntu0.19.04.1 (Ubuntu)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> _

Exit MySql and we'll move on to installing Composer.

mysql> exit

Install Composer

First, we need to update our package manager.

sudo apt update

Now we are going to install some dependencies needed for the Composer installation.

  • curl for downloading Composer
  • php-cli is needed to run the install
  • php-mbstring provides functions for a library used
  • git will allow Composer to download project dependencies
  • unzip for unzipping packages
sudo apt install curl php-cli php-mbstring git unzip

With that out of the way we can download the Composer installer.

Begin by moving into your home directory

cd ~

and starting the Composer installer download.

curl -sS https://getcomposer.org/installer -o composer-setup.php

Before we can run the installer it needs to be verified for corruption.

Visit https://composer.github.io/pubkeys.html and find the 'Installer Signature (SHA-384)'

Use the signature and run the following commands to verify your installer.

HASH=your_hash_goes_here
php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

You should see the output of

Installer verified

otherwise you will need to re-download the installer and go through the verification steps again.

Run the installer

Now we are free to install Composer globally on the system.

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

Output:

All settings correct for using Composer
Downloading...

Composer (version 1.9.0) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

Lastly we need to update permissions to the composer directory.

sudo chown -R $USER ~/.composer/

Verify Composer is installed

⚠️ Do not run composer as root using sudo ⚠️

Simply run

composer

and you should see:

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.9.0 2019-08-02 20:55:32

Time for Laravel!

But first, some dependencies.

sudo apt-get install php7.3-zip
sudo apt-get install php7.3-xml

Download the Laravel installer using Composer.

composer global require laravel/installer

Now we need to update the Linux $PATH

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

After updating the $PATH close your terminal window and open a fresh one.

Hello, World

Let's get a boilerplate Laravel app running on our localhost.

First we will need to create the database that we will be connecting to.

Log into MySql via the terminal using the admin user that was created earlier.

mysql -u admin

Now run the following SQL to create a database named laravel.

CREATE DATABASE laravel;

With our database created we can move to the Laravel side of things.

cd via the terminal into the folder you want to create your Laravel application in.

For me that's ~/dev/laravel/

Run the following command to scaffold the app:

laravel new hello-world

hello-world is just the name of the app so name it whatever you like.

Once this finishes cd into the new directory for your app.

cd hello-world

At this point we need to tell the Laravel app how to connect to our database.

For this we will edit the .env file in the root of the application.

sudo nano .env

The connection settings should look like this

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=admin
DB_PASSWORD=

You can save the changes from nano with ctrl+x-> 'y' -> [ENTER]

Now we need to test this connection.

For this we will edit /routes/web.php

It should end up looking like

Route::get('/', function () {
    // return view('welcome');
    try {
        DB::connection()->getPdo();
        $caught = false;
    } catch (Exception $e) {
        $caught = true;
        die("Could not connect to the database.  Please check your configuration. error:" . $e );
    }

    if(!$caught){
        echo 'Hello, world.';
    }
});

Now start the server by running

php artisan serve

Output:

Laravel development server started: <http://127.0.0.1:8000>

Now you can check it out in your browser at http://127.0.0.1:8000

You should see 'Hello, world.' printed to the screen signalling a successful connection to the database.

That's it! 🎉

You now have PHP 7.3, MySql, Composer, and the Laravel installer configured for your local Ubuntu development environment with a running starter Laravel app.


I'm Andrew, a full stack software developer from the deep south who makes internet things.

Hit me up on twitter @andrew_brooksie

Reach out if you have any questions or requests for future write-ups!

Oldest comments (3)

Collapse
 
rezaodb profile image
Reza

Thanks, it works like charm ! I've shorten the process by simply running "sudo apt install composer" and to test the connection: "php artisan migrate" :-)

Collapse
 
gitwithuli profile image
gitwithuli

Thank you.

Collapse
 
thorondormanwe profile image
Carlos Rangel

Thank you, this helped me a lot