Installation of a Vagrant machine

Installation of a Vagrant machine

A Vagrant machine is a file that you can download and install on your machine, and it gives you access to a complete Ubuntu computer in a Mac or Windows environment. In the following, the prompt Terminal> indicates a Unix terminal window on Mac or a Command Prompt window on Windows (called Ledetekst in Norwegian). The prompt Machine> indicates a terminal window where Ubuntu is running (in a Vagrant machine).

Installation instructions

Step 1. Download and install VirtualBox. Choose the version according to the operating system on the host. For example, if you want to build or run Vagrant machines under Mac OS X, choose VirtualBox x.y.z for OS X hosts, where x.y.z is the version number of VirtualBox. Double click the downloaded .dmg file to install Vagrant. Those who work on a Windows machines will select VirtualBox x.y.z for Windows hosts, which downloads an .exe file which can just be double clicked to perform the installation.

Step 2. Download and install Vagrant. Choose the latest version and the installation file corresponding to the host's operating system (where you installed VirtualBox). On a Mac, you select the Vagrant-x.y.z.dmg file (x.y.z denotes the version of the software), on Windows the Vagrant_x.y.z.msi file is the relevant choice. On Ubuntu, select vagrant_x.y.z_*.deb and install it by sudo dpkg -i vagrant_x.y.z_*.deb.

Step 3 for Windows users. If you have a Windows machine, you should install Cygwin. Download the Cygwin's setup.exe file and follow the instructions given by the installer. Make sure you manually select the 'X11' category during installation. Cygwin is not needed on Mac computers.

Step 4. Start X11: run Applications - Utilities - X11 on a Mac, or Start - All Programs - Cygwin-X - XWin Server on Windows.

Step 5. Move to your home directory and make a new directory vagrant and a subdirectory projects:

Terminal> cd
Terminal> mkdir vagrant
Terminal> mkdir vagrant/projects
Terminal> cd vagrant

All files that you run from the Vagrant machine are supposed to reside in vagrant/projects and its subdirectories.

Step 6. Download the file from http://goo.gl/ta977B and store it as inf5620.box in the vagrant directory. The file is big, 3.8 Gb, and may take hours to download! Make sure you have a stable Internet connection and that you do not bring your computer to sleep before the complete file is downloaded.

Step 7. Make sure you stand in the vagrant directory. Run

Terminal> vagrant box add inf5620 inf5620.box
Terminal> vagrant init inf5620
Terminal> vagrant up
Terminal> vagrant ssh

You are now inside a full-blown Ubuntu system with all software you need for the course.

Step 8. Open a file vagrant/projects/test1.py in an editor on the host system and write the following lines in the file:

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 3, 11)
y = np.exp(-x)
plt.plot(x, y)
plt.show()

Save the file. Move to the terminal window with the Ubuntu (Vagrant) machine. Run

Machine> cd /vagrant/projects
Machine> python test1.py

You should see a plot of \( e^{-x} \) on the screen. If you encounter any problems, read the paragraphs below.

Troubleshooting: shared directory is invisible

It may happen that the /vagrant directory seems empty inside the Vagrant machine. Two steps will fix this problem. First, run

Machine> sudo /etc/init.d/vboxadd setup

inside the Vagrant machine. Second, log out and run

Terminal> sudo vagrant reload

outside the Vagrant machine. Then do vagrant ssh and take an ls /vagrant to see that the files in the project directory (e.g., Vagrantfile and the Vagrant box) are visible.

Troubleshooting: "couldn't connect to display ..."

This error message points to the problem that X11 graphics cannot be shown on the host. It should be sufficient to start X11 on the host, see Step 4 above.

Troubleshooting: Internet is not reachable

A test if Internet is reachable is to run a ping command inside the machine, e.g.,

Machine> ping us.ubuntu.archive.com

A hanging command indicates that Internet is not reachable. Log out of the box, run vagrant reload, and vagrant ssh. Try the ping command again.

Working with an initialized Vagrant machine

The daily work with the Vagrant machine is very easy. Simply go to the vagrant directory where the machine resides and run

Terminal> vagrant up
Terminal> vagrant ssh

You are now inside the machine and can reach files on the host from /vagrant/projects. Log out with Ctrl-D and in again with vagrant ssh. Create and edit files on the host in ~/vagrant/projects and its subdirectories.

Before closing a laptop or shutting it down, it is recommended to log out of the Vagrant machine and run vagrant suspend.

A typical session with writing and testing Python programs

Here is a worked example on creating a Python program on Windows or Mac running it on Ubuntu. Go to the vagrant project directory on your Windows or Mac computer, make a new directory mytest, go to this directory, and launch a text editor, such as TextEdit on Mac or Notepad++ on Windows, to create a Python program test.py. The relevant Unix commands are

Terminal> cd ~/vagrant/projects
Terminal> mkdir mytest
Terminal> cd mytest
Terminal> gedit test.py&

Write the following code in test.py:

print 'Hello!"

Save the file.

In another terminal window, go to the vagrant directory and start the vagrant machine if it is not already running:

Terminal> cd ~/vagrant
Terminal> vagrant up
Terminal> vagrant ssh

You are now inside an Ubuntu machine in this terminal window.

Inside Ubuntu, move to the mytest directory which is shared with your Mac or Windows computer:

Machine> cd /vagrant/projects/mytest

Run the test.py program:

Terminal> python test.py
  File "test.py", line 1
    print 'Hello!"
                 ^
SyntaxError: EOL while scanning string literal

You need to correct the bug. Go to the text editor with the test.py on your Windows or Mac computer and change the double quote to a single quote:

print 'Hello!'

Go to the terminal window where the Ubuntu machine is running and rerun the program:

Machine> python test.py
Hello!

You should now understand how you edit programs on your Windows or Mac computer and how you run the programs inside the Ubuntu machine in a terminal window. Just remember that /vagrant in Ubuntu corresponds to the directory ~/vagrant on the Mac or Windows computer.