Download and install Vagrant
Scroll down, download the version for macOS.
Run the package installer
Download and install VirtualBox
Vagrant cannot run without VirtualBox
Download the version for OSX, Run the package installer
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)
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.
and run this command to initialise Homestead
You should see a response like this
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]-----+
Now navigate back one step so that you are inside Documents/laravel folder.
And run this command to create a folder named code. This folder will hold your projects.
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
Save the file.
Start Homestead machine
Go back to your terminal and run this command. This will get the vagrant machine up and running.
If you make any changes to the Homestead.yaml file, you can restart the server by calling these commands
Alternatively, this one command also suffices to reprovision your server:
vagrant up --provision
Now run this command to ssh into the machine. This uses the key that we generated to login into the machine.
This should bring up the command line.
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
Install Laravel in Homestead
Make sure you are still logged in to the vagrant machine.
Navigate into the code directory
Following the steps from Laravel installation page now,
First, download the Laravel installer using Composer:
composer global require laravel/installer
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,
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.
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,
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.
Open your browser and open the website address, postfixed with :8000
You should see a basic LARAVEL website.
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.
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
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.