Installing the d8 JavaScript shell on Linux

Note: This article was written several years ago. It describes an installation process that may not work on your system today. If you run into trouble, please follow Google's instructions instead:

Google Developers: How to Download and Build V8

Get your daily vitamins with d8

If you're a full-stack JavaScript developer, you'll probably want to install the d8 shell. There are other ways to run JavaScript in a terminal, notably Node.js, but the simplest way is to execute your JavaScript program with d8.

What exactly is d8? It's a command-line environment that is packaged with the V8 JavaScript engine. The official instructions for setting up V8 are on the following pages.

Download V8 using Git

Build V8 with GYP

It can be difficult to sort out the various options mentioned in the documentation. I have successfully installed and executed d8 on Ubuntu versions 12.04 and 14.04 by running the following sequence of commands in a terminal.

$ sudo apt-get install git git-svn subversion
$ mkdir ~/js
$ cd ~/js
$ git clone git:// v8
$ cd v8
$ make dependencies
$ make native console=readline
$ sudo ln -s ~/js/v8/out/native/d8 /usr/bin
$ source ~/.bashrc
$ d8

What does it all mean?

Let's go through it step by step.

First, we have to install software that will download V8 packages from the appropriate repositories. The sudo command will probably prompt you for your password.

sudo apt-get install git git-svn subversion

Now let's make a directory called js in the home directory. We'll install V8 inside this directory.

mkdir ~/js

Go to the directory where we're going to install V8.

cd ~/js

Use git to download the latest stable V8 release into a new directory called v8.

git clone git:// v8

Go to the directory containing the V8 source code.

cd v8

Invoke a script that sets up the compilation environment.

make dependencies

Compile the various components of V8, including the d8 shell. The compilation process takes a long time. You can speed it up with parallel processing: add the option -j2 if you have a dual-core CPU, -j4 if you have a quad-core CPU, and so on.

make native console=readline

The compilation process is done. The d8 shell is ready to execute, and its absolute path name is ~/js/v8/out/native/d8. To avoid typing the whole path each time we want to run d8, let's add a symbolic link in the /usr/bin directory.

sudo ln -s ~/js/v8/out/native/d8 /usr/bin

If you're running the Bash shell, use the following line to refresh your configuration file so that you can immediately access d8 from anywhere. (If this doesn't work, open a new terminal window and you should now be able to use d8.)

source ~/.bashrc

You can run d8 in interactive mode to execute snippets of JavaScript.


Things that may not be obvious

What if you want to execute a longer program? Suppose the file a.js contains your program and you want to feed it the contents of in.0 as input. Execute the following.

d8 a.js < in.0

The following useful commands are built into d8.

effect: read a line from stdin
example: var line = readline();

effect: write the arguments to stdout, followed by a newline
example: print(1, "two", 3);

effect: read a text file into a string
example: var data = read('in.0');

effect: execute a JavaScript file
example: load('a.js');

A final command that may come in handy is quit(), which leaves the d8 environment. Alternatively, you can get out of the interactive loop by entering an EOF character with Ctrl-D.