Introduction to the Rails Development Environment
Okay, so you've been hearing some buzz around full stack web development, and you want to give Ruby on Rails a try. Well, that's fantastic. You see, Ruby on Rails is an amazing web framework that enables developers to go from idea to MVP super fast, and it's only getting better and better.
But hold on, cowboy. Before we start building, we have to set up our development environment. So in this video, we're going to cover everything you need, including Xcode command line tools, which are needed for compiling code and for other tooling needed for a lot of different languages and frameworks. We're also going to cover Homebrew, which is the unofficial macOS package manager. It's pretty amazing. We're going to use it heavily to install all the things we need.
We're also going to cover installing Ruby 3.4.2, which is the programming language that Rails runs on, and it's the latest version of Ruby. And then finally, last but not least, we're going to install Rails 8 itself, which is the latest version of the Ruby on Rails framework. And then we're going to be ready to go. We're going to be up and running, ready to make our web apps.
Okay, I'm ready. Are you ready? I'm ready. Let's get into it.
A Note for Non-macOS Users
Now, before we start, a quick notice to all of our non-MacOS friends out there. Now, whether you're on Linux or Windows, these specific commands will be different, but the steps will conceptually be the same. You'll still need to install Ruby, install Rails, set up a database, and create a new Rails app, which is all the things we're going to be doing in this video.
So even if you're still using a different operating system, stick around because you'll still learn how all these pieces fit together. Right? Yeah. And of course, if you want step by step instructions for your operating system and you want to read out these instructions, you can check out the official Rails guides. We will put a link on the screen right now, maybe, and also in the description for sure. All right, enough talk. Let's get started.
Now, before we install anything Ruby or Rails related, we need to install Xcode command line tools. This includes compilers and other system utilities that Ruby and Rails depends on. Now, if you're a developer, you're going to want to install this anyways because this is necessary for development of all kinds on a macOS machine.
So to install Xcode command line utilities, we want to open up a terminal in macOS and type xcode-select --install. This will install Xcode command line utilities if they aren't already installed on your system. Now for me, I actually already went ahead and installed Xcode command line utilities, but it's as simple as that. You can just follow the prompts, and it's pretty straightforward.
Now, to verify that you have the Xcode command line utilities installed correctly, we can merely type xcode-select -p, and that will give us the path that the Xcode command line utilities are installed into. And for us, that is /Library/Developer/CommandLineTools. Okay, now pat yourselves on the back because Xcode tools are now installed. Let's move on to the next section.
Installing Homebrew, the macOS Package Manager
Okay, step two, the next thing you need to install, if you haven't already, is Homebrew. Now, Homebrew is a package manager for macOS. It makes installing software of all kinds super easy, and we're going to rely on it very heavily for everything we need for all of our development needs.
Now, to install Homebrew, you can go to the Homebrew website, which is brew.sh, and you can copy this script right here and paste it into a terminal to run it. Now, this is a bash script that will copy a file from the internet and then run it in bash to install Homebrew on your system. So let's just go ahead and copy this line here, go back to our terminal and paste it in. And we can see here that we need to enter our password because pasting things from the internet is super safe, right? Well, in this case, it actually very much is. Homebrew is very, very safe and totally okay to install on your system.
Now, Homebrew gives us a quick overview of what this script is going to install, and we just hit Enter, and away it goes. So now we can see here that Homebrew has given us some direction on some extra things we have to do to make sure Homebrew is running correctly on our machine.
Now, this brings us to an important note. If you're running anything newer than macOS Catalina, which was released in 2019, you're going to be using ZSH as your shell. If you want to be sure, you can check that by typing ECHO $SHELL in all caps, and we can see that I'm running /bin/zsh. Now, some other things you could be running would be possibly BASH, that is the bourne again shell, but ZSH is a more modern extension of BASH that has been used in macOS since, like I said, 2019.
And you can see now in the directions for installing Homebrew, we want to echo out to a file called .zprofile. Now, I prefer the .zshrc file, which is where we typically keep all of the commands and configuration that we want to load whenever we enter a new zsh shell, so I'd rather use .zshrc. So I can open up our .zshrc file, which is in our home directory, which is denoted by the tilde, and it is in a dotfile, which is denoted by a dot before the file. Dotfiles typically keep configurations for your system on most Linux and Unix-like machines, which macOS is a Unix-like environment.
So let's open our .zshrc file, and we can see that it wanted us to enter this eval string into our .zshrc file. So let's just copy that from our terminal, go to our .zshrc file, and paste it in there. Okay, great.
So now what's going to happen is every time we enter our ZSH shell, which is what happens whenever we open our terminal, it will evaluate the opt/homebrew/bin/brew shellenv script, which will essentially load Homebrew for us. So we can save this file. And then in order to make sure we have these settings correctly set in our shell environment, we could exit this terminal, but I prefer to just source our .zshrc file that we just made. So now that we've sourced our .zshrc file, we should have all the things in this configuration file available to us. So let's run brew -v to see if it's installed correctly. And what do you know? It is. Okay, great. We've just installed Homebrew. Now, let's move on.
Installing Essential Ruby Dependencies
Okay, so for step number three, before we actually install Ruby, we're going to need a few system libraries that Ruby depends on. Now, if you don't install these, then you would be prompted for them later, so it's better to just install them and get them out of the way.
What we're going to want to do is use our newly installed Homebrew and install openssl@3, which is the third version of OpenSSL. We want to install libyaml. We're also going to want to install gmp and rust. So what are these programs?
OpenSSL is used for secure connections for things like HTTPS, and a lot of things rely on OpenSSL. Next up is LibYAML, which helps Rails handle YAML files, which is a configuration language that Rails relies heavily on. GMP is a math library that is required for some gems, and Rust is a programming language. So let's go ahead and install these through Homebrew. And when they're all said and done, we have all of our Ruby dependencies installed. Pretty easy. Okay, and just like that, using Homebrew, all of our Ruby dependencies are installed. Awesome. Let's move on.
Installing Ruby with the mises Version Manager
Okay, so we have Homebrew, we have all of our system dependencies installed. We're finally ready to install Ruby. But before we install Ruby manually, we should install something that helps us manage Ruby versions.
Now you see a typical thing in Ruby, much like other programming languages and development tools, would be to install a version manager to help you manage different versions of a programming language or a development tool. And for us, a fantastic Ruby version manager is one called mises. Now mises is really great, and it's super easy to set up, and it helps you manage your Ruby versions, where sometimes in different projects, you could be using version 3.4.2, and then other projects are using version 3.1 or something like that. It's great to be able to switch through these versions really easily instead of manually having to switch them yourself.
So mises allows us to switch easily between different versions of Ruby, and it also supports things like Node.js, Python, and other stuff. So mises is pretty cool. Check out the documentation if you're more interested. But for us, we are going to copy the shell script, which seems to be a running theme here. To install mises, we can just copy this curl command that will pipe whatever it installs through sh, which will run a bash script that it installs through curl. And now we have mises installed. Wow, that was pretty easy.
And much like Homebrew, mises is going to want us to put an entry in our .zshrc file to make sure it is working correctly on our system. So whenever we start up our shell, whenever we open a terminal, it will call this Activate ZSH command in this mises script. So let's just copy this line and paste it in our .zshrc file, which we opened earlier in our editor. And once that is pasted and saved, we can now source our .zshrc file. And there we go. Now, mises is working.
So let's just type mises now. And, yes, mises is definitely installed and running correctly. So now let's install Ruby 3.4.2 on our system. We can use mises to install Ruby and use it globally. We call mises use, and we pass the -g flag so that when it is installed, this version of Ruby will be used globally in our machine, and we can install Ruby at version 3.4.2. Okay, now mises is installing Ruby, and under the hood, it's using all of the things that we've just installed through Homebrew and everything else to download, and to then use Ruby 3.4.2 on our system.
Okay, now mises has installed Ruby 3.4.2, and if we type ruby -v, we should get version 3.4.2 because after installing it, mises should be using Ruby globally at this version. And of course, there we go. It is now Ruby 3.4.2. Awesome. Okay, now we are ready to install Rails itself.
Understanding and Installing Rails as a Ruby Gem
But before we get into Rails, let's talk about something. You see, Rails is a Ruby gem. But what's a Ruby gem? Now, you can think of gems as plugins or libraries that add extra functionality to Ruby. Instead of writing everything from scratch, you can install a gem to reuse code that someone else has already written. If you're familiar with something like Node or JavaScript, then you can imagine that a gem is similar to an NPM package.
Now, some gems will help with handling dates, some make it easier to send emails, and some, like Rails, are entire frameworks that help you build web applications. So let's go ahead and install Rails. So we know that we are currently using Ruby 3.4.2, so all we have to do to install a gem for Ruby is type the gem install command, and we want to install rails. It's as simple as that, really. And once it's done, we can verify the installation by checking the version of Rails.
Okay, now that Rails has finished installing, we can check our version by typing rails -v. And of course, we're on Rails 8.0.2, which is the latest release of Rails 8 at this time. And we're now ready to go. Rails is installed.
A Quick Note on Databases
Okay, so really quick, let's talk about databases. You see, by default, Rails uses SQLite, which is a lightweight built-in database that works right out of the box. For small projects or prototypes, SQLite is perfect because it requires zero setup. But there's a possibility you may want to use another database.
If you want to use another database like PostgreSQL or MySQL, then there's no problem there. Whenever you want to switch, you can create a Rails app by either doing something like rails new myapp and passing in the -d flag for database. You can type postgresql if you want to use PostgreSQL or mysql if you want to set up your application to use MySQL as the database. But for now, for our app, we're going to roll with SQLite because SQLite is awesome.
Creating and Running Your First Rails App
Okay, gang, we're here. I think we're ready. Let's create a new Rails application. To create a new Rails app, all you have to do is type rails new and the name of your application. In this example, we'll just type MyApp because I'm not very creative, and this will do for now.
Okay, so we just created a new Rails application, but what the heck just happened? Well, you can see Rails, the gem, when we used it to create a new app, created all of these folders and all of these files. We can see if we go into the myapp directory and open our editor within this directory. And you can see Rails created this folder with all the files that are necessary for running Rails. It also set up SQLite as the database. And we can see in here we have our database set to storage/development.sqlite3, and it also automatically installed a whole bunch of gems by running bundle install.
Now, Bundler is another gem that actually manages the installation of other gems in your projects, which we will talk about later.
Okay, so now let's run our application. We can go into our application folder, which is myapp, and we can just type rails s, and this will start our server for Ruby on Rails. So now if we go to our browser and we go to localhost:3000, we can see that we have a Ruby on Rails welcome page.
Congratulations! You did it. You started a new Rails application. Look at you, you awesome web developer, you.
What's Next?
Okay, so this setup was just the beginning. Next up, we're going to be diving hands-on into a series we're calling Rails New, where we break down how Rails makes it ridiculously easy to go from idea to MVP and beyond. So make sure to subscribe so you don't miss this series. We are just getting started. Thanks, nerds.