Rails Setup on Ubuntu 19.04
Tags: ubuntu, linux, ruby, rails
Published: 2019-06-13 19:31:13

Around this time last year we soft released the first version of [Rivet](https://rivet.es/), a marketplace designed to find and activate resources for the development of creative practices. 

I asked our developer, the amazing [Francis Tseng](https://frnsys.com/), to teach me how to setup a local development environment so that I could *a)* make copy edits to our view templates and *b)* learn how the system works with the (at the time) far fetched idea that one day I could maintain the product and build new features. 

The first challenge was to get my hands on a Unix based operating system. This meant I either had to setup a Linux distro via a Virtual Machine on my Windows machine, or just wipe the hard drive and go full Linux. 

Since I like to do things the hard way, I chose the latter and after many(and i mean many) installs of various linux distros, I've settled, for the moment, with [good old Ubuntu](https://ubuntu.com/). 

As such, today I want to share how to setup a Ruby on Rails local development environment on a Linux machine running Ubuntu 19.04 Disco Dingo: 

##Step 1: Install the latest version of Ruby. 

_There may be a need to work with older versions of the Ruby language depending on your project, no worries, you'll be able to add them after this initial setup with rbenv(see more below)._

First, we need to make sure you have all the dependencies required for both Ruby and Rails.

In your terminal, run: 
```
sudo apt install curl
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
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-get update
sudo apt-get install 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 nodejs yarn
```
##Next:

Install Ruby using the version managing tool called [rbenv](https://github.com/rbenv/rbenv). 

_*Note* you can also use another manager called [rvm](https://rvm.io/), but personally I had an easier experience with rbenv._

In your terminal, punch in: 
```
cd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

rbenv install 2.6.3
rbenv global 2.6.3
ruby -v
```

The last 3 lines are important here because once you have rbenv setup, you can install other versions of ruby and switch between them per your development needs.

The below example will install ruby 2.5.1 and set it as the local version for a specific application you may be working on. _Make sure you're in the working folder of your app._
```
rbenv install 2.5.1
rbenv local 2.5.1

```
Last but not least, install Bundler so you can package ruby gems to your hearts content. 
```
gem install bundler
```
rbenv users need to run `rbenv rehash` in your terminal after installing bundler.


##Step 2 - Configure Git

For versioning control of your code, we have to setup Git with your account info (for this tutorial, we're using a Github account):
```
git config --global color.ui true
git config --global user.name "YOUR NAME"
git config --global user.email "YOUR@EMAIL.com"
ssh-keygen -t rsa -b 4096 -C "YOUR@EMAIL.com"
```
The last step generated a new SSH key for you and your machine. Copy the output of your key from the following command: 
```
cat ~/.ssh/id_rsa.pub
```
and paste it in [your Github settings here](https://github.com/settings/ssh)

Then go back to your terminal to see if it works:
```
ssh -T git@github.com
```
You should see: 
```
Hi (username)! You've successfully authenticated, but GitHub does not provide shell access.
```

##Installing Rails

`gem install rails -v 6.0.0.rc1`

Since we are using `rbenv`, run the following command to make `rails` executable available:

`rbenv rehash`

With Rails installed, you can run the `rails -v` to check if the version installed installed correctly:

`rails -v`
`# Rails 6.0.0.rc1`

##Setup PostgreSQL

Rails comes with SQLlite out of the box, very practical.. but if you want to deploy your app to a service like Heroku, you will need to use PostgreSQL. 

To set up PostgreSQL, first we'll need to add a repository to your machine: 
```
sudo apt install postgresql-11 libpq-dev
```
Next, you'll need to setup a user with permissions to create databases locally. 
```
sudo -u postgres createuser username_of_ur_choice -s

```
You can also (not required) set a password for this user, then: 
```
sudo -u postgres psql
postgres=# \password username_of_ur_choice
```

##And now, drum-roll... 

Lets create your first Rails app! 

```
## Using SQLite
rails new myapp

## Using Postgres
rails new myapp -d postgresql

# Move into the application directory
cd myapp

# Note - If your Postgres is installed with a username/password, modify the
# config/database.yml file in your ruby app folder to include the username/password you defined when setting up postgresql

# Create the database
rake db:create

# Moment of truth
rails server
```

Go to http://localhost:3000 on any browser of your Disco Dingo install to view your locally deployed install of Ruby on Rails! 

You should see: 

![Yay! You're on Rails](https://edgeguides.rubyonrails.org/images/getting_started/rails_welcome.png)


Now that you're up and running, it's time to start building your awesome application/s.