How the Gemfile Works in Ruby on Rails 6
In this lesson, we are going to dive into the Gemfile in a Ruby on Rails application. Now, Gemfiles can actually be used by any type of Ruby application that’s not specific to Rails. But what we’re going to do here is going to be specific to Rails, and we’re going to walk through exactly what a Gemfile does. And then also, we’re going to see how we can use it.
So we can open up our Gemfile, and you can see a few boilerplate items at the top here. Here, we’re saying that the source for our Gemfile is going to be rubygems. I’m going to show you how we can use RubyGems here in a moment. And then we’re also pointing to GitHub if we happen to ever wanna use a Git-related repository directly. Like we talked about in the last guide, we’ve defined here that we’re using Ruby 2.6.3. Don’t worry if you have a different version of Ruby. As long as you have a version of Ruby that is above two and below three, pretty much everything that we walk through in this course is going to be pretty much the exact same. So let’s move down here.
If you’re not really that familiar with Ruby, this little pound sign here, this, it means that everything after it is going to be a comment, which means it’s not going to get processed. This is simply giving us some instructions. Now, every time that we say gem, what we’re doing is we’re saying that we are pulling in an outside dependency. And so that is what a gem does, is a gem gives you the ability to install outside Ruby code and bring in features other developers have already built and install them directly in our application. So right here, you can see that even Rails itself is a gem. SQLite, which we use for our database, that’s a gem.
Puma, which is what we’re using for our web server, that’s how we can run the application both locally and on production. That’s a gem. And you can go down the list and see each one of the ones that have been added by default. Now, how can you add a new gem? Well, that’s what I’m going to show you how to do here. I’m going to come down all the way to the bottom, and then let’s open up the browser and go to rubygems.org. This is the repository that lists out all of the popular RubyGems that are out there. So you can search for gems, you can research them, and you can find out what code you need to place inside of your Gemfile when you want to use them. So the one that I’m going to use here, it’s actually the one at the very top here, ’cause I typed it in so much, is called devise, and this is going to be something we’re going to install.
We’re going to use it later on in our application. We’re not going to have to even get into what it does for right now. But just search for it, and you can see at the top here, it says devise 4.7.2. That is the version of devise that is current at the time of this filming. Now, if you click on that, you’ll be taken to the devise detail page, where it shows what devise is. And what it is, is it provides flexible authentication for Rails. So what that means is devise is going to give us the ability to very quickly implement user registrations, logins, and anything like that, which is something that if you built that yourself, that can take quite a while.
And so in this guide, we’re going to see how we can install it and how we can use a Gemfile in order to get it going. And later on, probably in the next section, or in the next few sections, we’re actually going to see how to make this work. Now, if you come to the right-hand side here, you can see that it even has some instructions on how to install this. If you wanna install it on your local system, you can run gem install devise, and you can just run this in the console. But if you want to install this in an application in a Gemfile, then you can simply click on this copy-to-clipboard button here, and this is going to give you all the code that you need for the Gemfile.
So let’s open up Visual Studio Code, and I’m going to open up the terminal here, and I’m going to give us a little bit room so you can see both the code and the terminal at the same time. So here, I’m going to paste in the code that was provided to us. So I’m going to hit Save, and let’s walk through. Before I run the installer code, let’s actually run through what this is telling us. So we already talked about this being a gem. Now, the gem here, this is a Ruby function. So we’re telling Ruby when we add this that we want to run this gem method. The first argument of that method is the name.
So as you can tell, with each one of these, like bootsnap or rails or puma, each one of these, this is the official name that is on RubyGems. So you could copy each one of these names, paste them into RubyGems, and then you would see that it has its own dedicated gem page, and you can go look at the documentation. Now, after the name, you got a comma, and then you can place in the versions that you want.
So if you just did gem devise, so if I had a line here that just said gem devise like this, then this would still work because what Ruby is going to do is it’s going to go out to RubyGems, and it’s going to pull in the latest version. Now, the reason why you may not wanna do this is because you always need to be careful of the gem versions that you’re using. Because say that you’ve been building this application out, and it stays there for five years, well, the version that was built five years ago may not be the same version that’s out there today. And because of that, what could happen is you could actually have version conflicts, and it could create some bugs in your application. So it’s smart to define the versions that you are using with each one of these dependencies because then you’re going to be able to control them.
So I’m going to hit Save here in this file. And now, to actually install this so it is available in our system, I’m going to go to the terminal and I’m going to type bundle install. Now, what you can do here, and make sure you’re also in the root of the application, you can type bundle install just as a shortcut. You also could just type bundle. It’s going to be the exact same code. Bundle install and bundle do the same thing.
So in the terminal here, I’m going to type bundle, hit Return, and let’s see exactly what is going on here. Because what is happening is the system is going to go up to RubyGems, it’s going to pull in all of the dependencies that are needed for devise. So it’s going to say, “Okay, devise, I’m bringing down this specific version.” So I want this version, 4.7 to 4.7.2. And then that’s going to bring in that code, but then from there, devise may have its own sets of dependencies. And so what the system is doing is it’s going to find each one of those dependencies, and it’s going to install those. So if you look at the logs right here, you can see that devise was installed.
But then also, if you scroll up, you can see that another library called bcrypt, which this is how you can use encryption for your passwords, and devise does this automatically, this also was installed with a specific version. So that is in kind of a, in a summary how the Gemfile works and then also how you can add new dependencies into your Gemfile. Now, if you wanna see how this updates part of the rest of the application, you can also open up your Gemfile.lock file, and you can see that this is a much larger file.
What this does is it lists out every one of the dependencies in the application along with any nested dependencies that it has. So let’s search here for devise. You can see that devise was installed. This is now in our Gemfile.lock with the specific version of 4.7.2, and then it has five different nested dependencies, and each one of these was brought into our application, so we now have access to bcrypt for the encryption library. We have orm_adapter, which is a way to communicate with the database, then a few other ones, including warden, which is a very powerful authentication library. And so each one of these is now installed in our application, and our Gemfile.lock file is what manages all of those nested dependencies. Now, this lock file is something that you never want to change directly. The only time that you use this Gemfile.lock is to reference something. So if you’re debugging a, say, a dependency seems to be causing an error, you can use this file to see all of the nested dependencies and their versions.
That’s really all you’re ever going to use this file for. You’re always going to make changes, install dependencies, you’re going to change versions all in this Gemfile. And then after you’ve made a change, just run bundle install, and then that is going to update the Gemfile.lock, and your application is going to be able to use those outside libraries.
So in summary, that is how the Gemfile works in a Ruby on Rails application.