Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MikeCraig418/ab6f7a381103ed12cc774bcc11e4af49 to your computer and use it in GitHub Desktop.
Save MikeCraig418/ab6f7a381103ed12cc774bcc11e4af49 to your computer and use it in GitHub Desktop.

Installing v8 and v8js on Ubuntu 18.04.md

I completed the installation on September 19, 2019. I tried a few different tutorials and linux distros, ultimately I was successful on Ubuntu 18.04 from Digital Ocean.

The following guides got me started:

I was able to complete the install by taking a little bit from each tutorial. For the sake of brevity, I'll copy and paste below exactly what worked for me. A big thank you to the original authors!

Installing v8

Note: This portion was originally from the CentOS 7 tutorial, but it worked perfectly on Ubuntu 18.04. Additionally, the command /build/install-build-deps.sh does not work on CentOS anyways and will throw an error stating it will work on Ubuntu.

This will build the latest stable version as of this moment used by Chromium and Chrome (7.5.288.23). You can find the currently latest stable versions here, found in the column v8_version.

# Add depot_tools
cd /usr/local
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
export PATH=`pwd`/depot_tools:"$PATH"

# Build our v8 version
cd /usr/local/src
fetch v8
cd v8
git checkout 7.5.288.23 # v8_version here
gclient sync
./build/install-build-deps.sh
gn gen out.gn/library --args='use_custom_libcxx=false is_component_build=true is_debug=false target_cpu="x64" use_goma=false goma_dir="None" v8_enable_backtrace=true v8_enable_disassembler=true v8_enable_object_print=true v8_enable_verify_heap=true'
ninja -C out.gn/library libv8.so

# Move files we need to /opt/v8
sudo mkdir -p /opt/v8/{lib,include}
sudo cp -v out.gn/library/lib*.so out.gn/library/*_blob.bin out.gn/library/icudtl.dat /opt/v8/lib/
sudo cp -vR include/* /opt/v8/include/

# Update shared libraries when needed
sudo ldconfig

We're using gn gen for the configuration. Please refer to build documentation for more information about it or information about the available options.

Installing php-v8js

Now that v8 is installed, we can add v8js. Rather than compiling, I was able to successfully download it from PEAR.

# Install PEAR/PECL
apt-get install php-pear

# Install v8js
pecl install v8js # When prompted, enter the path "/opt/v8" without quotes

# If successful, you should receive a path to the compiled extension
# Mine was located in /usr/lib/php/20170718/v8js.so

Now find the location of your php.ini file by dropping <?php phpinfo; ?> into your index.php. Back

nano /etc/php/7.2/apache2/php.ini

# Using CTRL+W search for ".so"
# Then add the extension via 

extension=/usr/lib/php/20170718/v8js.so

# Close and save with CTRL+O, [Enter], CTRL+X

In your browser, open your script with <?php phpinfo; ?> and search for v8. You should see it!

Now test some simple JavaScript on the server side!

<?php

$v8 = new V8Js();
echo $v8->executeString('1 + 1'); // Should display (int) 2

?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment