Setting up a dev environment for Laravel - the easy way
Tags: ubuntu, laravel
Published: 2020-04-02 15:47:46

> 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](https://www.lifewire.com/install-linux-mint-4173111)

Now let's begin our setup. It consists of the following steps:
- [Installing Zsh](#install-zsh)
- [Install php, php-dependencies, composer](#install-php)
- [Install valet, valet external libraries](#install-valet)
- [Install laravel installer](#install-laravel)
- [Install mysql, gui tool](#install-mysql)
- [Install nodejs, vue-cli](#install-vue-cli)

<a name="install-zsh"></a>
## 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](http://zsh.sourceforge.net/Intro/intro_toc.html).

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

```shell
sudo apt update
sudo apt upgrade
sudo apt install zsh
```

Now let's setup [**Oh My Zsh**](https://ohmyz.sh/).

> 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**
```shell
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](https://linuxhandbook.com/change-shell-linux/).

Follow [this guide](https://github.com/ohmyzsh/ohmyzsh/) for settings up plugins and themes for Zsh.

<a name="install-php"></a>
## 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](https://stitcher.io/blog/new-in-php-74).

**Install php by typing:**
```bash
sudo apt install php7.3-cli
```

If you are getting an error while installing PHP like:
```bash
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

```bash
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

```bash
php -v
```
which will give the output,
```bash
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](https://laravel.com/docs/7.x#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.

```bash
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](https://getcomposer.org/).

```bash
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
```bash
composer
```
which results in
```bash
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.10.1 2020-03-13 20:34:27
...
```

**Now, add the composer global tools to your PATH:**

- open `zshrc` file:
```bash 
nano ~/.zshrc
```

- add these lines to the last of your `zshrc` file
```bash
PATH=$HOME/.config/composer/vendor/bin:~/.composer/vendor/bin:$PATH
export PATH
```

- Now read and execute the file by:
```bash
source ~/.zshrc
```

<a name="install-valet"></a>
## 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](https://cpriego.github.io/valet-linux/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**
```bash
sudo apt-get install network-manager libnss3-tools jq xsel
```

**Install Valet via composer:**
```bash
composer global require cpriego/valet-linux
```

**Run the valet install command.**
```bash
valet install
```

**Once Valet is installed, you’re ready to start serving sites.**
```bash
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.

<a name="install-laravel"></a>
## Install the Laravel Installer

```bash
composer global require laravel/installer
```

**Create a new Laravel site within the `Sites` directory:**
```bash
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. 

<a name="install-mysql"></a>
## Install Mysql

```bash
sudo apt update
sudo apt install mysql-server
```
**configure a MySQL `root` user to work with.**

- open up the MySQL prompt from your terminal:
```bash
mysql -u root
```

- modify the `root` user with all privileges.
```bash
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](https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-18-04).

**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.

- [SequelPro](https://www.sequelpro.com/) only available for Mac.
- [TablePlus](https://tableplus.com/)
- [Workbench](https://www.mysql.com/products/workbench/)
- [PhpMyAdmin](https://www.phpmyadmin.net/)

<a name="install-vue-cli"></a>
## Install vue-cli

> Before we start installing vue-cli, it requires `nodejs` and `npm` as its dependency. [Follow this guide](https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-18-04) for installing nodejs and npm in your system.

Vue-cli provides a full system for rapid Vue.js development. [Follow this guide](https://cli.vuejs.org/guide/installation.html) for installing vue-cli to your system.

Great!! Build something amazing. :metal: