Installing Laravel Valet on MacOS

1. Install Xcode

Laravel Valet is a powerful development environment for MacOS that allows you to easily serve your Laravel sites. It configures your Mac to always run Nginx in the background when your machine starts, providing a blazing fast Laravel development experience. In this guide, we’ll walk through the steps to install Valet on your MacOS machine.

Install Xcode Via the command line:

xcode-select –install

Or via the Apple Store

Alternatively, you can install it via the Apple Store. For more information, refer to the documentation: https://mac.install.guide/commandlinetools/4.html

2. Generate and add an SSH Key

An SSH key is a secure way to authenticate with servers and services without having to provide your password each time. Here’s how to generate and add an SSH key:

Step-by-step:

ssh-keygen -t ed25519 -C "[email protected]"
pbcopy < ~/.ssh/id_ed25519.pub
touch ~/.ssh/config
nano ~/.ssh/config

Paste the following into the terminal:

Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

In nano, save the file with ctrl + o, then ctrl + x and enter to exit. Then, run:

eval "$(ssh-agent -s)"
ssh-add --apple-use-keychain ~/.ssh/id_ed25519

For more information, see GitHub’s guide on generating SSH keys: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

3. Install Homebrew

Homebrew is a package manager for MacOS that makes it easy to install various Unix tools and open-source software. Install it with the following command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Then, set up your shell to use Homebrew. Read the brew docs at: https://brew.sh/

touch ~/.zprofile
nano ~/.zprofile

Paste the following into the file, replacing “yourprofile” with your actual username:

(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/yourprofile/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
zsh

Brew will tell you exactly what commands need echoed in your zshell.

Exit nano and run:

zsh

Verify the installation with:

brew doctor

4. Installing PHP

Installing PHP via Homebrew: PHP 7.4 is depreciated, we will need to install a brew tap: https://github.com/shivammathur/homebrew-php

brew tap shivammathur/php

brew install [email protected]

All the additinal versions of php can be installed, so you can switch them as needed. The commands below is a handy way to see what versions are available:

brew search php
brew install [email protected]
brew install php

Verify your PHP installation with:

 php -v

5. Installing Composer (PHP Dependency Manager)

curl -sS https://getcomposer.org/installer | php

mkdir -p /usr/local/bin

sudo mv composer.phar /usr/local/bin/composer

chmod +x /usr/local/bin/composer

which composer

The last command should output:

/usr/local/bin/composer

6. Installing Laravel Valet

composer global require laravel/valet

valet install

mkdir valet && cd valet

valet park

If successful, you should see this output: This directory has been added to Valet’s paths.

Now we can add an unzipped WordPress install to that directory, and rename it to the TLD we want to use. For example, if we want to use the domain example.test, we would add the WordPress install to the valet directory, and rename it to example.

A domain extensions of .test will be added to the end automatically. So, if you want to use example.test, you would name the directory example.

7. MariaDB Server

Our SQL DB isn’t installed, so lets, do that next.

brew install mariadb

Starting the server: brew services start mariadb

Initial setup and securing the installation:

mysql_secure_installation

Verifying the installation: Connect using

mysql

With that command it will prompt you for your password, and you can use these commands to create datbaes and users:

create database DATABASE_NAME;

grant all privileges on DATABASE_NAME.* TO 'USER_NAME'@'localhost' identified by 'PASSWORD';

flush privileges;

exit;

8. NVM and Node.js

Node.js is a JavaScript runtime used for serving assets. NVM (Node Version Manager) allows you to easily install and switch between Node versions.

Installing NVM:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash

Replace v0.39.4 with the latest version from the NVM GitHub repository https://github.com/nvm-sh/nvm. Verifying the installation: command -v nvm

Installing Different Versions of Node.js Using NVM

Installation command:

nvm install --lts

nvm install (for a specific version, e.g., nvm install 14.16.0)

Switching between versions:

nvm use <version>

Setting a default version:

nvm alias default <version>

Verifying the installation:

node -v

9. Using Oh My ZSH

Oh My Zsh is a framework for managing your Zsh configuration that provides helpful functions, aliases, and themes for your command line.

Read the docs here: https://ohmyz.sh/#install

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

To edit your terminal theme, edit the .zshrc file in your home directory.

ZSH_THEME="xiong-chiamiov-plus"

Oh My Zsh has many other plugins and configuration options to streamline your command line workflow. Check out their documentation to learn more: https://ohmyz.sh/

Wrapping Up

You now have a fully functioning Laravel Valet development environment on your Mac! Place your Laravel projects in the ~/valet directory and they will be instantly accessible at http://projectname.test. For further customization options, check out the official Laravel Valet documentation: https://laravel.com/docs/valet

Some additional configuration you may want to set up includes:

  • Installing additional PHP extensions
  • Configuring a preferred code editor
  • Setting up additional Valet drivers for other frameworks

Happy coding!