Web Development with Rails and Git Tutorial

Demo 1: Development Environment Setup

▶️ Play from the beginning (~30 minutes)

In this demonstration, I will show you how to setup the development environment used in the rest of the demos. The following setup should generally work for Windows, Mac, and Linux.

Before I jump into the demo, I’d like to clear up a little terminology. In these demos, I will be using a virtual machine (VM). A VM is where one OS is run inside of another OS (rather than directly on a physical machine). The parent OS is called the host OS. For example, imagine you have a Windows computer—that’ll be the host OS. You can use the VirtualBox software to install a Linux OS within the host Windows OS and to run a Linux VM as if it were a Windows program.

▶️ 1. Installing Software

  1. ▶️ Register an account at https://github.com/ (if you don’t already have one). Git and GitHub will be used for version control and collaboration in these demos. Be sure not to lose your GitHub username and password.

  2. ▶️ Install the Visual Studio Code (VS Code) editor (https://code.visualstudio.com/). VS Code will be the code editor of choice for these demos. Use the latest stable version. Additionally, within VS Code, install the following extensions:
    • Rails
    • Ruby
    • erb
    • markdownlint
    • Markdown PDF
    • Code Spell Checker
  3. ▶️ Install a Bash shell with SSH client (if you don’t already have it). Windows users, download and install the Git for the Windows platform (http://git-scm.com/download/win), which comes with a Bash shell and SSH client. MacOS users have this software by default (see the Terminal app). Debian/Ubuntu Linux users have the shell, but may need to install the “openssh-client” package (if it’s not already installed by default).

  4. ▶️ Download and install VirtualBox (https://www.virtualbox.org/). I will be using this software to run an Ubuntu Linux virtual machine. This VM will house the majority of the Rails development tools. In essence, the development environment in these demos will mainly be on Ubuntu Linux (with some graphical software running in the host OS).

  5. ▶️ Download and install Vagrant (https://www.vagrantup.com/). Vagrant is used to package, distribute, and run custom-configured VMs. I have prepared a Vagrant “box” as you will see below.

▶️ 2. Setting Up Workspace and Initializing VM

  1. ▶️ Create a folder workspace. This folder is where all the Rails projects in these demos will go.

  2. ▶️ Download the file Vagrantfile and the file provisioner.sh, and save them in your workspace folder. Make sure that no file suffix (e.g., “.txt”) gets added to the Vagrantfile when saving it. For example, one way to download it would be to right-click on the hyperlink and select “Save Link As…” (or similar) from the context menu.
    • Update: This step was updated after the video was recorded. In particular, the provisioner.sh script was added. Thus, the script is not mentioned in the video.
  3. ▶️ Launch a terminal. In Windows, it involves launching Git Bash. In MacOS, this involves launching the Terminal app. I assume that Linux users need no clarification here.

  4. ▶️ In the terminal, change directory (using the cd command) to the workspace folder. Note: I will be using the command-line a lot in the demos. I will generally assume that readers are familiar with the basic file management and navigation commands (cd, rm, cp, mv, etc.). If you’re new to the command-line, I highly suggest you spend some time on your own learning about it—for example, Codecademy has a course: https://www.codecademy.com/learn/learn-the-command-line.

  5. ▶️ Run the command vagrant up to download and initialize a Vagrant box. BEWARE! This command (1) may take a long time to complete, (2) downloads a big file (~700MB), and (3) performs at least one processor-intensive compilation (of Ruby). Once this command completes, you will have a running Ubuntu Linux VM (headless).
    • Update: Your computer must support virtualization in order for VirtualBox to work (and consequently this step). Even if your computer does provide virtualization support you may need to enable in your system BIOS. For Windows users, I have also heard reports that Hyper-V and VirtualBox don’t play well together, so you may need to uninstall Hyper-V.

▶️ 3. Testing the Environment

  1. ▶️ Run the command vagrant ssh to SSH into the Linux VM. A command prompt should appear that looks like this: [vagrant@ubuntu-xenial:~] followed by a $ prompt. If you use the ls -l command, you will see a list of files in the current directory. Among them should be a workspace folder (actually a symbolic link to the folder /vagrant).

  2. ▶️ Change directory (using the cd command) to the workspace folder. Note that this folder is synced with the workspace folder on the host OS. That is, changes made in the folder on one side (VM or host OS) are instantly visible on the other side.

  3. ▶️ Enter the command git clone https://github.com/sdflem/sample_app.git to use Git to download an example project. A sample_app folder should be visible inside the workspace folder. Note that the workspace folder is synced with the host OS, so the sample_app folder should also be visible in the host OS’s file explorer. The main reason for syncing this folder is that it will enable the use a GUI code editor (VS Code) to work on the code files.

  4. ▶️ Change directory (using the cd command) to the sample_app folder. When you run this command, RVM should print a message, which lets you know it’s working. If no such message appears, then something is wrong. A common problem is that the terminal application is not configured to run as a “login” shell. This issue seems to come up the most for Linux users, or users of more exotic terminal applications. Typically, the solution can be found in the terminal application’s settings.

  5. ▶️ Run these three commands to set up the web app project (i.e., prepare it to run). The first two commands may take a while to complete because they download and install a lot of Ruby gems.
    • gem install bundler -v '~> 1.17'
      • Update: Version option wasn’t needed when the video was made. However, since then, Bundler came to require a newer version of Ruby than the one used in the sample app. Thus, we will use an older version of Bundler that doesn’t have this requirement.
    • bundle install --without production
    • rake db:setup
  6. ▶️ The project comes with some automated tests. Run the command rake test to execute the tests. You should see that all the tests passed.

  7. ▶️ Start up the Rails web app server with the command rails s -b 0.0.0.0 (those are zeros). You should see that the server has started without error. Note that this command will not “return” like other commands—that is, the command prompt will not reappear until you halt the server process (covered below).

  8. ▶️ Now open the URL http://localhost:3000 in a web browser. A “Welcome to the Sample App” web page should appear.

  9. ▶️ To further test out the web app, log in with the email example@railstutorial.org and the password foobar. It should quickly become clear that this is a Twitter-like app. To create a post, click the “Home” link, and then enter and submit some text. The new post should appear at the top of the post stream.

▶️ 4. Shutting Everything Down

To shut down everything:

  1. ▶️ Back in the terminal, type Ctrl-C to kill the Rails server (that is, press and hold the Ctrl key and then click the ‘C’ key).
  2. ▶️ Enter the command exit to logout of the VM.
  3. ▶️ Enter the command vagrant halt to shut down the VM.

To restart the VM, run vagrant up to start it up (should be much faster than last time) and run vagrant ssh to log in again.

© Scott D. Fleming 2018 • Made with GitHub Pages and Markdown