Comprehensive Guide for Configuring a Ruby on Rails and Postgres Development Environment on Linux
When it comes to Ruby on Rails development one of the most confusing and many times intimidating concepts is simply how to set up a development environment.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Now you may think that you need a Macintosh computer in order to go and build rails applications and the majority of the Rails developers I know do use Apple products. However, if you have a Windows PC you can build the exact same applications and you can leverage the Linux operating system in order to do it. And one thing that I want to say that I think is actually a good thing when it comes to following this path is that if you want to build and deploy applications nine times out of 10 if you're not using a tool like Heroku for your deployments you're going to have to build out a Linux server and the process that we're going to go through right now is very similar to that.

So if you go through this process then you're going to have a leg up on anyone else because you've already installed the entire rail system directly on a Linux machine. So I definitely recommend that even if you're on a Mac and you could follow along and do everything on your local machine that you still follow these guides because the same process is what you're going to do if you ever build or you need to build out your own server to actually serve the application on the web.

So the very first thing that I'm going to do is I want to install the RVM system. If I go to rvm and this is going to be rvm.io /rvm/install then this is going to allow us to install the ruby version manager on our virtual box. And so this is going to give us the ability to have multiple versions of Ruby and also to nest our dependencies inside of specific Ruby version. So what that means is if you are building rails applications and you want to build using Ruby 2.5 then all of the gems and all of these 3rd party libraries they want to add in in order to do that can be nested inside of a specific version and then imagine that you're a freelancer and you need to go work on an older application. This happens to me quite often and that older application is built in Ruby 2.2.

Well, I can take and use a tool like RVM to simply switch Ruby versions and then it also switches all of those and nested dependencies all those nested gems. It's going to go use the older versions so it's a really nice way being able to manage your dependencies. So first step is a security item where we need to install the GPG keys so this is going to allow us to have a more secure connection to the outside system so I'm going to just triple click on this copy it

large

and now we can open up the terminal and I'm going to increase the font here just to make it a little easier for you to see what's going on and I can now paste this in.

large

And this part of the installation I have had some errors in the past so if you follow along and you run into errors I actually kind of hope I run into some errors so that you can follow along. If they happen to you too then we can go through them together but if not the recommendations that the error messages give are incredibly explicit. Many times they even give you the command to run in case you run into an error.

So right here it appears that it worked

large

but we're not going to actually know until we go on to the next step. So let's switch back move down to the installation and I'm going to go with the second one so if you're curious about what the differences are right here we have just a plain RVM install and then we have one with a specific version of Ruby and then we have some that even install rails and some different components like that. I want to go with this one because I want RVM and then I also want to bring in the latest stable version of Ruby so I'm going to copy this

large

switch over into the terminal and let's paste it and run it and let's see if this works. It's going out and it's grabbing RVM, it's grabbing the specific version of Ruby and here it's asking for my password.

large

So type in whatever your system password is and then it's going to see if it can install that version of Ruby and so far it looks like it is working. If you run into any errors typically the main error that I see here whenever I'm creating a new environment and I run into an error at this stage it usually is related to those GPG keys and so if you run into it usually it will give you or every single time I've had it happen. It will give you an error message it'll say something like it appears that you need to use this other version of GPG please try running this command. Copy it and paste it into your command line and then it should work for you.

Okay. So everything appears like it worked, we didn't have any errors or anything like that.

large

Now if you try typing rvm list here then this is going to say no command of rvm found but that doesn't mean it didn't go through it means that you just need to restart the terminal. So get out, get back into it and now you can just type in the same command. So rvm list and now you can see that that is working.

large

So we have Ruby 2.4.1 on our machine. Now if you want to start up what is called a Repl environment meaning that you want to enter your ruby code directly into the command line. This is something that's helpful for whenever you're building test scripts but also it's very helpful for making sure that we installed Ruby properly. Now if I type irb right here it's going to say the program IRP is currently not installed you can install it by typing this sudo apt install ruby command but do not do that.

large

I knew that our command wouldn't work. I wanted to do it so that I could show you something very specific related to setting up your environment and that is that the default terminal that you get whenever you install ubuntu does not give you all of the permissions that you need in order to work properly. What you need to do is not install this sudo apt install ruby command, they actually just have to come up here and go to the terminal preferences or go to edit then profile preferences

large

and then right here go and click on command and then click on run command as a login Shell then click on close.

large

Now let's restart the terminal once again and so now this should work. So now if I type irb you can see it's working.

large

We can type in any Ruby code here we want and you can also see that it is saying we're using Ruby 2.4.1 so that is perfect.

large

It means rvm has overridden the default system settings for Ruby and now we can dictate what version of Ruby we want to use via rvm. So type control + D to get out of there and now let me also show you how to use a different version of Ruby. So at the time of this recording we have the ruby 2.5 version. So if I want that I can say rvm install 2.5.0 and then it's going to go out and it's going to grab Ruby 2.5 bring it down on to our system and then we can use it. So depending on how fast or slow your connection is and also the speed of the rvm server this could take a few seconds to a few minutes. It appears everything works here.

large

If I type rvm list now you can see that we have Ruby 2.5 and 2.4.1 so that is working nicely.

large

If I type irb you can see that it's now using Ruby 2.5 but if I want to also override the default, so notice here whenever you see this little asterisk what this means is that the default version is Ruby 2.4.1. Let's switch it so that 2.5 becomes the system default, we can do that with this command so I can say rvm --default use ruby-2.5.0 just like this and if I run that the default has been switched so now if I say rvm list. Now, not only is the current version of Ruby 2.5.0 it also is the default, so that is working nicely.

large

We have Ruby installed on our system and we are well on our way to having a full rails environment.

The next thing that we're going to do is install the gems that we need. So that when you're installing gems one thing that you should be aware of is that some gems especially larger ones have a lot of documentation and if you try to install the plain gem then what you're going to run into is a situation where you are going to have very long install times and it's not just for the ruby code it's also for the documentation itself.

So what we can do is type echo "gem: --no-document" >>~/.gemrc and run that command. Now if that code looked really weird to you, all we're doing is we are saying take this code inside of this string. So really adjusts this gem to where it says no document and take that and put it in a file at the root of our system called .gemrc and so you can actually test this out by saying cat ~/.gemrc and you can see that that code has been added.

large

You also could have done the same thing by creating the file and then opening up in whatever text editor that you use and then simply adding that line of code right into it. All that we're doing is we're saying when you install a gem I no longer want you to install the documentation with it and that helps to speed up that install process.

So with all of that in place now let's install our set of gems we need to get rails working. The first one is going to be bundler so I can say gem install bundler and bundler is going to give us the ability to grab outside gems and run a command called bundle install in our Rails app and that's going to bring in all the dependencies so all third-party libraries and everything like that is going to be brought into our application. So we need bundler we also need nokogiri so I'm gonna say gem install nokogiri and nokogiri's probably one of the most popular gems in the Ruby world. It has all kinds of different tools, a lot of them revolve around processing text and whenever the rail systems using generator a lot of times it's going to leverage some of the nokogiri tools in order to do that. And so nokogiri is a very popular one.

It is a very large gem, so once again depending on the speed of your connection and also the ruby gems servers this could take a little while. It looks like it's done now and we know that it's successful because if it says that successfully installed the name and then says which ones were installed or gives a number then that means that that worked.

large

Now we only have one more gem to install. So I'm gonna say gem install rails and now this is going to go out and it's actually going to just install the rails framework. If you weren't aware of it rails is just a package of Ruby code. It's a lot of Ruby code but it's just a regular set of Ruby files that allow you to build web applications so everything there worked and I think we're good to go on our gems.

large

So now that we have that it's time to install the Postgres database so this is going to allow our application to have persistence where we can go and we can save records directly in the database. And then when you come back to a page you're going to have those records there because they're all saved inside of that system database. So let's go out and get it so I'm going to say sudo apt -get install and now we're going to list out the database and then a couple other dependencies that it needs so I'm gonna say postgresql.

As you may have guessed the spelling on these is critical but if you typed something wrong don't worry it will tell you because if you type in a name of a library that doesn't exist it'll throw an error. So postgresql and then space and then postgresql-contrib and then another one let me shrink the font down just a little bit. And so now libpq-dev and I believe I have all of those typed in correctly type run type in your system password. And then when it asks you if you want to continue type the letter y.

sudo apt-get install postgresql postgresql-contrib libpq-dev

large

It's going to go run and if everything installs correctly then we should be good to go with working with the postgres database. Now if you have experience and you've set up a Mac environment then this is going to be a slightly different setup. And so whenever you're using postgres on linux there are a few other steps you need to take. So we actually have to create a postgres user and work with that, so that's what we're going to do now.

So it looks like everything worked, I don't see any errors there.

large

Now that we have that let's create that postgres user. So I'm gonna say sudo and then -u for user postgres and then createuser all one word and then from there -s and the user name is whatever you want to give it. I'm going to say railsdevuser and so it's created that user. That also tells you that postscripts is ready for us to work with.

sudo -u postgres createuser -s railsdevuser

So now we can actually log directly into the postgres database and the way we can do that is by typing sudo -u postgres psq and psql is the command that you use to access the actual database itself which we now have on the system. So if you typed that command in now you see this new kind of prompt.

large

That means that everything is working. It also shows that we have postgres 9.5.12 installed on the system and so now what we can do is create our customized passwords so if you type in the command slash and then password and then type in the user that you want to give the password to. So here I wanna use the same name because this is the same user. Remember up here where I said railsdevuser? So railsdevuser and it's going to ask for the password to give. I'm just going to go with the actual word password and then it's done so as long as you don't get any errors then you've set up the user along with a password and now you can type in \q and now we are back to the regular command prompt.

large

So if everything worked this is now the moment of truth or the first of a few moments of truth we're going to have and so now we can actually create our rails applications. I'm gonna say rails new TestEnvApp and then I'm going to have it skip the testing so I'll say -T and then -d and then we with this one we can just pass in the type of database that we want to create so, in this case, I'm going say postgresql just like this.

rails new TestEnvApp -T -d postgresql

So that's just telling rails that for this application I want to use the postscripts database and now I run this if everything works it should go out and build the entire rails application and so far so good, we haven't run into any errors. So the process that's happening right now is it's going out and it's installing all of the basic gems that rails uses by default right here you can see that its even saying installing pg which is short for postgres so that is now installed because that worked. And so everything here is looking good.

large

If I type ls you can see that we have, if I type cd this TestEnvApp so this is created and now we're in the application if you type LS You can see all of the different files that were created.

large

And before we can test it fully out we need to go and make one last change so I'm going to change into config and then database.yml. And so this is one step that you are going to need to make that's a little bit different than if you're on a Mac. And so if you are using sublime text you can just open that up using Sublime Text or atom or whatever text editor you're using. And I'm in VIM so I'm going to use the letter J to come all the way down to right where it says pool.

large

If you are not familiar with vim I would highly recommend not to use vim in this case, use whatever text editor that you want to use. I recommend sublime for rails development because if not there are a lot of keybindings you have to memorize. So for this case I'm going to add a few configuration items so I'm going to say that I want the host to be local host then I want the username to be whatever my username was so, in this case, it is railsdevuser, and then the password is whatever password you typed in, I literally just typed the word password in, you can type in whatever you typed.

large

Now I'm going to give one caveat before we continue. If you're doing this on an actual server or something like that. This is not what you'd want to do but we're on a local machine so it doesn't matter that we have typed our password in right here because if someone can hack into our system and see this file, or if they even see it on version controller or anything it's not going to matter because this database is only used on our local system.

Now if you're doing this on a server then you're going to want to do something like this where you have an environment variable and then its securely stored on some other place of your system and so no one can see what your production database password is because if they did then they could access all the data in your database. Imagine if someone got the Twitter or the Facebook database passwords they'd be able to simply login just like we did with that psql command and run and see all of the data in the entire system. On your local machine, this is perfectly fine, I just did want to add that as a caveat.

So now I'm going to save this file and now what we can do is test it out. So type rails db:create and if our database is connected then it'll say that it created the database and in this case, it created the development one and the test one.

large

So this is very helpful this tells us that everything's working. If your database was not installed properly then you'd get an error right here because what rails did is it went out to our installation of postgres it said I need to create two tables this one called TestEnvApp_development and then this test one and so everything there looks like it's working. But let's also just run a quick scaffold to triple check that everything is working properly.

So I'm gonna say rails g scaffold and then let's say guide with the title of string.

rails g scaffold Guide title:string

If you've not gone through a rails course before and this is your introduction to it and you're setting up the environment for your first one. All I'm doing here is I'm creating a feature called guide that has a title of string and this is going to create the entire system for us and so this is going to go out. It's going to create the database modification and all of those files.

large

Now type rails db:migrate and this is going to update the database table itself on our system.

large

And now we can finally fully test it out. So type rails s and this has got startup the rails server.

large

Now we can pop over into safari or into Chrome or whatever browser you're using type localhost:3000 in the url bar and that works.

large

Now if you type localhost:3000/guides and open that up now you can see that we have that little guides feature that we just built the scaffold.

large

And if I type new guide and say testing and create guide if that all works this means your entire system is working because right here we have persistence so this is being stored in our postgres database and then everything else inside the rails from the routing to everything else. It means that everything is now working and you can build out any kind of rails application that you want. So great job if you have gone through that you now know how to build out a full rails development environment inside a Linux.

Instructions

sudo apt-get update

https://rvm.io/rvm/install
Get GPG keys (if fails use the recommended command)
\curl -L https://get.rvm.io | bash -s stable --ruby

Edit/Profile Preferences
Select tab: Command
Run command as login shell
restart

Test with command irb
rvm --default use ruby-2.5.0
echo "gem: --no-document" >> ~/.gemrc
gem install bundler
gem install nokogiri
gem install rails
sudo apt-get install postgresql postgresql-contrib libpq-dev
sudo -u postgres createuser -s railsdevuser
sudo -u postgres psql
\password railsdevuser
\q
rails new TestEnvApp -d railsdevuser

config/database.yml
host: localhost
username: pguser
password: pguser_password