Setting Up a Programming Environment in a Virtual Machine
Steven J. Zeil
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.
-
You could, of course, simply install it directly on your PC and later uninstall it when you were done or if you decided you didn’t want it. But uninstalls are often not “clean” – some will leave debris behind and occasionally that can affect the later operation of your PC.
-
Or, you could install it in a virtual machine, use it within that simulation, and later simply delete the entire virtual machine if you no longer need that software.
Advantages of using virtual machines:
- Lets you try out new packages with minimal risk to your real PC.
- Lets you install other operating systems and software that runs on thise operating systems with no permanent change to your PC.
- VMs can be paused and shut down when you feel like it, then restarted later as if they had never been paused. You can also take “snapshots” of the VM before doing anything risky and later roll the VM to a former snapshot.
Disadvantages of using virtual machines:
-
The VM will run somewhat slower than the real thing. It used to be that VMs ran a lot slower than the real thing, but the simulator technology has progressed quite a lot.
-
A VM sucks up significant resources on your PC.
- You will need to set aside multiple Gigabytes of disk storage to serve as the simulated hard drive of the VM.
- You will need to allocate a significant chunk of your RAM memory to the VM when it is running. (You get this back when the VM is not in use.)
- Assuming your real CPU is multi-core, you will probably want to allocate at least 2 of those cores to the VM when it is running. This may noticably slow down operations in your PC’s “real” operating system. (Again, you get these back when the VM is shut down.)
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.
-
Get it here.
-
You will want to download both the “platform package” and the “extension pack”.
-
-
Run the platform package and follow the instructions to install.
If you need more information, look here.
-
Run VirtualBox once you have it installed.
From the
File
manu, selectPreferences
, go toExtensions
, 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.
-
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.
-
Run VirtualBox. Click the “New” button to start the process of setting up a new VM.
-
In the box that pops up, give your VM any easy to remember name (e.g., “Ubuntu VM1”).
For the
Type
, select “Linux”, then forVersion
select “Ubuntu 64-bit”.Click
Next
. -
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
. -
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. ClickNext
.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.
-
Select your machine and click “Settings”.
-
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. -
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
. -
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.
-
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.
-
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.
-
apt-get
is the program you use to update your Linux software and to install or uninstall software packages.apt-get
, however, can only be run by a Linux administrator. Now, you are the adminstrator for the Linux OS you have just installed, but for safety’s sake you run most programs as and ordinary, non-administrator user. -
sudo
is a command that says “run the following as the adminstrator”.
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.
- 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
-
The Java run-time system, because the IDE we will use, Eclipse, actually runs in Java. While we’re at it, we might as well get the whole Java compiler, in preparation for future programming projects in that language.
-
The
g++
compiler for C++. - The
gdb
debugger. - The
make
project build system.
-
Open a Terminal from the launch menu (lower left corner).
-
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
-
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”.
-
Launch the Firefox web browser from the left toolbar in Ubuntu.
-
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.
-
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.
-
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.
-
-
What you will receive from this download is a
tar.gz
package. 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. -
Type
~/eclipse/eclipse &
to launch Eclipse.
-
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.