Setting Up a Programming Environment in a Virtual Machine

Steven J. Zeil

Last modified: Jan 21, 2019
Contents:

A virtual machine (VM) is a simulation of a complete computer, including display & networking hardware, operating system, & applications.

If that sounds daunting, it used to be. But now there are free packages to set up and run virtual machines that make it fairly easy.

Suppose that you are thinking about installing a large software package that you might not want to use only for a limited time or that you worry might prove to be incompatible with other software already on your PC.

Advantages of using virtual machines:

Disadvantages of using virtual machines:

The remainder of this document walks you through setting up a virtual machine with Java and C++ compilers and the Eclipse IDE.

1 Install the Virtualization Software

We’re going to use VirtualBox, a free virtualization package from Oracle.

  1. Get it here.

    • You will want to download both the “platform package” and the “extension pack”.

  2. Run the platform package and follow the instructions to install.

    If you need more information, look here.

  3. Run VirtualBox once you have it installed.

    From the File manu, select Preferences, go to Extensions, and add the extension packe that you downloaded.

2 Create Your First Virtual Machine

For your first virtual machine, we are going to install Ubuntu Linux.

  1. Go to the Ubuntu download page and download the Ubuntu desktop OS. What you will be getting is an ISO file, an image that could be burned directly onto a DVD. But we won’t need to burn it. VirtualBox can read ISO files as if they really were DVDs.

  2. Run VirtualBox. Click the “New” button to start the process of setting up a new VM.

  3. In the box that pops up, give your VM any easy to remember name (e.g., “Ubuntu VM1”).

    For the Type, select “Linux”, then for Version select “Ubuntu 64-bit”.

    Click Next.

  4. Next you need to decide how much of your RAM memory the VM will take up when running. You need to leave enough to keep your real OS running smoothly, but enough to run your development tools and, eventually, your developed programs in the VM. 1024M (1GB – the default) is, in my experience, not enough. On a machine with a total of 8G RAM, I generally allocate 2GB (2048M) to a VM.

    Click Next.

  5. Now you need to set up the simulated hard drive. Select “Create a Hard Drive Now” and click Create.

    Select VDI and click Next.

    Select Dynamically Allocated. This means that the disk space does not actually get grabbed up until the VM actually needs it, at the cost of slightly slower simulated disk access. Click Next.

    Now you get to choose the max size of the drive. Again, I find the default a bit skimpy for a development machine, so I generally allocate 20GB.

    Click Create.

3 Install the OS on the New Virtual Machine

This brings you back to the main VirtualBox manager. You now have a VM, but its simulated drive is completely empty.

  1. Select your machine and click “Settings”.

  2. Go to System, Processor. If you have at least 4 CPU cores on your real processor (you can see the total number on the Processor(s) scale), reserve 2 for this VM.

  3. Go to Storage. You should see a “Controller: IDE” device and, beneath that, an icon for a DVD platter. That’s your simulated DVD drive.

    Select the platter icon. On the right, you should see the same icon with a menu drop-down arrow next to it. Click it and select “Choose Virtual Optical Disk File”. Find the Ubuntu ISO file that you downloaded earlier and select it.

    Click OK.

  4. You are now back at the main VirtualBox manager window. Make sure your new VM is selected on the left, and click the Start symbol in the toolbar.

    This will start the boot-up of your simulated machine. Because we have placed the Ubuntu DVD image in the simulated DVD drive, it will boot from there, and eventually offer you the option to install Ubuntu Linux.

  5. Follow the prompts to install Ubuntu. Don’t be worried about warnings about erasing your entire disk. This VM only knows about the existence of the simulated disk drive created for it earlier. It can’t touch your existing files on the real drives.

    When offered the option, do accept the offer to download updates during the installation. Third party packages can also be accepted safely.

    Other than that, if you are uncertain about what to choose at some of the Ubuntu prompts, the suggested defaults are usually safe.

  6. After the installation, Ubuntu will want to restart the simulated machine. Let it do so. Hit Enter when prompted to do so.

    A few seconds later, you should be logged in to your Ubuntu VM.

4 Setting Up Your Programming Environment

4.1 sudo apt-get

Before we continue on, let’s introduce a couple of the programs that you will be using through the rest of this process.

For example, the following sequence is how you update your Linux software:

sudo apt-get update
sudo apt-get upgrade

The first command actually fetches the latest information about what updates are available. The second installs the updates. The “sudo” in front causes them to be run as the administrator. When you give the first “sudo” command, you will be prompted for your password to prove that you really are the account owner. After that, sudo remembers your identity for a short period of time, so you can give multiple sudo commands in a row, only asked for your password once, as long as you don’t take too much time in between.

  1. Go ahead and give the above two commands apt-get commands now.

Try to give those two commands on a fairly regular basis so your Linux OS stays up to date. You’ll actually be notified when you start a new bash session if there are updates awaiting.

Close your bash session for now by giving the command

exit

4.2 Installing the Compilers

Now we’re ready to start installing some real software. You will need

  1. Open a Terminal from the launch menu (lower left corner).

  2. Install your compilers with the following commands:

    sudo apt-get install default-jdk
    sudo apt-get install g++
    sudo apt-get install gdb
    sudo apt-get install make
    
  3. Once those are done, verify your installation by typing the following in your terminal:

    java -version
    g++ --version
    gdb --version
    make --version 
    

    Each should respond with an identifying message making clear that the software is installed and running.

4.3 Installing Eclipse

Next up is the Eclipse IDE. We could install this with an apt-get command also, but the version in the Ubuntu respository seems to lag far behind the Eclipse project releases. As of this writing, you want “Eclipse Neon”.

  1. Launch the Firefox web browser from the left toolbar in Ubuntu.

  2. Get Eclipse for your platform from the Eclipse Foundation.

    Under the “Get Eclipse…” button, look for and click on the “Download Packages” link. You’ll have a choice of various pre-packaged forms of Eclipse.

    1. If all that you are interested in is C++, scroll down to the “Eclipse IDE for C/C++ Developers” and download the installer for 64-bit Linux.

    2. If you want to reserve the possibility of doing Java programming in the future, scroll down to the “Eclipse IDE for Java Developers” and download the installer for 64-bit Linux.

  3. What you will receive from this download is a tar.gzpackage. This is a compressed archive (similar to a .zip file).

    I will assume that you downloaded this into your Windows Downloads directory. From your Terminal, copy this into your Linux home directory:

    cd ~
    cp ~/Downloads/eclipse-*.tar.gz .
    tar xvzf  eclipse-*.tar.gz
    ls    
    

    You should see a new ~/eclipse/ directory. Your Eclipse installation is in there.

  4. Type

    ~/eclipse/eclipse &
    

    to launch Eclipse.

  5. If You Installed the Eclipse for Java Developers

    If you installed the Java Eclipse rather than the C/C++ Developer’s version, we need to add the C++ support.

    From the Eclipse Help menu, select Install new software....

    Search “All Available Sites” for “C++” (Note, the search is really slow. Be patient.) or scroll down to “Programming Languages”.

    Select:

    • “C/C++ Development Tools”
    • “C/C++ Library API Documentation…”.

    and follow the on-screen instructions to install those.

You should now be able to create C++ projects in Eclipse.

If you need some help, revisit the Eclipse sections of IDEs for Compiling under X and Debugging under X from CS252.