Linux Commands and Scripts

How to Install Rails and Nginx on Ubuntu 20.04

In this article, we will see how it is easy to install Rails and Nginx on Ubuntu 20.04 with passenger.

Ruby on Rails, or Rails, is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller framework, providing default structures for a database, a web service, and web pages.

Ruby originated in Japan and now it is gaining popularity in US and Europe as well. The following factors contribute towards its popularity −

  • Easy to learn
  • Open source (very liberal license)
  • Rich libraries
  • Very easy to extend
  • Truly object-oriented
  • Less coding with fewer bugs
  • Helpful community

As a result, Rails features a routing system that is independent of the web server. The web server is used to render the content only. The choice for that would be Nginx. Nginx is fast webserver with a strong focus on high concurrency, high performance and low memory usage.

Let’s proceed with the installation.

1. Keep the server up to date

Before we proceed with installation, we should make sure that our system repositories are up to date:

# apt-get update -y

2. Install Ruby version manager

Once we are up to date with the latest available packages, the next step is to install Ruby Version Manager. It is application that allows to manage several different ruby versions easily, we can install RVM and then load it with:

# gpg –keyserver hkp://pool.sks-keyservers.net –recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

# curl -sSL https://get.rvm.io | bash -s stable –ruby

# source /usr/local/rvm/scripts/rvm

3. Install all dependencies from RVM

We must make sure that we have all dependencies from RVM. To make sure that we have all required dependencies, we execute the following command:

# rvm requirements

4. Install and configure Ruby

Following two commands will install Ruby and set the system to use version 1.9.3 by default.

# rvm install 2.7.2
# rvm use 2.7.2 –default

5. Verify all components for Ruby on Rails

The next step is to make sure we have all components for Ruby on Rails. Ruby Gems is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries, a tool designed to easily manage the installation of gems, and a server for distributing them, we can install it with this command and then use it to install Rails:

# rvm rubygems current
# gem install rails

This process could take some time, but after it finish, Ruby on Rails is installed.

6. Install Passenger

Next, we need to make sure that we can easily deploy Ruby on rails to any web server. We will install and use Passenger for that. It will serve as interface or bridge for communication between Ruby and the web server, you can install it with the following command:

# gem install passenger

Once passenger is installed, the rest of the required setup is fully automated. We execute the command:

# rvmsudo passenger-install-nginx-module

Once we do this, it checks for all dependencies automatically and install those that are missing. If some manual user action is required, Passenger will tell us, as well as give us detailed instructions how to do it.

Now we need to configure nginx to “talk” to Passenger. In order to do that, we need to open the nginx configuration file (/opt/nginx/conf/nginx.conf), using our favorite editor and add the following:

server {
listen 80;
server_name example.com;
passenger_enabled on;
root /var/www/rails_app/public;
}

7. Install Node.js

In order to create our rails app, we need to install Node.js first

# sudo apt-get install nodejs

Once that is that, we should go to our directory (in this case it is /var/www/rails_app/public) and create the application.

8. Start Nginx

After all this is installed and configured, we simply need to start Nginx.

# rails new my_first_rails_app
# sudo service nginx start

We can try and access our new Ruby on Rails application using our browser. It seems that it was much easier to setup the environment and create our first Ruby on Rails project.

We have seen how it is easy to install Rails and Nginx on Ubuntu 20.04 with passenger.

[Need assistance to fix this error or install tools? We’ll help you.]

Related Articles