Installing on Linux

Linux has many different distributions, each having its own method of installing software, along with various idiosyncracies around how it behaves. It would be too difficult to cover them all, so in this section, we’ll outline how to get a Rails environment running in a virtual machine running Ubuntu Linux. Most of the software we’ll install would be needed on any Linux distribution, so if you aren’t using Ubuntu, hopefully this will help you know what you need to set up.

Also note that if you are using Ubuntu but not a virtual machine, some of the behavior might be different, especially if your machine already has some packages installed on it.

With that disclaimer out of the way, our setup will require a few different steps. First, we’ll set up a virtual machine using Vagrant (which you can skip if you already have Linux running on your computer). Then, we’ll install some system software that the Ruby and Rails development tools require, before finally installing Ruby and Rails.

Setting Up a Virtual Machine

Vagrant[14] is a system that can manage a virtual computer. Virtual Box[15] provides that virtual computer, and together they can mimic a real computer, but do so to provide a totally clean, predictable, and isolated environment from your actual computer. We’ll set up the virtual machine with Ubuntu Linux.

Of course, that means you need to install both Vagrant and Virtual Box on your computer, and how you do that depends on your operating system! Vagrant provides installation instructions[16] and the best thing to do is to follow the instructions there. Once you’ve done this, head over to Virtual Box’s installation page[17] and install that on your computer.

With those installed, we’ll create a file to describe how Vagrant should set up your virtual machine. This file is called Vagrantfile. You’ll need to create it somewhere on your computer where you prefer to work, such as your home directory or a subdirectory of it. Locate wherever that is and create Vagrantfile to have the following contents (comments in the code explain what each bit does, if you are curious):

 # We want to use version '2' of Vagrant's configuration language
 Vagrant.​configure​(​"2"​) ​do​ |config|
 
 # This is the operating system to use, in this case
 # Ubuntu Linux
  config.​vm​.​box​ = ​"ubuntu/trusty64"
 
 # This is configuration specifically for the virtual
 # machine, and this gives it 4G of memory
  config.​vm​.​provider​ ​"virtualbox"​ ​do​ |vb|
  vb.​memory​ = ​"4096"
 end
 
 # When Rails runs on port 3000 inside your virtual machine, this
 # allows you to access it from a browser on your machine by
 # going to port 3000 on your machine.
  config.​vm​.​network​ ​"forwarded_port"​, ​guest: ​3000, ​host: ​3000
 
 # This will mount your current directory on your computer
 # to the directory /files_on_your_computer inside the virtual machine
  config.​vm​.​synced_folder​ ​"."​, ​"/files_on_your_computer"
 end

If you aren’t in a command-line window at this point, open one up and change to the directory where you created this file. Then bring up the machine with the following command:

 $ ​​vagrant​​ ​​up

The very first time, it will take a while for this command to complete. It has to download and install an entire operating system, so be patient. When it’s done, you can log in to the virtual machine, like so:

 $ ​​vagrant​​ ​​ssh

You are now logged in to your virtual machine, so we can now start installing the software you’ll need to learn Rails.

Installing System Software

If you are using a virtual machine, the commands that follow assume you have logged in to it with vagrant ssh. Otherwise, we’ll assume you are logged in as a user who can execute sudo. Also note that in this case, you may have some software installed already. If you experience problems, you might want to update that software to the latest versions.

Many Ruby libraries are actually wrappers for C libraries, and when you install them, your system will try to build those libraries or build native connectors. This is the main reason we need certain software installed before we get to Ruby. First, refresh the list of packages available for your operating system:

 $ ​​sudo​​ ​​apt-get​​ ​​update

That will produce a large amount of output. Once that’s done, we’ll install several different libraries and tools:

 $ ​​sudo​​ ​​apt-get​​ ​​install​​ ​​-y​​ ​​autoconf​​ ​​
  ​​bison​​ ​​
  ​​build-essential​​ ​​
  ​​curl​​ ​​
  ​​g++​​ ​​
  ​​gcc​​ ​​
  ​​git​​ ​​
  ​​libffi-dev​​ ​​
  ​​libgconf-2-4​​ ​​
  ​​libgdbm-dev​​ ​​
  ​​libncurses5-dev​​ ​​
  ​​libreadline-dev​​ ​​
  ​​libreadline-dev​​ ​​
  ​​libsqlite3-dev​​ ​​
  ​​libssl-dev​​ ​​
  ​​libxi6​​ ​​
  ​​libyaml-dev​​ ​​
  ​​make​​ ​​
  ​​sqlite3​​ ​​
  ​​xvfb​​ ​​
  ​​zip​​ ​​
  ​​zlib1g-dev

This will produce yet more output and likely take a while. When that’s done, we next need to install NodeJS, because you can’t do web development without JavaScript, and Rails requires Node to help manage the JavaScript you’ll eventually be writing.

 $ ​​curl​​ ​​-sL​​ ​​https://deb.nodesource.com/setup_12.x​​ ​​|​​ ​​sudo​​ ​​-E​​ ​​bash​​ ​​-
 $ ​​sudo​​ ​​apt-get​​ ​​install​​ ​​-y​​ ​​nodejs

You can verify this worked by executing node --version on the command line. You should see a version of the form 12.x.y.

Lastly, we’ll install Yarn, which is a package manager for Node libraries.

 $ ​​curl​​ ​​-sS​​ ​​https://dl.yarnpkg.com/debian/pubkey.gpg​​ ​​|​​ ​​sudo​​ ​​apt-key​​ ​​add​​ ​​-
 $ ​​echo​​ ​​"deb https://dl.yarnpkg.com/debian/ stable main"​​ ​​|​​ ​​
  ​​sudo​​ ​​tee​​ ​​/etc/apt/sources.list.d/yarn.list
 $ ​​sudo​​ ​​apt​​ ​​update​​ ​​&&​​ ​​sudo​​ ​​apt​​ ​​install​​ ​​-y​​ ​​yarn

You can verify this worked by executing yarn --version on the command line. It should show you something like 1.19.x, though as long as it’s 1.19 or higher, you should be fine.

Now we can install Ruby and Rails!

Installing Ruby and Rails

Your system likely has Ruby installed, though it’s also likely not the version you need to run Rails. You need Ruby 2.6.0 or higher, but updating your system’s Ruby can have unintended consequences. Instead, we’ll use rbenv[18] to install Ruby in parallel to your system Ruby. This also allows you to use many different versions of Ruby on the same computer, but without disrupting the verison of Ruby your system may depend on. rbenv is widely used for exactly this purpose. First, install it like so:

 $ ​​curl​​ ​​-sL​​ ​​
  ​​https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-installer​​ ​​
  ​​|​​ ​​bash​​ ​​-

Next, you’ll need to adjust your shell settings to put the rbenv-managed Ruby in your path. If you are using bash, do this:

 $ ​​echo​​ ​​'export PATH="$HOME/.rbenv/bin:$PATH"'​​ ​​>>​​ ​​~/.bashrc
 $ ​​echo​​ ​​'eval "$(rbenv init -)"'​​ ​​>>​​ ​​~/.bashrc
 $ ​​source​​ ​​~/.bashrc

If you are using another shell, consult the rbenv website for instructions if you aren’t sure. Next, we’ll install Ruby 2.6.5, the lastest version of Ruby 2.6 at the time of this writing:

 $ ​​rbenv​​ ​​install​​ ​​2.6.5

This will take a long time, as it’s downloading and compiling Ruby locally. Once that’s done, you won’t yet be using Ruby 2.6.5. To do that, you either need to tell rbenv to use Ruby 2.6.5 in the current directory or globally. To avoid confusion, we’ll do it globally, meaning that rbenv should use Ruby 2.6.5 if it doesn’t know what other version to use. We do that like so:

 $ ​​rbenv​​ ​​global​​ ​​2.6.5

With this done, you can try running ruby -v on the command line. You should see 2.6.5 in the output.

Next, we will install Rails itself. Rails is a RubyGem, and Ruby comes with the command gem which installs RubyGems. We will use that to install Rails like so:

 $ ​​gem​​ ​​install​​ ​​rails​​ ​​--version=6.0.1​​ ​​--no-document

When that completes, you can verify it worked by running rails --version. You should see 6.0.1 in the output.

The last thing to install is ChromeDriver.[19] This is a tool that manipulates Google’s Chrome browser during some of the tests you’ll write, specifically what Rails calls system tests. We’ll learn about that later, but to set it up, you’ll need to first install Chrome, which requires the following incantations:

 $ ​​sudo​​ ​​curl​​ ​​-sS​​ ​​-o​​ ​​-​​ ​​
  ​​https://dl-ssl.google.com/linux/linux_signing_key.pub​​ ​​
  ​​|​​ ​​sudo​​ ​​apt-key​​ ​​add​​ ​​-
 $ ​​sudo​​ ​​echo​​ ​​
  ​​"deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main"​​ ​​
  ​​|​​ ​​sudo​​ ​​tee​​ ​​/etc/apt/sources.list.d/google-chrome.list
 $ ​​sudo​​ ​​apt-get​​ ​​-y​​ ​​update
 $ ​​sudo​​ ​​apt-get​​ ​​-y​​ ​​install​​ ​​google-chrome-stable

After that, to install ChromeDriver, you’ll need to download a zip file, unzip it, and copy the binary into your path.

 $ ​​wget​​ ​​https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip
 $ ​​unzip​​ ​​chromedriver_linux64.zip
 $ ​​sudo​​ ​​mv​​ ​​chromedriver​​ ​​/usr/bin/chromedriver
 $ ​​sudo​​ ​​chown​​ ​​root:root​​ ​​/usr/bin/chromedriver
 $ ​​sudo​​ ​​chmod​​ ​​+x​​ ​​/usr/bin/chromedriver

There’s no easy way to test that this works, but when we get to writing a system test, remember this section in case something goes wrong.

This completes the setup of Ruby and Rails. The rest of this chapter will outline other software you might need to do development.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.8.247