I’ve been working on a system where the Ruby install was a bit messed
up on the host. Normally, I would fix this, but I wanted to get the my
blog working to write an article.
With Ruby not working, I definitely saw a Rabbit Hole of stuff to fix:
get rvm setup properly, install newer Ruby, make sure Gems work, etc.
I’ve done this process before and it can be frustrating because I
don’t know how long it would take, even though I have done it before
(sounds silly, but it’s true!) The main reason is I don’t know how the
system got into such a state, so I don’t know what to undo.
Instead of fixing this problem directly on the system, I want to have
a solution that can work for any system, Ruby setup or not, new or
old, now and in the future:
Setup Vagrant to automatically configure a virtualbox to get
everything needed for Jekyll to run.
Obvious, right? :-)
Requirements
If you don’t know what Vagrant or Virtualbox is, please read my
introductory
article
on them.
If you don’t know what Jekyll is: Jekyll is a static site
generator. It will take files formatted in Markdown and convert them
into HTML and create a whole website.
Jekyll is a great tool to blog and focus on content, not markup.
I will be starting with a bare Vagrantfile and installing onto a
Ubuntu 16.04 box (Xenial). So the Vagrantfile will be:
Installing Ruby
I usually use rvm to manage different version of Ruby in any
system. Initially, I followed the standard installation method I would
take to install, right from the rvm site
Putting the above script into the Vagrantfile as:
The above Vagrantfile results in a working version of Ruby, BUT, when
I wanted to install items using bundler, these errors started
occuring:
Trying with sudo prepended:
This made me think: hmm… having rvm installed by root is not
good. rvm is a user level utility and using sudo for everything just
won’t work in all cases.
As much as I want to just run things as root all the time, being the
root user is just too crazy for me, even in a virtual machine.
Better way to install Ruby
My desire is to use rvm to manage different ruby versions and to have
vagrant install everything for me.
I tried:
From my understanding, makes root the vagrant user and executes commands from that point forward.
Nope, rvm was still installed with root privileges.
I also tried:
But that did not work as well. Vagrant didn’t want to boot up for some
reason.
The rvm site had the answer, use
an external shell script.
and the file contents of install_rvm.sh are:
and install_ruby.sh are:
Place those two files in the same directory as the Vagrantfile and run
vagrant up.
Now, gem can install bundler without root permissions:
Installing Node
Of course, where there’s JavaScript, there’s an execjs requirement!
I defaulted to installing Node.JS for the ExecJS runtime:
Keep it simple!
Networking?
After installing jekyll with:
and starting up:
I ran into a problem, how to access the site?? The IP address of the
server is automatically resolved internally when I login through SSH.
Public Network
To setup an IP address that can be accessible from the host computer,
add the public_network option to the configuration:
On the next time the host system starts, there will be a question
about which network adapter to use:
To find the address, from the host system, use the ifconfig command:
In this case, the IP address is: 192.168.1.100. On the browser of the
host system, use that address to access the local server with Jekyll’s
port:
http://192.168.1.100:4000
Whole Script
Vagrantfile
install_rvm.sh
install_ruby.sh
Post Install
Some things to do post install:
run: gem install bundler
run: bundler at the folder with Gemfile listed
run: ifconfig to find out the IP address
Conclusion
Whew, what turned out to be a way to avoid fixing the installed
version of Ruby on my system turned into a massive learning in Vagrant
configuration. This article covered:
Using the “public_network” option
Using external scripts
Using “privileged” option to avoid root from installing software
This definitely took work, maybe just as much work as fixing the Ruby
installation, but it has given me a tool to run a Jekyll server on
almost any computer.