In this article I will show you how to set up a CentOS 7 development machine for the Rails development. I won't cover the actual CentOS install here, but I will cover everything that comes after. This article will cover using
ruby-build for ruby version management. Note that there are some prerequisites. You must have root access to the machine that you are using, or you must be able to use the
sudo command as we will need to install a few system packages.
Step 1: Update your system.
It's important to ensure your system is up to date. To do this, run the
yum update command like so:
$ yum update -y
Now we need to install a few dependencies that will allow us to install
mini_racer. Run the command below to install these:
$ sudo yum install git make gcc gcc-c++ zlib-devel sqlite-devel readline-devel bzip2 openssl-devel
Step 2: Install rbenv and ruby-build
The installation of rbenv is simple. Run the following commands below:
git clone https://github.com/rbenv/rbenv.git ~/.rbenv cd ~/.rbenv && src/configure && make -C src && cd ~ echo 'export PATH="~/.rbenv/bin:$PATH"' >> ~/.bash_profile echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
Next, we install
ruby-build. This one is simple as well:
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
Now, at this point, let's reboot. This will allow any package/kernel updates to take effect, and once we log back in we will be ready to go:
Step 3: Install Ruby
After the system reboots, it's time to install Ruby and Rails. As of this writing, the latest version of Ruby is version
2.5.1, so I'll use that for demonstration purposes, but you can install whatever version you want. To install, simply run the command below, replacing
2.5.1 with the version of your choice.
rbenv install 2.5.1
Assuming you don't have any problems, Ruby 2.5.1, should now be installed, however, we need to make it our active version of Ruby. To do this, run the command below:
rbenv global 2.5.1
Now let's verify that Ruby is correctly installed. Run the command below and you should get output similar to below:
ruby --version ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
What if you have an application that needs another version of Ruby? For instance, you have a legacy application that only runs on 2.3 due to compatibility issues. With rbenv, you can simply run the following commands to do this:
$ rbenv install 2.3.7 $ cd /path/to/your/app $ rbenv local 2.3.7
If you run a
ruby --version while in your application's path, you should get output similar to below:
ruby 2.3.7p456 (2018-03-28 revision 63024) [x86-64-linux]
Note that this will also create a .ruby-version file that you can check into source control, so other developers using rbenv can use the same version. There are other uses for this file as well that will be covered in another article.
Step 4: Configure the firewall
Chances are firewalld is enabled. If this is the case, you'll want to add a port for port 3000 if you wish to access the development server from outside the machine, for instance, in the case of VMs. To do this, simply run:
sudo firewall-cmd --zone=public --permanent --add-port 3000/tcp --reload
Installing Database Clients and Servers
In this section we will show you how to install MySQL and PostgreSQL clients and servers. Note that MySQL is actually MariaDB on CentOS 7, which is a fork. However it works perfectly fine for our needs.
CentOS 7 uses the MariaDB fork of MySQL. This should cause no issues should you decide to go with this approach.
Installation of the Client
To install the client and gem, simply run the commands below:
sudo yum install mariadb-devel gem install mysql2
The commands should complete without error. You may then add the
gem 'mysql2' line to your Gemfile.
Installation of the Server
If you need the server, simply run the commands below, first, install the mariadb-server:
sudo yum install mariadb-server
Next, enable it as a service and start it:
sudo systemctl enable mariadb sudo systemctl start mariadb
Finally, to finish setup, run
mysql_secure_installation, you will be prompted with the prompts below, respond appropriately (Note: I skipped the text here and only included the prompts):
Enter current password for root (enter for none): (push enter) Set root password [Y/n] (push enter, defaults to capital letter, in this case Y for yes) New password: (enter a good root password) Re-enter new password: (confirm your new password) Remove anonymous users? [Y/n] (push enter) Remove test database and access to it? [Y/n] (push enter) Reload privilege tables now? [Y/n] (push enter)
MariaDB is now set up and available for use.
Installing PostgreSQL is pretty easy as well.
Installing the Client
Simply run the commands below to install the client and the gem:
sudo yum install postgresql-devel gem install pg
The commands should complete without error. You may now use the
gem 'pg' line in your Gemfile.
Installing the Server
Installing the server is pretty easy as well, simply run the commands below:
sudo yum install postgresql-server sudo postgresql-setup initdb sudo systemctl enable postgresql sudo systemctl start postgresql
PostgreSQL should now be available for use. Have any further thoughts for expansion on this article? Find any problems with these steps? Be sure to leave a comment below! Thanks for reading!