Skip to content
Advertisement

How to automatically enable php extensions in Homestead on vagrant up

Im using Laravel 5.3 in Homestead with Vagrant 1.8.7 running on VirtualBox.

I have need to enable some php extensions.

I know that I could ssh into the box and edit the php.ini to enable the extension but this seems like a very anti-vagrant way to do this.

I want to tell Vagrant to provision the box with specific php extensions enabled so that I can simply call vagrant up --provision and the box will be ready to go (kinda the point of vagrant right?)

So, How can we automatically enable php extensions in Homestead on vagrant up?

Advertisement

Answer

After some tinkering, the below is what I came up with. I make no assurances that this is the right way to do it only that, in my case, it seems to be working:

Find the after.sh that was generated when you installed homestead. For me, on Mac El Capitain, the file is created at ~/.homestead/after.sh, I imagine there is a .bat in a similar location on windows.

Do not make the mistake of editing ~/Homestead/src/stubs/after.sh, thats the template file from the homestead installation, not your actual generated copy.


Edit after.sh

Add the below lines to after.sh (this is my whole file, only the first 5 comment lines were in the default file):

JavaScript

If you dont psychically know the exact name for the extension you need (I didnt) you can use sudo apt-cache search php7-* or similar to list the available ones


vagrant destroy

Now, if you have homestead up, in the terminal, cd to your Homestead dir, for me cd ~/Homestead and then run vagrant destroy


vagrant up

While inside /Homestead run vagrant up --provision


Check install

To check that the extensions installed correctly, while inside /Homestead run these two commands:

vagrant ssh

php -r "print_r(get_loaded_extensions());"

My output (33 and 61 were added):

JavaScript

Like I stated at the beginning, I cant say this is the right way, but it’s working for me so far.

If anyone sees a flaw in this approach, feel free to tell me Im doing it all wrong 🙂

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement