Treat Yourself to a VM

Maybe you want you run linux on your mac (to play with docker, perhaps). This tutorial will walk you though setup of a Vagrant VM. At the end, you'll be able to easily boot and connect to a virtual linux machine and develop ruby on it. We're also going to install docker. This is basically a list of commands without a lot of context. Sorry!

environment setup

These instructions use Vagrant. You can download a .pkg file on vagrant's site. Install that, fire up your terminal, mkdir and cd into a new directory and follow along. First:

$ vagrant init ubuntu/trusty64

When it finishes, you will see that your directory has new Vagrantfile. Open this up and uncomment/modify the line about port forwarding so that it reads something like this: "forwarded_port", guest: 3000, host: 3000  

That will let you access port 3000 on your vagrant box via port 3000 on the host machine. We're using this port because it is the default for Rails. Fire up your new linux box with vagrant up. This may take a little while because you have to download and set up the linux machine.

Next, we need to get the new environment set up. That entails:

There are a bunch of great provisioning tools out there, but that's for another post. I'm going to just go through what I think are some of the basics.

Install vbguest on your copmuter with

$ vagrant plugin install vagrant-vbguest

Now you can ssh in with the vagrant ssh command.

Installing Programs

You're going to be doing a lot of apt-geting. Install a necessary Vagrant plugin and update your apt repositories with

$ sudo apt-get update

Then you'll need to install a bunch of other software. I recommend the following:

$ sudo apt-get install git
  • ruby - We get 1.9.3 for free, but we're going to use something a little more recent. We're going to use rbenv and ruby-build. You can follow their install instructions. I'll just reproduce the commands here:
$ sudo apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev libsqlite3-dev
$ git clone ~/.rbenv
$ git clone ~/.rbenv/plugins/ruby-build
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

At this point you'll need to restart your shell window. One way to do this is to log out of the vagrant box with logout and then log back in with vagrant ssh.

Then back to installing ruby. You can see a list of available ruby versions with the rbenv install -l command. Actually installing ruby takes a little while so you will have a minute to get up and stretch*.

$ rbenv install 2.2.2 # obviously you can use whatever version you would like

Once that's installed, you can set it to be your global version (used everywhere) with

$ rbenv global 2.2.2

Now you have ruby!

$ sudo apt-get install wget
$ wget -qO- | sh
$ sudo usermod -aG docker vagrant
$ sudo docker run hello-world

The last command should show you a bunch of things including: Hello from Docker.

  • docker-compose - while we're at it, we will install docker-compose to help manage multi-container apps:
$ sudo -i
$ curl -L`uname -s`-`uname -m` > /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
$ exit

I like vim. If you've been using it all along then that's great. Otherwise, this will be an opportunity. One great thing about using vim all the time is that you get it for free with our standard ubuntu version and you only need to clone or copy a few files in order to feel at home wherever you find yourself working. Clone or copy them into your new box.

Clone Your Project

The next thing you need is your project. For this demo, I'm using my most-seen and possibly least-favorite project, storedom.

$ git clone ~/src/storedom

Now take a look at what you have: a linux virtual machine running on your computer that you can use to develop on or do whatever you need. If you run your migrations then you should be able to run rails s in your vagrant box and hit the app at localhost:3000 on your host machine. Things are going well!

* If you really want to follow along then you can open a new window, vagrant ssh, cd into /tmp/, figure out what the ruby-build log file is and then tail it. E.g. tail -f ruby-build.20150712002651.320.log