Categories
IT

How to install Rails 7

Time for a new dev environment for home/hobby.

Environment: Windows 11 Home, Windows Subsystem for Linux, Ubuntu 20.04, ruby 3.1.2, rails 7.3.0.1

This time, I decide not to use a usual virtualization layer like OracleVM VirtualBox or VMworkstation, instead I give a try to the Windows Subsystem for Linux: WSL. Using WSL defaults to a Ubuntu install, which is fine.

The installation will require 7 steps

  1. Install WSL and Ubuntu
  2. Get rbenv
  3. Install ruby-build as a rbenv plugin
  4. Build ruby
  5. Add the bundler gem
  6. Add sqlite
  7. Install rails

1. Install WSL and Ubuntu

Open a PowerShell command prompt as admin:

$ wsl --install

Then reboot your laptop, the “Ubuntu on Windows” is automatically started and prompts for a user/password:

No Ubuntu update is available, but if some updates are available the command to install them is shown, simple and well known:

$ sudo apt update

2. Get rbenv

$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init - bash)"' >> ~/.bashrc
$ exec $SHELL

Note
Since I have installed manually rbenv from git, the way to keep it up to date will be:

$ cd ~/.rbenv; git pull; cd -

3. Install ruby-build as a rbenv plugin

$ mkdir -p "$(rbenv root)"/plugins
$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

You can check the installation of rbenv and ruby-build using the rbenv-doctor script:

curl -fsSL https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-doctor | bash

4. Build ruby

First, I need to add some packages to my Ubuntu system:

$ sudo apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm6 libgdbm-dev libdb-dev uuid-dev

Then build a version of ruby, you can display a list of avaible stable ruby releases:

rbenv install -l
2.6.10
2.7.6
3.0.4
3.1.2
...
Only latest stable releases for each Ruby implementation are shown
...

I decide to go for the latest 3.1.2 that was released in April 2022:

$ rbenv install 3.1.2

The ruby interpreter installs in a subdirectory of ~/.rbenv:

I set this installation as the default version:

$ rbenv global 3.1.2

Then I can check my ruby installation and version using ‘ruby -v’:

$ ruby -v
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux] 

5. Add the bundler gem

The ruby language is augmented by some modules that are named “gems”. gem is also the name of a binary tool that is installed along with ruby and allows to basically add/list/remove/update the gems. Managing the gem requirements of a rails app can be a tedious task. Bundler is a gem that will automatically install the correct gems in the expected versions, it enforces the gem dependencies of the app. Rails is a collection of Ruby gems, which requires the help of bundler to be properly installed.

Bundler gem installation is as simple as:

$ gem install bundler

6. Add sqlite

I need a database server for my rails application. For the purpose of development, sqlite is the easiest and lightest option. Ubuntu does not ship with sqlite installed, it is recommended to add it before the installation of rails:

$ sudo apt install sqlite3

SQLite Browser would be a nice addition to the development environment, unfortunately it is not available in WSL. I don’t understand why as WSL is able to display some graphic Linux apps but I didn’t dig the question.

Update: Nov. 16, 2023

At the time of writing this post, Aug. 12, 2022, there was no way to run SQLite Browser in WSL but now there is a workaroud using WSLg, check SQLite Browser fails to open a DB inside a wsl2 installation

Let’s add the SQLite library anyway that can be useful for future gems:

$ sudo apt install libsqlite3-dev

Production environments will require a more powerful database software like postgresql for instance.

7. Install rails

It it not required anymore to specify the target version of your rails setup after the -v switch, instead you can just type:

$ gem install rails

This will add 35 new gems, rails being the last one of them. Additionally, the documentation for these gems is being generated and indexed. Check your rails installation by displaying its version number:

$ rails -v
Rails 7.0.3.1

Now, I have everything in order to create a rails application. I point your preferred text/code editor to \\WSL$\Ubuntu\home. The required gems will be added when I create my app using rails new <app-name>.


Useful references:

Categories
IT

How to install Rails 6

This post was written on April 2020, the versions Rails 6 and Ubuntu 19.10 are quite obsolete now.

I’ve been enjoying Ruby and Rails for a long time and I wanted to setup a new dev environment.

To be precise, I did not use Ubuntu but Lubuntu

My laptop runs Windows 10, VirtualBox 6.1 and I find that Lubuntu runs a lot smoother than the usual Ubuntu desktop. Yet I give 2 gig of RAM and 2 vCPU to my VM. Moreover, I like the simplicity of the LXQt desktop environment.

As a prerequisite, it is always a good thing to update your current installation:

$ sudo apt update

The installation will require 7 steps

  1. Get rbenv
  2. Install ruby-build as a rbenv plugin
  3. Build ruby
  4. Add the bundler gem
  5. Install rails
  6. Add a database
  7. Add Nodejs and yarn

1. Get rbenv

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

Note
Since I have installed manually rbenv from git, the way to keep it up to date will be:

$ cd ~/.rbenv; git pull; cd -

2. Install ruby-build as a rbenv plugin

$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

You can check the installation of rbenv and ruby-build using the rbenv-doctor script:

curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash

3. Build ruby

First I need to add some packages to my Ubuntu system:

$ sudo apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm6 libgdbm-dev

Then build a version of ruby, I decide to go for the latest 2.7.0 that was released in December 2019:

$ rbenv install 2.7.0

The ruby interpreter installs in a subdirectory of ~/.rbenv:

Downloading ruby-2.7.0.tar.bz2…
-> https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.0.tar.bz2
Installing ruby-2.7.0…
Installed ruby-2.7.0 to /home/jeff/.rbenv/versions/2.7.0

I set this installation as the default version:

$ rbenv global 2.7.0

Then I can check my ruby installation and version using ‘ruby -v’:

$ ruby -v
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux] 

4. Add the bundler gem

The ruby language is augmented by some modules that are named “gems”. gem is also the name of a binary tool that is installed along with ruby and allows to basically add/list/remove/update the gems. Managing the gem requirements of a rails app can be a tedious task. Bundler is a gem that will automatically install the correct gems in the expected versions, it enforces the gem dependencies of the app. Rails is a collection of Ruby gems, which requires the help of bundler to be properly installed.

Bundler gem installation is as simple as:

$ gem install bundler

5. Install rails

Specify the target version of your rails setup after the -v switch, we choose to install the 6.0.2.2 version that was out in March 2020:

$ gem install rails -v 6.0.2.2

This will add 40 new gems, rails being the last one of them. Additionally, the documentation for these gems is being generated and indexed. Check your rails installation by displaying its version number:

$ rails -v
Rails 6.0.2.2

6. Add a database

I need a database server for my rails application. For the purpose of development, sqlite is the easiest and lightest option. Production environments will require a more powerful database software like postgresql for instance. Let’s install the sqlite package in our Ubuntu environment:

$ sudo apt install sqlite3 libsqlite3-dev

SQLite Browser is a nice addition to the development environment, its installation is straightforward:

$ sudo apt install sqlitebrowser

The support for mysql and postgresql can be added so that you have the three DB flavors available for Active Record:

sudo apt-get install mysql-server libmysqlclient-dev postgresql postgresql-client postgresql-contrib libpq-dev

Redis will be required for Action Cable and Active Support
Memcached will be required for Active Support
Imagemagick, ffmpeg and mupdf will be required for Active Storage
Let’s have them all installed:

sudo apt-get install redis-server memcached imagemagick ffmpeg mupdf mupdf-tools

6. Add Nodejs and yarn

$ curl -sL https://deb.nodesource.com/setup_13.x | sudo -E bash -
$ sudo apt-get install -y nodejs
$ curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
$ curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
$ sudo apt-get update && sudo apt-get install yarn

Now, I have everything in order to create a rails application.


Useful references: