Coding Wizardry
  • Home
  • Contact
Coding Wizardry

Explore the magical world of programming

  • Home
  • Contact

How to Install Rails with PostgreSQL on Ubuntu 20.04

by Nicky van Urk November 10, 2020
November 10, 2020

In this post we are going to install Ruby and Ruby on Rails with the PostgreSQL database on Ubuntu 20.04. We are going to install the latest version of Ruby and Rails as of November 2020 which for the former is v2.7.2 and the latter v6.0.3.4. The following steps should be identical when other versions are desired. Let’s start by installing the dependencies.

Installing Dependencies

Install curl and other required packages for Ruby on Rails installation:

$ sudo apt update
$ sudo apt install -y curl gnupg2 dirmngr git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev

Installing Node.js and Yarn

Rails needs Node.js and Yarn installed to manage the application’s JavaScript. Let’s start by installing Node.js:

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

Verify that Node.js installed correctly:

$ node -v
v14.15.0

Next up, installing Yarn:

$ 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

Verify that Yarn installed correctly:

$ yarn -v
1.22.5

Installing Ruby using rbenv

rbenv lets you install and manage versions of Ruby easily. The simplest way to install the rbenv tool is to use the installation shell script. Run the following command to download and execute the script:

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

The script installs rbenv under ~/.rbenv on your system. We’ll have to add ~/.rbenv/bin to the $PATH to access the rbenv command-line utility.

For bash:

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

For Zsh:

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

Verify that rbenv installed correctly:

$ rbenv -v
rbenv 1.1.2-36-g60c9339

We can now use rbenv to install Ruby. To install Ruby on your system and make it the default run the following commands:

$ rbenv install 2.7.2
$ rbenv global 2.7.2

Verify that Ruby installed correctly:

$ ruby -v
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]

The last step is to install bundler, which will make it easier to manage Rails project dependencies. To make the bundler command executable after installation we’ll also have to run the rbenv rehash command. Run the following commands:

$ gem install bundler
$ rbenv rehash

Installing Ruby on Rails

We’ll install Rails using the gem command-line utility, it’s installed automatically when we installed Ruby:

$ gem install rails -v 6.0.3.4
$ rbenv rehash

Verify that Rails installed correctly:

$ rails -v
Rails 6.0.3.4

Installing PostgreSQL

Rails ships with sqlite3 as the default database. Sqlite3 stores its data as a simple file on disk, you’ll probably want to use something more robust. We are going to use PostgreSQL, let’s start by installing it. Run the following commands to install version 13, the latest version as of November 2020:

$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get -y install postgresql-13 postgresql-contrib libpq-dev

Verify that PostgreSQL installed correctly:

$ psql --version
psql (PostgreSQL) 13.0 (Ubuntu 13.0-1.pgdg20.04+1)

Finally, we’ll also want to install the pg gem so that we can interface with PostgreSQL from Ruby code:

$ gem install pg
$ rbenv rehash

Setting Up PostgreSQL

Create a PostgreSQL user for the Rails app we’ll create in the next step. To do this, switch into the postgres user and fire up psql:

$ sudo -u postgres -i
$ psql

Then create a new user (or “role”, as PostgreSQL calls it):

$ create role myapp with createdb login password 'password1';

To go back you type \q to exit psql and exit to return to your main terminal.

Creating a New Rails App

And now for the moment of truth. Let’s create a Rails application. Navigate to a directory on your computer where you want to store the project (I like to use ~/dev) and execute the following commands:

$ rails new myapp -d=postgresql
$ cd myapp

Open up the config/database.yml file in a text editor and under default find the line that says pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> and add the following lines underneath:

host: localhost
username: myapp
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

We are going to need an environment variable to store the database password. The password should match the password used in the previous step. Execute the following commands:

$ echo 'export MYAPP_DATABASE_PASSWORD="password1"' >> ~/.bashrc
$ source ~/.bashrc

Finally, we’ll create our database and launch our application!

$ rails db:create
$ rails server

You can now visit http://localhost:3000 and if everything has gone right you should be greeted by the following website:

linuxrailsrubytutorialubuntu
0 comment
1
FacebookTwitterPinterestLinkedinRedditEmail
next post
My 10 Best Books for Programmers in 2020

Related Posts

How to Set Up CI/CD with GitHub Actions...

December 2, 2020

Leave a Comment Cancel Reply

Save my name, email, and website in this browser for the next time I comment.

Recent Posts

  • How to Set Up CI/CD with GitHub Actions for Rails

    December 2, 2020
  • My 10 Best Books for Programmers in 2020

    November 13, 2020
  • How to Install Rails with PostgreSQL on Ubuntu 20.04

    November 10, 2020

Newsletter

Subscribe to my newsletter for new blog post updates.

Subscribe Newsletter

Subscribe to my newsletter for new blog post updates.

© 2020 Coding Wizardry. All rights reserved.