30 C
Jaipur
Sunday, May 9, 2021

How to Install Ruby on Rails on Ubuntu 20.04

Must read

Ruby and Rails commonly known as “Rails” is an open-source web application framework. Ruby and Rails framework is written in Ruby programming language which helps you to create highly powerful websites and applications. Rails is a server-side web application framework that follows the MVC concept.

In the previous article, we discussed Ruby installation on Ubuntu 20.04. In this article, we are going to learn how to install the Ruby and Rails framework on Ubuntu 20.04.

Prerequisites

  1. Ubuntu server 20.04
  2. A non-root user with Sudo privileges
  3. Internet connection to download packages

Install Ruby on Ubuntu

There are many ways to install Ruby on Ubuntu 20.04. In this article, we are going to use Rbenv. You will need another tool ruby-build to install Ruby.

Use the following command to update your Ubuntu 20.04.

$ sudo apt update

Use the following command to install Ruby dependency packages.

$ sudo apt install git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential        libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev

After the complete installation of dependency packages, run the following command to clone both Rbenv and Ruby-build repositories at GitHub.

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

To set path in .bashrc, run the following command:

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

Now you can install any available version of Ruby using Rbenv. To list available Ruby version, run the following command:

$ rbenv install -l

You will get output as:

Listing available Ruby version

You can install any version of Ruby using Rbenv. In this example, I am going to install Ruby 3.0.0. To choose any version compatible with your system, replace version number.

$ rbenv install 3.0.0

After completing installation, run the following command to set global variable:

$ rbenv global 3.0.0

To verify installation, run the following command:

$ ruby -v

You will get output similar as:

output: 

ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]

Run the following command to install bundler .

$ gem install bundler

Install NodeJS on Ubuntu 20.04

Rails requires Javascript runtime to build application so you need to install latest version of Node.js.

Run the following command to install NodeJS version 12 Repositories. If you want the latest version, you can install NodeJS version 14.

$ curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -

Execute following command to install Nodejs.

$ sudo apt install -y nodejs

To check nodejs version, run the following command:

$ node -v

You will get the output as:

output:

v12.20.2

After installing nodejs, you need to install yarn package manager. Run the following command to add gpg key for yarn package manager.

$ curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -

Add repository for yarn package using following command:

$ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

Finally run following command to install yarn package manager .

$ sudo apt-get update && sudo apt-get install yarn

Install Rails on Ubuntu

Rails can be installed using the gem install command. Execute the following command to install Rails on ubuntu 20.04.

$ gem install rails

You can use gem install rails –version=<version> command to install specific version of Rails. Use the following command to verify Rails installation on your system.

$ rails -v

As of writing this article the latest version of rails is 6.1.3.

output:

Rails 6.1.3

Install PostgreSQL

In this article, we are going to use PostgreSQL to run the Rails application. In ubuntu 20.04, PostgreSQL is available in the default repository.

Run the following command to install Postgres and dependencies:

$ sudo apt install postgresql postgresql-contrib libpq-dev -y

Once the installation is completed, start and enable postgres service to start automatically after reboot.

$ sudo systemctl start postgresql
$ sudo systemctl enable postgresql

Postgres service has been started and enabled. Now login to the PostgreSQL shell and create a new role with a password and the privileges createdb and login. In this example I am going to use ‘linoxide_dev’ as a role.

$ sudo -i -u postgres psql

To create role and grant permission run the following command:

$ create role linoxide_dev with createdb login password 'mystrongpassword';

To list all available users on PostgreSQL, run the following command:

$ du

You will get the role named ‘linoxide-dev’ on the PostgreSQL list users.

Displaying user list in PostgreSQL

Start New Ruby on Rails Project with PostgreSQL

Ruby on Rails uses SQLite as the default database. In this article, we are going to start new Rails project using PostgreSQL database.

In this article, I am going to create a new application ‘linoxideapp’ with the default database PostgreSQL using the following ‘rails’ command.

$ rails new linoxideapp -d postgresql

You will get output similar as:

Creating new project using Ruby on Rails

Now you will get the project directory ‘linoxideapp‘, go to the project directory, and modify the database configuration file ‘config/database.yml‘ using any text editor.

$ cd linoxideapp/
$ vi config/database.yml

In the development section and use your database details. In this example, I have used default credentials created in above steps. You can have your own assumption.

   host: localhost
   port: 5432
   username: linoxide_dev
   password: mystrongpassword

Now go to the testing section and modify PostgreSQL database configuration as below:

   host: localhost
   port: 5432
   username: linoxide_dev
   password: mystrongpassword

Save the file and exit.

Now, run the rails command to generate and migrate database for our new Rails project.

$ rails db:setup
$ rails db: migrate

You will get output similar as:

DB setup and migrate for Project

Now its time to host application. Start the default rails web server using the following command:

$ rails s -b 0.0.0.0 -p 8080

Your rails application will listen to the port ‘8080’ for an incoming connection. The linoxideapp project will run with the port ‘8080’.

Running default ruby web server

Now open your browser and type your server IP address with port ‘8080’ on the address bar.

http://your-server-ip:8080

You will get the default web page of the Ruby on Rails as:

Ruby on Rails default web page

The output shows the first project is running successfully.

Conclusion

In this article, you have learned how to install Ruby using Rbenv with application compatible version on Ubuntu 20.04. You got knowledge about PostgreSQL database installation and configuration. Finally, you got an idea on creating Rails project and hosting an application using PostgreSQL. Thank you for reading and feedbacks are appreciated.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article