in Code, Tutorials

Creating a Vagrant Box for Strongloop Development with Ubuntu 16.04 (Xenial)

FacebookTwitterRedditEmail

So I’m working on a mobile app development project which is using Loopback for REST API generation. I wanted to set up a Vagrant box with the following attributes:

  • Use VirtualBox
  • Ubuntu Server 16.04 (Xenial Xerus) x64
  • Latest NodeJs (currently 6.7.0)
  • Strongloop (npm package that installs CLI for working with loopback)

This wasn’t as straightforward as I expected. Here are the steps I took to get it up and running.

Step 1: Install latest VirtualBox and Vagrant

First, grab the latest version of VirtualBox. If you have an older version installed, you can just install the new one using the downloadable installer and it will overwrite the previous version.

Next, grab the latest version of Vagrant. Again, you can just run the installer to overwrite any previously installed version with the latest one.

Step 2: Create a new directory for the box

Personally, I have a dev directory in my home folder where I keep all of my projects, so for me:

Step 3: Install and configure the latest Ubuntu

The default install of the Ubuntu Vagrant box doesn’t have enough memory allocated for strongloop to install properly. Here are the steps I had to follow. First, initialize a new box:

Next, in your favorite text editor, you need to update the Vagrantfile (which was created for you in your current folder) so that it has enough memory. In my case, I also configured a “private network” IP address. I’ll explain why I did that later. Here’s what my Vagrantfile looks like with everything but the necessary bits removed:

Once you save this file, you can go back to the terminal and fire up your box:

NOTE: I found that on one of my machines I had trouble with this command. It appears that for some systems the version of curl that ships with Vagrant doesn’t work. Follow the instructions on this thread to figure out how to replace it with one that works!

Once the box has started, SSH into it:

Step 4: Install latest NodeJs

I found this handy tutorial at DigitalOcean which described how to do this. Here are the commands:

Step 5: Install Python2.7 and configure npm to use it

Ubuntu 16.04 comes with Python3 installed but apparently, npm uses the node-gyp package which needs Python2.7.

Step 6: Change owner of /usr/local and set npm prefix

In order for npm to be able to install everything in /usr/local you need to change its ownership to the default user (not root). You also need to make sure that npm is configured to install packages to /usr/local which, I was surprised to find, was not the default. (In my case the default prefix was /usr.)

Step 7: Install Strongloop

Finally, we can install Strongloop:

Assuming everything went smoothly, you can test to make sure that Strongloop was installed by typing slc -v at the console. It should print out the version of Strongloop/API Connect that is installed.

Additional Configuration Steps

There are a couple more things you may want to do if you’re setting this up as part of your dev environment.

Associate A Domain Name

I like to create a domain name, e.g. my.supercoolapi.com, and associate it with the IP address I set up in the “private network” setting in the Vagrantfile. That way, I can test out the API in a web browser just by typing in the domain name.

To do this, in a text editor on your host machine, open up the hosts file. On Windows machines, this is located at c:\Windows\System32\drivers\etc\hosts and on OSX/Linux it’s at /etc/hosts. Add a line at the bottom of the file that looks like:

You can substitute in any domain name that you would like, but be sure that:

  1. The IP address is the same one you set up in the Vagrantfile, and
  2. The domain name you choose is not the same as an actual website that you actually visit, e.g. google.com, because you won’t be able to get to that site anymore.

Sync Dev Directory from the Host to the VirtualBox

Since API development is a lot easier using a GUI editor (I like Sublime Text), it would be nice to be able to edit my loopback API in my host OS and have those changes synced with the files on my virtual machine. This is as simple as adding one line to your Vagrantfile that looks like:

You can use absolute or relative paths. It’s easiest to map your code into a subdirectory of the home directory of the virtual machine. The path on the host OS must exist before you start up your VM. It doesn’t matter if the path on the VM exists, and you can create it after you load your VM. In my case, my final Vagrantfile looked like:

To do this, I had to run mkdir api in the same directory as my Vagrantfile before reloading my virtual machine. It also meant that when I generated my loopback API on the VM (using slc loopback), I needed to do so from the home folder and using the directory name api.

Run loopback through port 80

After you’ve generated your loopback API app, you can go to ~/api/server/config.json and change the port number to 80. In order to start up the server you’ll have to use sudo, so from the root of your API (i.e. ~/api) run sudo node ..

Conclusion

I really love building API’s with loopback. The whole process runs super smoothly and I like being able to develop the REST API in Javascript just like the rest of my apps. One thing I didn’t do in this tutorial was walk you through how to install a database on the VM, but there are plenty of tutorials for that out there already.

FacebookTwitterRedditEmail

Write a Comment

Comment

  1. Hey Dr. Benton,

    I encountered an error when following your tutorial during the ‘npm install -g strongloop’ step. The error was about a read-only filesystem, and the solution was to stop/destroy the vagrant machine I had previously made, install VirtualBox Extension Pack, and then follow the steps to create/init the vagrant box and re-install everything.

    Just for reference, I’m running Linux Mint 18 (Debian-based, so I assume the logic is the same for Ubuntu hosts).

    Thanks!