Set up Laravel Homestead on Mac — Step by step

Zeba Rahman
fabcoding
Published in
5 min readApr 20, 2020

Step 1

Download and install Vagrant

Scroll down, download the version for macOS.

Run the package installer

Step 2

Download and install VirtualBox

Vagrant cannot run without VirtualBox

Download the version for OSX, Run the package installer

Step 3

Add Homestead to Vagrant Box

Open terminal and run the following command

vagrant box add laravel/homestead

You may get a response like this:

This box can work with multiple providers! ...
1) hyperv
2) parallels
3) virtualbox
4) vmware_desktop

We are using VirtualBox, so enter the option 3.

This will take quite a while. It may even take up to 2 hours.

Run this command to check if it has been successfully added to our box.

vagrant box list

You should see something like this

laravel/homestead (virtualbox, 9.4.0)

Step 4

Start installing Homestead

Now we need to download the laravel files into our computer. Choose a location where you would like to place it and modify the path in the code accordingly. For me, I have chosen a new folder named laravel inside my Documents folder.

git clone https://github.com/laravel/homestead.git ~/Documents/laravel/Homestead

After successful cloning, navigate inside this folder.

cd Documents/laravel/Homestead

and run this command to initialise Homestead

bash init.sh

You should see a response like this

Homestead initialized!

Step 5

Create Keys

Navigate to the Homestead folder that was created under Documents/laravel. Open the file named Homestead.yaml

You will see these lines in the file.

authorize: ~/.ssh/id_rsa.pub

keys:
- ~/.ssh/id_rsa

These are the keys that we need to create before we can get it to run.

So go back to your terminal and run this command to create it.

ssh-keygen -t rsa -C "you@homestead"

You will be prompted to set the location. Hit enter to let it remain in the default root location.

You will be again be prompted to enter a passphrase. Hit enter to leave it blank, as we are simply running on our computer and not on a server.

You will get a response like this

Your identification has been saved in /Users/technofreek/.ssh/id_rsa.
Your public key has been saved in /Users/technofreek/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxx you@homestead
The key's randomart image is:
+---[RSA 2048]----+
[image]
+----[SHA256]-----+

Step 6

Configure Homestead

Now navigate back one step so that you are inside Documents/laravel folder.

cd ..

And run this command to create a folder named code. This folder will hold your projects.

mkdir code

Now you should have this folder structure

Documents
- laravel
- code
- Homestead

Go back to your text editor or IDE where you opened the Homestead.yaml file. Locate the folders tree and modify it to map to the folder that you just created.

folders:
- map: ~/Documents/laravel/code
to: /home/vagrant/code

Also, let us add the names of our new project site and database to the respective places

sites:
- map: myproject1.test
to: /home/vagrant/code/myproject1/public

databases:
- myproject1

Save the file.

Step 7

Start Homestead machine

Go back to your terminal and run this command. This will get the vagrant machine up and running.

vagrant up

If you make any changes to the Homestead.yaml file, you can restart the server by calling these commands

vagrant halt

and then

vagrant up

Alternatively, this one command also suffices to reprovision your server:

vagrant provision

or

vagrant up --provision

Now run the following command to ssh into the machine. This uses the key that we generated to login into the machine.

vagrant ssh

This should bring up the command line.

vagrant@homestead:~$

Note: Enter exit command to logout from the machine.

Enter the command ls to check if our configuration was correct and you should see our newly created code directory listed there.

vagrant@homestead:~$ ls
code

Step 8

Install Laravel in Homestead

Make sure you are still logged in to the vagrant machine.

Navigate into the code directory

cd code

Following the steps from Laravel installation page now,

First, download the Laravel installer using Composer:

composer global require laravel/installer

Step 9

Create first project

Now the ‘new’ command will use this installer to create a new project with a fresh installation of laravel and all its dependencies.

So in the command line, still inside code folder,

vagrant@homestead:~/code$

Enter the command to create your first project

laravel new myproject1

This takes a little while. Once successfully completed, check the folder that was just created. As you can see, the entire project structure was generated.

Step 10

Configure the project

Now we configure this new project in our Homestead.yaml file.

Firstly, Add the names of the new project site and database to the respective places.

sites:
- map: myproject1.test
to: /home/vagrant/code/myproject1/public
databases:
- myproject1

Secondly, go to myproject1/.env file, in the code/myproject1 folder, and change the following values to set the database name and set the default username and password used by homestead.

DB_DATABASE=myproject1
DB_USERNAME=homestead
DB_PASSWORD=secret

Save the file. Your project is configured now.

But you cannot visit the website in your browser just yet. You need to map the hosts file so that the server recognizes the components,

Step 11

Map the hosts file

The domain that we have set which is ‘myproject.test’. In order to be able to open this in our browser we need to add it to the system hosts file.

Open a new window in your terminal, and type the following command.

sudo nano /etc/hosts

This will open up the hosts file in your terminal in edit mode. It will look like below code. At the end of the file, add a line with the local address and our project site domain that we have set.

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 myproject1.test

The last line is the one that we added.

Save and close the file. Ctrl+O -> Enter to save, and Ctrl+X to close.

Step 12

Run

Open your browser and open the website address, postfixed with :8000

http://myproject1.test:8000/

You should see a basic LARAVEL website.

Step 13

Setting up multiple projects/sites

First, (same as step 9) let us create the second project from laravel machine. Log in to it and navigate to code folder and simply create the new project. We’ll name it as proj2.

vagrant ssh
cd code
laravel new proj2

Second, for configuring the new project, (same as step 10) go to Homestead.yaml file and make new entries for sites and databases, and give the names.

sites:
- map: myproject1.test
to: /home/vagrant/code/myproject1/public
- map: proj2.demo
to: /home/vagrant/code/proj2/public
databases:
- myproject1
- proj2

And, go to proj2/.env file, which is inside code/proj2 folder and change the following values:

DB_DATABASE=proj2
DB_USERNAME=homestead
DB_PASSWORD=secret

Third, (same as in step 11), open a new terminal window, edit hosts file, and add an entry for the new site.

127.0.0.1       myproject1.test
127.0.0.1 proj2.demo

That’s all. Open your browser and try both URLs, they should work fine and the basic laravel site page should appear.

http://proj2.demo:8000/http://myproject1.test:8000/

But these look the same for now. Let us change the title for both websites.

Go to the code folder of the first project, in my case its Documents/laravel/code/myproject1.

Open the file

myproject1/resources/views/welcome_blade.php

Change the heading text in the body

<div class="title m-b-md">
My first project
</div>

Save and close. Run the reset provision command

vagrant up --provision

And refresh the websites. You should be able to see different headings.

Originally published at Fabcoding.

--

--