DEV Community

Abhishek
Abhishek

Posted on • Updated on

Setting up a dev environment for Laravel - the easy way

Working with the Laravel ecosystem is great. But before starting to build an application using Laravel, there always a complicated setup for your development machine that needs to be done first.

So, in this post, I am writing a step by step guide to setup your dev machine that will make your day awesome. In this guide, I am using ubuntu as my operating system. I am using a lightweight version of Ubuntu called Linux mint.

Read more about the installation of Linux Mint at How to Install Linux Mint

Now let's begin our setup. It consists of the following steps:

Installing Zsh

The Z shell (zsh) is a Unix shell. Zsh can be thought of as an extended Bourne shell with a large number of improvements, including some features of bash, ksh, and tcsh.

Read more about ZSH at An Introduction to the Z Shell.

Open a terminal window, Copy & paste the following into the terminal.

sudo apt update
sudo apt upgrade
sudo apt install zsh

Now let's setup Oh My Zsh.

Oh My Zsh is a delightful, open-source, community-driven framework for managing your Zsh configuration. It comes bundled with thousands of helpful functions, helpers, plugins, themes, and a few things that make you shout...

Install oh-my-zsh by entering

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

After the installation has been completed you need to make zsh as the default shell.

Follow this guide for settings up plugins and themes for Zsh.

Install php, php-dependencies, composer

Now, we need to install PHP to work with the Laravel framework. We will be using PHP version 7.3 so that it doesn't break the external packages we use with Laravel (since most of the external packages have not been updated to PHP-7.4 while writing this post and the latest Laravel 7.x requires PHP >= 7.2.5).

You could read more about PHP 7.4 here.

Install php by typing:

sudo apt install php7.3-cli

If you are getting an error while installing PHP like:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package php7.3-cli
E: Couldn't find any package by glob 'php7.3-cli'
E: Couldn't find any package by regex 'php7.3-cli'

then make sure to add the ppa:ondrej/php repository by

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install -y php7.3

Now, check PHP version

php -v

which will give the output,

PHP 7.3.16-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Mar 20 2020 13:51:46) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.16, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.16-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

Install PHP external dependencies, that is required by Laravel framework

Laravel Server Requirements mention that BCMath, Ctype, Fileinfo, JSON, Mbstring, OpenSSL, PDO, Tokenizer and XML extensions are required. Most of the extensions are installed and enabled by default.

sudo apt install openssl php7.3-common php7.3-bcmath php7.3-curl php7.3-json php7.3-mbstring php7.3-mysql php7.3-xml php7.3-zip php7.3-sqlite3

We will be using the SQLite package for our PHP unit testing.

Install composer

A Dependency Manager for PHP.

You could learn more about the composer here.

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer

To check if composer installed correctly, please enter

composer

which results in

  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.10.1 2020-03-13 20:34:27
...

Now, add the composer global tools to your PATH:

  • open zshrc file:
nano ~/.zshrc
  • add these lines to the last of your zshrc file
PATH=$HOME/.config/composer/vendor/bin:~/.composer/vendor/bin:$PATH
export PATH
  • Now read and execute the file by:
source ~/.zshrc

Install valet

Valet is a Laravel development environment for Mac minimalists. No Vagrant, no /etc/hosts file. You can even share your sites publicly using local tunnels. Yeah, we like it too.

Since, we are using ubuntu as our operating system, to make valet work with the system we will be using a package called valet linux.

Valet Linux is a Laravel development environment for Linux minimalists. No Vagrant, no /etc/hosts file. You can even share your sites publicly using local tunnels. Yeah, we like it too.

Before installation, you should review your system specific requirements and make sure that no other programs such as Apache or Nginx are binding to your local machine’s port 80.

Add valet external dependencies

sudo apt-get install network-manager libnss3-tools jq xsel

Install Valet via composer:

composer global require cpriego/valet-linux

Run the valet install command.

valet install

Once Valet is installed, you’re ready to start serving sites.

mkdir ~/Sites
cd ~/Sites
valet park

The valet park command will register your current working directory (~/Sites) as a path that Valet should search for sites.

Install the Laravel Installer

composer global require laravel/installer

Create a new Laravel site within the Sites directory:

laravel new blog

Open http://blog.test in your browser. You should see a default Laravel welcome page.

Next, we need to install Mysql as our database management system.

Install Mysql

sudo apt update
sudo apt install mysql-server

configure a MySQL root user to work with.

  • open up the MySQL prompt from your terminal:
mysql -u root
  • modify the root user with all privileges.
DROP USER 'root'@'localhost';
CREATE USER 'root'@'localhost' IDENTIFIED BY '';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;

the above command will create a root user without a password.

As we are in local development mode, trying to neglect the password field. In the production server, always create a different user with the required privileges and a strong password.

You could read more about setting up MySQL in ubuntu here.

Using database management tools
there are many great guy tools available for managing databases. I am listing the ones which I personally prefer the most.

Install vue-cli

Before we start installing vue-cli, it requires nodejs and npm as its dependency. Follow this guide for installing nodejs and npm in your system.

Vue-cli provides a full system for rapid Vue.js development. Follow this guide for installing vue-cli to your system.

Great!! Build something amazing. 🤘

Latest comments (3)

Collapse
 
jhonathanalfonso profile image
JhonathanAlfonso

If you have issue with composer instalation, you can view the official guide in composer website: getcomposer.org/download/

Collapse
 
tdrcavallini profile image
Thiago Cavallini

Awesome tutorial, tks a lot!

Collapse
 
wouterv profile image
WouterV

Thanks for the tutorial, this helped me a lot! One thing that worked different on my machine is that to get the mysql shell, I had to do sudo mysql.