How to Setup a Local Environment for Laravel Development With PHP, Node.Js, MySQL And Redis

Written by dkhorev | Published 2022/09/05
Tech Story Tags: laravel | phpstorm | productivity | software-development | software-engineering | php | laravel-framework | laravel-tips-and-tricks

TLDRIn this part of my series, we will install a minimum viable local environment for Laravel development. We’ll learn how to switch between PHP 8.0 and 8.1 versions of PHP. We will install Node.js and switch Node.js versions with nvm (node version manager) We will then launch common supporting services for Laravel applications — MySQL database and Redis database via Docker.via the TL;DR App

To follow along I recommend either a Linux machine or a virtual machine with fresh Ubuntu installed (Virtualbox recommended).

VM has the advantage that your work environment is isolated from the personal environment, and can be easily snapshotted and rolled back in case of any trouble.

In this part of my series, we will install a minimum viable local environment for Laravel development.


Here we install 2 versions of PHP binaries and we’ll learn how to switch between them. Most modern projects would be 8.0 or 8.1 versions of PHP so it’s OK to have them both installed.

Next, we will install Node.js binaries and also will learn how to switch versions with nvm (node version manager).

We will then launch common supporting services for Laravel applications — MySQL database and Redis database via Docker.

Finally, we will install a clean version of PhpStorm.


PHP 8.0 and PHP 8.1 binaries installation

#!/usr/bin/env bash

sudo apt update
sudo apt -y install curl

# Set up PHP 8.0
sudo apt update
sudo apt -y install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt -y install --no-install-recommends php8.0 composer php8.0-xml \
php8.0-intl php8.0-curl php8.0-zip php8.0-sqlite3 php8.0-mysql \
php8.0-xdebug php8.0-mbstring php8.0-redis

# Set up PHP 8.1
sudo apt update
sudo apt -y install --no-install-recommends php8.1 composer php8.1-xml \
php8.1-intl php8.1-curl php8.1-zip php8.1-sqlite3 php8.1-mysql \
php8.1-xdebug php8.1-mbstring php8.1-redis

# How to switch php versions
sudo update-alternatives --config php

# How to disable/enable xdebug
sudo phpdismod xdebug
sudo phpenmod xdebug


Node.js: NVM and NPM install

First, we need to install the Node version manager package.

Check the current LTS version here https://nodejs.org/en/

#!/usr/bin/env bash

# Installing Node 16:
nvm i 16

node -v
# v16.15.0

npm -v
# 8.5.5

# Switching Node.js versions
nvm i 18
# or (if already installed)
nvm use 18

In the next part, we will install all required services including Docker, and start commonly used development services like MySQL and Redis databases.


Docker

Just follow the guides below until you can successfully run a hello-world container.

https://docs.docker.com/engine/install/ubuntu/

https://docs.docker.com/engine/install/linux-postinstall/

MySQL database

We will launch a docker instance of MySQL database.

Visit https://hub.docker.com/_/mysql for more info.

#!/usr/bin/env bash

# first run
docker run -d --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=secret \
-e MYSQL_DATABASE=laravel  -e MYSQL_USER=laravel -e MYSQL_PASSWORD=secret \
-v ~/mysql-data:/var/lib/mysql mysql:8

# stop service
docker stop mysql

# start service
docker start mysql

When using the DatabaseMigrations trait in testing and a schema:dump command, you would need to install MySQL tools so Laravel can run them locally.

sudo apt install mysql-client-core-8.0

Redis database

Visit https://hub.docker.com/_/redis for information on the Redis container.

#!/usr/bin/env bash

# first run redis with persistence
docker run -p 6379:6379 --name redis -d redis --save 60 1 --loglevel warning

# stop service
docker stop redis

# start service
docker start redis


Below are my recommended apps for managing DBs in Ubuntu. Both tools have SSH tunneling for Production networks behind firewalls.

MySQL Workbench

Visit https://dev.mysql.com/downloads/workbench/ to download your version or snap:

sudo snap install mysql-workbench-community

sudo snap connect mysql-workbench-community:password-manager-service :password-manager-service

Redis Desktop Manager

Visit https://resp.app/ for more info.

sudo snap install redis-desktop-manager


PhpStorm

Finally, we finish by installing a fresh copy of PhpStorm.

sudo snap install phpstorm — classic

or https://www.jetbrains.com/help/phpstorm/installation-guide.html#snap


In the next chapter, we are going to review the list of recommended plugins for PhpStorm that boost your Laravel and overall productivity.

Also Published here


Written by dkhorev | Sharing my ideas on software development (NestJS, Laravel, System Design, DevOps for backend engineers)
Published by HackerNoon on 2022/09/05