The Hitchhiker’s Guide to Laravel Setup
Tags: laravel, programming
Published: 2023-06-03 18:37:44

This is an almost straightforward guide to setting up [Laravel](https://laravel.com/) on your machine. Laravel, if you didn’t already know, is a PHP framework for building robust web applications in a more expressive and structured way. Feel free to check the docs on Laravel’s [website](https://laravel.com/docs/6.x).

Most of the procedure here would be familiar to developers who, at one point or another, fiddled with the CLI. To follow along, you should have worked with either a Terminal (Unix) or Command Line (on Windows).

---

## Getting Started (PHP 7.3)

To get started with Laravel, PHP is the first and most obvious thing you’d need to have installed. Since Laravel is a PHP Framework, it doesn’t work without it.

## Windows

To get PHP installed and set up on your Windows machine, you could:

- Decide to go with some excellent all-in-one solutions that contain **Apache, PHP, MySQL**, and other applications in a single installation file, e.g. [XAMPP](http://www.apachefriends.org/en/xampp.html).

- Or, use the official installer available from [php.net](http://www.php.net/downloads.php).

If you’re new to web development, I would recommend the first option as it’s straightforward and gets everything set up for you (including annoying environment variables).

## macOS

For users of the latest macOS versions, You should already have PHP installed by default. You can verify this by opening your terminal and typing:$ `php -v`.

Chances are the version of PHP you’ll have will be somewhere around 5x, but the latest version of PHP available for installation is 7.3 (at the time of this post).

**PHP 7.4** is scheduled to be released on November 28, 2019. It’s the next PHP 7 minor release and should once again boost performance and improve code readability/maintainability.

To install PHP 7.3, open the terminal and run these commands:

```bash
curl -s http://php-osx.liip.ch/install.sh | bash -s 7.3
```

```bash
export PATH=/usr/local/php5/bin:$PATH$
```

```bash
php -v
```

## Install with brew

You can also install PHP with Homebrew. [Homebrew](https://brew.sh/) is a package manager for macOS. First, uninstall any existing version of PHP.

```bash
brew remove — force — ignore-dependencies httpd
brew remove — force — ignore-dependencies php70-xdebug php71-xdebug
brew remove — force — ignore-dependencies php70-Imagick php71-Imagick
brew remove — ignore-dependencies — force php70 php71 php72
```

Then, install `php@7.2` and upgrade to 7.3.

```bash
brew install php php@7.2
```

Once that’s completed, you can upgrade with

```bash
brew upgrade php@7.2brew upgrade php
```

---

## Linux
​
For Linux users, your package manager would handle most of the package installation. For example, apt is the default package manager for managing Linux packages in Ubuntu. To get started with installing PHP on Ubuntu, first update your Ubuntu OS packages to the latest version using

```bash
sudo apt update && sudo apt upgrade
```

The next step is to install a web server. Depending on your preference, you can choose either Apache or Nginx. They work pretty much the same way, but the configuration differs. To install the Apache web server, run the following command from your terminal:

```bash
sudo apt install apache2
```

You can then start the Apache2 server:

```bash
systemctl start apache2
```

For your convenience, you can enable autostart for Apache, so every time your machine is restarted, Apache automatically starts:

```bash
systemctl enable apache2
```

You’ll need to add a PPA to have php7.3 in your repository:

```bash
sudo add-apt-repository ppa:ondrej/php && sudo apt update
```

Once your packages have been updated, you can install PHP:

```bash
sudo apt install php7.3 php7.3-cli php7.3-common
```

If you need to install PHP extensions, you can also get them using the apt package manager:

```bash
sudo apt install php-pear php7.3-curl php7.3-dev php7.3-gd php7.3-mbstring php7.3-zip php7.3-mysql php7.3-xml php7.3-fpm libapache2-mod-php7.3 php7.3-imagick php7.3-recode php7.3-tidy php7.3-xmlrpc php7.3-intl
```

---

## PHP Extensions on macOS
Homebrew has officially dropped support for PHP extensions. However, extensions can still be installed with `pecl`.

Here’s a list of extensions you’ll want to install for Laravel Development.

- BCMath PHP Extension
- C Type PHP Extension
- JSON PHP Extension
- Mbstring PHP Extension
- OpenSSL PHP Extension
- PDO PHP Extension
- Tokenizer PHP Extension
- XML PHP Extension

Using Perl:

```bash
pecl install extension_name
```

---

## Installing Composer

Like most PHP Frameworks, Laravel also uses Composer to manage its dependencies. To Install Composer, you can run these commands accordingly in your terminal:

```bash
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
```

```bash
php -r "if (hash_file('sha384', 'composer-setup.php') === 'a5c698ffe4b8e849a443b120cd5ba38043260d5c4023dbf93e1558871f1f07f58274fc6f4c93bcfd858c6bd0775cd8d1') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
```

```bash
php composer-setup.php
```

```bash
php -r "unlink('composer-setup.php');"
```

The four lines above will:

- Download the installer to the current directory.

- Verify the installer SHA-384, which you can also [cross-check here](https://composer.github.io/pubkeys.html).

- Run the installer.

- Remove the installer.

If the installation was successful, you should be able to type `composer` in the terminal and see the currently installed composer version and command options.


![Composer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3sflt7745trz2kf45b5f.png)

---

## Installing Laravel

With composer installed, we can proceed to install Laravel like so:

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

Once the installation is complete, you’ll need to confirm that you have your composer system-wide vendor bin directory in your PATH. This is so `laravel` can be accessible as an internal command from the terminal.

You’ll likely find the vendor directory here:

- macOS and GNU / Linux Distributions: `$HOME/.composer/vendor/bin`

- Windows: `%USERPROFILE%\AppData\Roaming\Composer\vendor\bin`

Once you find the composer vendor bin directory, you can add this to your path by opening up **~/.bashrc** or **~/.bash_profile** in a text editor (or from the terminal with `vim` or `nano`) for macOS users. Also, if you use a Unix shell extension like Zsh, you can open up ~/.zshrc and add the line below:

```bash
export PATH="$PATH:$HOME/.composer/vendor/bin"
```

Close the file and run the below command in your terminal so the changes will take effect:

```bash
source ~/.bashrcsource ~/.bash_profilesource ~/.zshrc
```

Once that’s done, you should have access to the Laravel command on the terminal:


![Laravel command line](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wpqo4due4y5qr9455y32.png)


At this point, you can heave a sigh of relief. We’re pretty much done setting up Laravel on our machine and can jump into building Laravel applications.

---

​
## Getting Started With Laravel
With the Laravel CLI, scaffolding a new Laravel application has never been easier. From the terminal, to create a new application:
​
```bash
laravel new application_name
```


![terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1nvnavxcz8usgpmk1xye.png)

You should see this in your terminal once the new application has been generated successfully. You can then go into the application root folder and start the application:
​
```bash
cd application_name
```
​
Run the application with:

```bash
php artisan serve
```

Laravel development server started: [http://127.0.0.1:8000](http://127.0.0.1:8000)

You can open the URL ([http://127.0.0.1:8000](http://127.0.0.1:8000)) in your browser to see your new Laravel application.

## Database

Laravel works flawlessly with Relational Databases, including PostgreSQL and MySQL. If you don’t already have MySQL installed on your local machine, here’s the official documentation on how to get MySQL installed:

- For Unix / Linux: https://dev.mysql.com/doc/refman/8.0/en/binary-installation.html

- For Windows: https://dev.mysql.com/doc/refman/8.0/en/windows-installation.html

- For macOS: https://dev.mysql.com/doc/refman/8.0/en/osx-installation.htmll

Once you install MySQL, you can install a database client to manage your databases and tables. I’d recommend [Sequel Pro](https://www.sequelpro.com/) for macOS users or [MySQL Workbench](https://www.mysql.com/products/workbench/), which works for multiple platforms.

You can create a new database from your MySQL Database client and give it a catchy name if you want.


![Database folder image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p91kb4g98nahgegdri3i.png)

You can then configure your Laravel project to use the new database you created. Open your application folder in any code editor and edit the **.env file**:

```php
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=new_database
DB_USERNAME=grimebuttom
DB_PASSWORD=foolstark
```

Once that’s set, you can run a migration successfully:

```bash
php artisan migrate:fresh
```

If you followed through, I’d expect you to have a working setup of Laravel on your machine.

Go build something amazing!

Pssst! If you’re not sure where to go next, you can try creating an actual [Laravel application](https://devjavu.space/post/build-a-simple-blog-multiple-image-upload-with-laravel-vue/).

---

Cheers ☕️