How to install laravel homestead windows 10. Before my close acquaintance with vagrant and virtual machines, installing laravel homestead on windows 10 led to a time-consuming and complex process, looking for solutions to errors that rained down throughout the installation. However, having dealt with this in more detail, and having practiced several times, I realized that working with vagrant is not so difficult, and there are a lot of advantages. I understand the pain of PHP programmers working on windows. Therefore, in our case, the creation of a virtual machine is simply vital.
First of all, I want to say that not everyone needs this article. For standard solutions, for developers creating landing pages and business card sites, a virtual machine is not needed at all. Installing a virtual OS, using Vagrant, Docker is already the next step in development, which makes it easier to create complex products that are not limited to one programming language.
I am writing this article to illustrate the process of installing and running a laravel project using vagrant and VirtualBox. Well, enough words, let’s do it.
Official documentation for homestead. I advise you to familiarize yourself with what this assembly is, what it contains inside.
In order to understand the installation issue, I advise you to type all the commands manually and look at the result. There is no sense in copy-paste or thoughtless printing. Are you here for knowledge?
First of all, you need to pre-configure the system. In order for the virtual box to work correctly, you need to enable hardware virtualization.
A common problem with disabled virtualization (64-bit system) is the lack of selection of x64 operating systems (when trying to create a virtual machine manually)
Now that the preparatory procedures are completed, install:
VirtualBox and vagrant
After downloading, you will need to install VirtualBox, and then Vagrant. You may need to restart your computer and you can proceed to the next step.
You will need git to easily work with the repositories. Therefore, in addition, you need to download git bash from the link and install it.
In this step, the general settings have already been completed, now we need to download the homestead.
To do this, you need to run git bash as an administrator, and run the command:
vagrant box add laravel/homestead
And we need to choose which virtualization program we will use (in this case, VirtualBox, we indicate 3 )
After a long download and installation process, we should receive a message in the console that the box has been successfully added, and, accordingly, is ready to run.
If you get an error while trying to execute the command, try installing / updating MS Visual C ++ 2010
After the installation is complete, you need to select the directory in which the core of our project will be stored (vagrant configs, our laravel project, etc.), and go to it.
For myself, I decided that I would place the current project in the folder of the current userC:\Users\Fomenko Alexandr
In git bash-e, I change to the desired directory cd C:\Users\Fomenko Alexandr
And I execute the command:
git clone https://github.com/laravel/homestead.git Homestead
This command clones the Homestead repository into our folder
And, as indicated in the documentation, we will execute 2 commands.
The first one goes to the newly created Homestead folder.
And the second command, which looks for the init.sh file in this folder, and executes it. And he already automatically initializes the project and creates configuration files
It should look like this:
As a result, this command had to create a configuration file Homestead.yaml, which we will consider below
In this step, you need to create ssh keys. In order to check their presence, you need to go to folder C:\Users\YOUR_USER_NAME and check the existence of the folder .ssh, which should contain 2 files – id_rsa and id_rsa.pub.
In fact, you do not need to check manually, but simply execute the command to create a new key
ssh-keygen -t RSA -C “[email protected]”
AND, if the keys were created earlier, you will be prompted to overwrite them.
When creating a key, you will be asked several questions (in the console), you just need to press enter.
This stage is the final one for pre-setting the project. Now it remains to configure the options for the created virtual machine.
All settings are in the Homestead.yaml file , which we previously generated automatically in step 5.
By opening this file in an editor, you can see its standard appearance:
– map: ~/code
– map: homestead.test
# – id: foo
# token: bar
# client-id: foo
# client-token: bar
# – send: 50000
# to: 5000
# – send: 7777
# to: 777
# protocol: udp
Now, step by step, we will configure this file. And I will explain what each of the editable lines means.
At the top of the file, this block is striking:
The first line specifying ip- indicates which ip address we need to listen to. This is optional, you can specify any. I’ll leave it as it is now.
memory– sets the maximum amount of RAM the virtual machine can use (in this case – 2048 MB).
cpus- the number of cores used,
and provider– obviously, unchanged, virtualbox
This block contains the paths to the previously created ssh keys.
We need to provide the full path to the key files. As a result, this block will look like:
Instead, USER_NAMEenter your user name
The following code “forwards” our windows 10 folder to the local machine:
– map: ~/code
Given that when we change these files, similar files of the virtual machine will automatically change.
map– means which folder to throw-in
to– on which path it will be available in the virtual machine
Knowing this, let’s rewrite the config:
– map: C:\OSPanel\domains\new-laravel.test
And now, when we launch the virtual machine, both the virtual system and the local one will refer to the same folder. Therefore, the changes will be bidirectional, and the changes will be applied immediately.
I have forwarded an empty folder ( C:\OSPanel\domains\new-laravel.test), which will be available in the virtual machine by the address/home/vagrant/code
The next block helps us configure nginx. Here we indicate which domain corresponds to which folder:
– map: homestead.test
Not to be confused with the previous point, they have nothing to do with each other. It just coincides so that we enter there the folder that we threw earlier. However, we can enter any folder that exists on the virtual machine.
We haven’t created /home/vagrant/code/Laravel/public it yet, however, we’ll fix it shortly. And, in view of the fact that laravel names the public folder public, this is just a template for redirecting to a single entry point public / index.php.
In the future, this site will grow, and will contain several sites at the same time:
In our case, while everything is simple, we only need one site:
– map: new-laravel.test
The last important parameter is the databases being created. Here we specify which databases to create when initializing the virtual machine:
I will create under the name new-laravel
Hooray! The setup is complete. Nearly…
As planned, in Homestead.yaml we specified the domain under which our site will be available. However, our computer does not know anything about this domain yet. And, trying to open this address in the browser, we get an error. And in order for him to learn about this domain, we did some preparatory operations earlier.
At the beginning of step 7, we indicated IP. And you could set it by anyone because you have to manually register it in the host’s file (C: \ Windows \ System32 \ drivers \ etc \ hosts). In my case, I have to prescribe 192.168.10.10 new-laravel.test. This means that in case of an attempt to go to the address new-laravel.test, the responsibility for displaying it will be transferred to our local machine with IP 192.168.10.10.
When adding multiple sites, the hosts file will look like this:
And now, all the necessary settings have been made. And now you can start the virtual machine initialization process.
To do this, in the directory where Homestead.yaml the command lies vagrant up. And, after a while, your virtual machine will work and the world will become one programmer happier.
Also, it is worth remembering one more useful command, which, in the event of a configuration change, re-creates the virtual machine:
And, in order to connect from the console to the virtual machine itself:
And all commands will be executed inside the virtual machine
We now have a fully operational unit. And when we connect via ssh vagrant ssh, going to the project folder: cd /home/vagrant/code
Run the command composer create-project –prefer-dist laravel/laravel Laravel
And now, by going to the folder on your computer, you can see the new Laravel folder, which we just created from the virtual machine, and in which, now there is a new laravel project.
This is one of the use cases for homestead-a when there is one system, and all our projects are thrown into it. And this option is an excellent replacement for OpenServer, only with more features.
Also, a common practice is to create a separate virtual machine with its own configuration for each of the projects. But this is another topic beyond the scope of this article.