Quick Tip: Forget setting up your dev environment use Vagrant/Docker

Quick Tip: Forget setting up your dev environment use Vagrant/Docker

If you are like me, while starting with PHP development you might have used something like a WAMP/XAMPP. Although it is good when you are starting out, but, it sets up things which in my opinion were non-standard.  I say non-standard because when I logged into a remote server, things were not in the usual place as *AMPP would have put it. 

Being unaware of the things like Vagrant/Docker, I decided that it would be wonderful if I can set up things the way they are set up on a remote server(Such an original thought!) and since then had been installing the stack manually. 

Then I got a chance to work on projects which used Vagrant and Docker but I never grew fond of it because of the following reasons: 

  1.  Vagrant: everything on my system literally came to a halt, not an issue with Vagrant and most probably because of the mediocrity of my computer’s hardware(what would you expect out of a Non-SSD, 8GB, i3 machine)
  2. Docker: There were so many things to learn and understand that I just stuck to what was already set up on the project and never took the time to understand and actually use it myself. Things were still a bit slow but still better than Vagrant. 

So while system limitations and learning curve were stopping me from using these tools, but the moment of transformation came when I was fed up of setting up everything, time and again(i.e when I upgraded system, a hard disk crash etc)

I needed an environment which was quick to set up and easy to use across different projects that I work on and even allow me to work on projects which use different sets of technologies and even different PHP versions.

Being a Laravel Developer a very easy choice is to use Vagrant and use the Homestead Box with it.

Homestead has every piece of software included that you would ever need being a Web developer. It allows you to use different versions of PHP, web servers and what not, you can checkout the full list here

Get started with setting up a PHP website using Vagrant and Homestead

  1. Download Vagrant(preferably latest version) for your OS and set it up. I have version 2.6 setup on Ubuntu Host.
  2. Download Virtual Box(latest version). I have version 6.0 which works well with 2.6 Vagrant. VBox 6.1 wasn’t working with 2.6 so I haven’t upgraded both Vagrant and VBox since then.
  3. In your command line(CMD or Terminal) run vagrant box add laravel/homestead
  4. In your projects directory(where you usually keep all your projects) clone the repository git clone https://github.com/laravel/homestead.git ~/Homestead
  5. cd ~/Homestead && git checkout release
  6. On Linux run bash init.sh (on Windows init.bat)
  7. Now configure your Homestead.yaml if you need to. Keeping the defaults I usually update following for setting up a local project
folders:
    - map: /path/to/project/projectname #change with path to your project
      to: /home/vagrant/projectname #replace projectname with your project like codisfy

sites:
    - map: projectname.test
      to: /home/vagrant/projectname/public
  1. Add your project name to /etc/hosts on Linux/Mac and to C:\Windows\System32\drivers\etc\hosts on Windows like

    192.168.10.10 projectname.test
  2. Now run vagrant up. It should set up your project. Once it finishes update your database settings, which would be
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
  1. Once you stop working you can close your box by vagrant halt

If you need to set up more projects just add more children to folders, databases and sites in Homestead.yaml and run vagrant up --provision or vagrant reload --provision.

Note: Before upgrading your vagrant box make sure to take a backup of things like DB, scripts, aliases and local files which are stored exclusively on the box. Ideally keep them outside of the box and mount them inside.

In 10 simple steps(and fewer for your next project) you can setup a project. This will not only increase your productivity but will also introduce you to working with remote servers, so when you login to your production environment for the first time you will feel a lot familiar.

In the next post I will talk about how to quickly get started with Docker even if you don’t know a lot about it. Docker is lightweight and would give better performance even if you don’t have high end computer configuration.

Leave a Reply

Your email address will not be published. Required fields are marked *