Search code examples
phplaravelvagranthomesteadphp-extension

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?


Solution

  • 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):

    #!/bin/sh
    
    # If you would like to do some extra provisioning you may
    # add any commands you wish to this file and they will
    # be run after the Homestead machine is provisioned.
    
    # in the below --assume-yes is to avoid confirms [y/N]
    # DEBIAN_FRONTEND=noninteractive is to avoid a big menu asking if it's ok to 
    # overwrite the php.ini file, may make --assume-yes redundant, not sure
    
    # run apt-get update first, without it I was getting errors not finding the extensions 
    sudo DEBIAN_FRONTEND=noninteractive apt-get --assume-yes update
    
    # load any extensions you like here 
    sudo DEBIAN_FRONTEND=noninteractive apt-get --assume-yes install php-xdebug 
    sudo DEBIAN_FRONTEND=noninteractive apt-get --assume-yes install php7.0-ldap # update to php7.2-ldap if using php 7.2 etc...
    
    # enable xdebug via cli
    sudo phpenmod -s cli xdebug
    
    # restart php and nginx
    sudo service php7.3-fpm restart && sudo service nginx restart
    

    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):

    DoDSoftware:Homestead DOoDSoftware$ vagrant ssh
    Welcome to Ubuntu 16.04 LTS (GNU/Linux 4.4.0-22-generic x86_64)
    
     * Documentation:  https://help.ubuntu.com/
    vagrant@homestead:~$ php -r "print_r(get_loaded_extensions());"
    Array
    (
        [0] => Core
        [1] => date
        [2] => libxml
        [3] => openssl
        [4] => pcre
        [5] => zlib
        [6] => filter
        [7] => hash
        [8] => pcntl
        [9] => Reflection
        [10] => SPL
        [11] => session
        [12] => standard
        [13] => mysqlnd
        [14] => PDO
        [15] => xml
        [16] => apcu
        [17] => apc
        [18] => bcmath
        [19] => calendar
        [20] => ctype
        [21] => curl
        [22] => dom
        [23] => mbstring
        [24] => fileinfo
        [25] => ftp
        [26] => gd
        [27] => gettext
        [28] => iconv
        [29] => igbinary
        [30] => imap
        [31] => intl
        [32] => json
        [33] => ldap
        [34] => exif
        [35] => mcrypt
        [36] => msgpack
        [37] => mysqli
        [38] => pdo_mysql
        [39] => pdo_pgsql
        [40] => pdo_sqlite
        [41] => pgsql
        [42] => Phar
        [43] => posix
        [44] => readline
        [45] => shmop
        [46] => SimpleXML
        [47] => soap
        [48] => sockets
        [49] => sqlite3
        [50] => sysvmsg
        [51] => sysvsem
        [52] => sysvshm
        [53] => tokenizer
        [54] => wddx
        [55] => xmlreader
        [56] => xmlwriter
        [57] => xsl
        [58] => zip
        [59] => memcached
        [60] => blackfire
        [61] => Zend OPcache
        [62] => xdebug
    )
    

    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 :)