How to automatically enable php extensions in Homestead on vagrant up

10,405

Solution 1

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

Solution 2

In case there's still a need for this :

=> https://guides.wp-bullet.com/install-apcu-object-cache-for-php7-for-wordpress-ubuntu-16-04/

=> Run the 3 first commands :

sudo apt-get update
sudo apt-get install php7.0-apcu -y
sudo service php7.0-fpm restart

Or simply add to after.sh:

sudo apt-get install php7.x-apcu -y
Share:
10,405
Wesley Smith
Author by

Wesley Smith

I enjoy working on PHP applications created with MySQL, Laravel, and Vue.js for fun or profit. Mostly, I love to build things that solve problems

Updated on June 17, 2022

Comments

  • Wesley Smith
    Wesley Smith almost 2 years

    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?

  • Wesley Smith
    Wesley Smith over 7 years
    Thanks for your time. Im aware of how to enable the extension manually. However, I dont want to have to do this every time I do vagrant up. I need to be able to automate adding the extension as part of the provisioning process so that it's all set up properly when doing vagrant up --provision
  • Sygmoral
    Sygmoral about 5 years
    Great, thanks for this! The only thing I changed was use vagrant reload --provision instead of destroy and up, to avoid losing my databases.
  • Wesley Smith
    Wesley Smith almost 5 years
    While accurate, this kind of misses the mark. The original need was specifically to automatically enable php extensions in Homestead on vagrant up. while this is only manually installing the same.
  • befabry
    befabry almost 5 years
    True. I understood it as "Make it available when I launch the vagrant". Not as "Get it ready when installing".