Installing Ruby on Rails on Ubuntu 22.04
The guide describes how to install Ruby on Rails on Ubuntu 22.04.
We will do everything through the console in a few steps.
We add the repository key to the system:
1 2 | gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 \ 7D2BAF1CF37B13E2069D6956105BD0E739499BDB |
We download the RPM installer
1 | curl -sSL https://get.rvm.io | bash -s stable --ruby |
The command will automatically install the required packages and install the Ruby version 2.7.
After completing all the installation, load the RVM to the system with the following command.
1 | source /usr/local/rvm/scripts/rvm |
Then update the RVM to the latest stable version and add the root user to the rvm group.
1 | rvm get stable --autolibs=enable |
1 | usermod -a -G rvm root |
Then check the rvm version with the command below.
1 | rvm version |
We move on to installing Ruby on Rails.
1 | rvm install ruby-2.7.1 |
When the entire installation is complete, set Ruby 2.7.1 as the default version.
1 | rvm --default use ruby-2.7.1 |
Now check the Ruby version with the command below.
1 | ruby --version |
Installation of the necessary Nodejs and Yarn packages
1 | sudo apt install gcc g++ make |
Now add the Nodejs Nodesource repository.
1 | curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - |
Then add the GPG key and the Yarn package manager repository.
1 | curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - |
1 | echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list |
Then update all available packages on your system and install Nodejs and Yarn, using the apt command below.
1 | sudo apt update |
1 | sudo apt install yarn nodejs |
After completing all the installation, check the Nodejs version with the following command.
1 | node --version |
We are updating GEM packages:
1 2 3 4 5 6 7 | gem update --system Otrzymasz pomyślną wiadomość, jak poniżej. Następnie utwórz nową konfigurację dla RubyGem '~/.gemrc' i wyłącz instalację dokumentacji pakietu za pomocą poniższego polecenia. <pre>echo "gem: --no-document" >> ~/.gemrc |
We install rails
1 | gem install rails |
In this tutorial, we will use PostgreSQL as the database for our Rails project. In this step, we will install the PostgreSQL database server provided by the official ubuntu repository.
Install the PostgreSQL database server on Ubuntu 20.04, using the apt command below.
1 | sudo apt install postgresql postgresql-contrib libpq-dev -y |
We run in the system:
1 | systemctl start postgresql |
1 | systemctl enable postgresql |
Then log in to the PostgreSQL shell and create a new role "user_dev" with the password "password" and permissions "createdb" and "login".
1 | sudo -i -u postgres psql |
1 | create role uzytkownik_dev with createdb login password 'haslo'; |
Now see all available users in PostgreSQL, using the following query.
1 | \du |
By default, Ruby on Rails used SQLite as the default database. In this guide, we're going to start a new Rails project, using the PostgreSQL database.
Create a new project 'application’ with the default PostgreSQL database, using the following command 'rails'.
1 | rails new aplikacja -d postgresql |
Now you will get the 'application' project directory, go to the project directory and edit the database configuration 'config / database.yml’ with the nano editor.
1 | cd aplikacja/ |
1 | nano config/database.yml |
In the developer section, add the database configuration as below.
1 2 3 4 | host: localhost port: 5432 username: uzytkownik_dev password: haslo |
Save and close.
Then run the rails command below, to generate and migrate the database for our Rails project, and make sure, that there is no error.
1 | rails db:setup |
1 | rails db:migrate |
After the installation is complete, start the default puma rails web server, using the command below.
1 | rails s -b 0.0.0.0 -p 8080 |
"Application" will run on your public IP address with port "8080".
Now open your web browser and enter the server's IP address with port "8080" in the address bar.
You will see the first Ruby on Rails project.
In this step, we will create a new simple CRUD application using Ruby on Rails and a PostgreSQL database.
Generate a simple CRUD application with ruby scaffold command, as below.
1 | rails g scaffold Post title:string body:text |
Then migrate your database with the following command.
1 | rake db:migrate |
make sure, that there is no error, and then restart the puma server.
1 | rails s -b 0.0.0.0 -p 8080 |
Go back to the web browser and enter the server's IP address with port "8080", followed by the "/ posts" path.
1 | http://10.5.5.32:8080/posts |
Create a new post and enter a title and content.
As a result, a simple CRUD application with a PostgreSQL database was created using Rails.
Ultimately installing and configuring Ruby on Rails with PostgreSQL database on Ubuntu 20.04 has been successfully completed.