Active Rails

Generated from b81e05214 on 2020-09-01

Rails Installation Guide - Linux

Linux is perhaps the easiest of the three operating systems to get Ruby running on, but Rails is a little more difficult. All Linux flavours come with decent package management built in, so getting the necessary pre-requisites is easy, safe, and guaranteed not to mess with software already installed on your system.

This guide has been verified with both Ubuntu 18.04, and presumes that your desktop is fully up to date, with all updates installed, but no other installed pre-requirements.

To make sure your system is up to date, run this command in a terminal:

$ sudo apt-get update

build-essential

The first thing we will need to install is a package called build-essential. This package includes a bunch of tools that will be used to install Ruby, and also by Ruby to compile C extensions. Without these tools, you won’t be able to install or let alone use Ruby.

To install these tools, run this command:

sudo apt-get install build-essential openssl libssl-dev libsqlite3-dev

Git

The second thing we will need to install is Git. This will be used for version control for your Rails application, and is also used to setup a tool called asdf in a little while.

We can install this with:

$ sudo apt-get install git

To verify that this has been installed, run:

$ git version

You should see something like:

git version 2.17.1

asdf

Next, we’ll need to install a tool that is used to manage the installation of different programming languages. We’re going to use this tool to install both the Ruby and Node.js programming languages.

Installation

To install asdf, you can go to https://asdf-vm.com/#/core-manage-asdf-vm and copy and paste the command at the top of the page. At the current time, that command is:

$ git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.7.8

This will add some shell scripts to the ~/.asdf directory. We can add these shell scripts to our .bashrc file by running these commands:

$ echo ". $HOME/.asdf/asdf.sh" >> ~/.bashrc
$ echo ". $HOME/.asdf/completions/asdf.bash" >> ~/.bashrc

This will make it so that we have access to the asdf command in our terminal.

We will need to either restart our terminal, or reload this ~/.bashrc file to load in these changes:

$ . ~/.bashrc

To verify that this has been installed correctly, run:

asdf --version

You should see this, or similar:

v0.7.8-4a3e3d6

Ruby

To install Ruby using asdf, we first need to add the ruby plugin to asdf. Do that with this command:

$ asdf plugin-add ruby

Next, we can install Ruby by running:

$ asdf install ruby 2.7.1

asdf and its Ruby plugin knows where to get the source code for Ruby 2.7.1, and also what dependencies are needed to compile it.

Once it’s done all of that, it will start compiling Ruby, and you’ll see lots and lots of random output as checks a lot of things and then compiles all of the necessary files. There doesn’t appear to be an in-built way to silence this output, but you can just let it do its thing - go get a can of Diet Coke or something, it will take a few minutes.

When it’s done, you’ll see a nice happy:

Installed ruby-2.7.1 to /home/youruser/.asdf/installs/ruby/2.7.1

So you now have a ruby installed. How do you use it? The folder it installed to isn’t part of your path, so calling ruby will have no effect.

We will need to set a current version of Ruby using asdf:

$ asdf gobal ruby 2.7.1

Now when we run ruby -v, we should see the current version of Ruby:

ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]

Node

Next up, we will need to install Node.js. This is used by the Webpacker gem, which is a dependency of Rails. The webpacker gem uses a JavaScript package called Webpack to compile the JavaScript assets for your application.

To install Node, we’ll also use asdf. We first have to install the Node plugin:

$ asdf plugin-add nodejs

And we have to run this command so that asdf can verify Node installation files:

$ bash ~/.asdf/plugins/nodejs/bin/import-release-team-keyring

Once those two steps are done, we can install Node:

$ asdf install nodejs 12.16.2

Then we will need to configure asdf to use this version of Node:

$ asdf global nodejs 12.16.2

To verify that this has been installed correctly, run node -v. You should see this:

v12.16.2
Yarn

The next thing we need to install for Node is a package manager called Yarn. This is used by Rails to install files for Webpacker. We can install Yarn with this command:

npm install -g yarn

Verify this has been installed by running yarn -v. As of this time of writing, Yarn’s version is 1.22.4. If you see any version number, then that is fine.

Rails

So now you have a working Ruby. What about Rails?

Ruby comes with its own package manager called RubyGems, and this is what we can use to install Rails. Gems are just little bundled-up packages of Ruby code, and Rails is just a gem.

$ gem install rails -v "~> 6.0.0" --no-document

This will install the latest from the 6.0.x branch of Rails and all of its dependencies. Whatever version this installs will be fine for this book. It’ll take a while, as it figures out the dependencies, installs the gems, and then parses and installs documentation. (You can skip the documentation install by running the command with --no-document, eg. gem install rails --no-document. But hey, you might want it one day!)

When it’s done, verify the installation by running rails -v. The output will look something like this:

Rails 6.0.2.1

Hooray!

Starting a new Rails app

Once all that complicated setup is done, starting a new Rails app is trivial. Simply enter in your terminal:

$ rails new my_awesome_app

Which will create a new app using SQLite, in the my_awesome_app directory of your current folder. This will also install Once that’s complete, you can start the Rails server:

$ cd my_awesome_app
$ rails server

Once Puma starts and tells you it has loaded on port 3000, you can open up a browser and visit http://localhost:3000.

You should see "Yay! You’re on Rails!"

Footnotes

© Ryan Bigg, Rebecca Le, Kieran Andrews & Robin Klaus