Set up Laravel Homestead on Mac – Step by step

Step 1

Download and install Vagrant

https://www.vagrantup.com/downloads.html

Scroll down, download the version for macOS.

Run the package installer

 

Step 2

Download and install VirtualBox

Vagrant cannot run without VirtualBox

https://www.virtualbox.org/wiki/Downloads

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 this 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

The name ‘myproject1‘ is the same name that we added under ‘sites’ in the Homestead.yaml file, we will get to it in the next steps.

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.

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 10

Map the hosts file

Go back to your IDE / Text Editor and open up the Homestead.yaml file again.

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

Firstly, check the ‘to‘ path for our first project domain. Make sure that the project name is the same as the project we created above.

Now 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 11

Run

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

http://myproject1.test:8000/

You should see a basic LARAVEL website.

 

Step 12

Setting up multiple projects/sites

First, 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, 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

Third, same as in step 10, 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.

 


Also published on Medium.

By |2020-04-20T16:02:08+00:00April 20th, 2020|Categories: Laravel|0 Comments

Leave A Comment