Why I have 10 years of experience in Ruby and don't run the [absolutely] gem install rails command

The title is fishing.

The subject is too big and I'm a little nervous. Now, let's talk about what the title says.

Conclusion

Development environment construction dilemma

There are various ways to build a development environment.

The lack of uniform means is both a merit and a demerit. (In the sense of freedom)

However, the problem is, "I'm not sure, but it's on Qiita, and it worked, so I'm okay with that procedure."

The software published on OSS is published in a purpose-specific manner. I would like to show myself how to make good use of it.

Concrete example

For example, there is an article like this

"(Ubuntu) Ruby on rails 6.0 environment construction"

It's important to note that ** this article isn't bad, it's just that I don't follow these steps **.

So how do you do it?

When I build a Rails project, I take the following steps: First, I will give an overview and then write specific commands.

  1. Pre-install the packages (libraries, etc.) needed to build Ruby
  2. Install Ruby using rbenv
  3. Install Node.js required for Rails 6 or later
  4. Create a directory for your project with a suitable name (ʻexample_project`)
  5. Execute the bundle init command under the project directory to create a Gemfile file.
  6. Edit the created Gemfile file and enable (comment in) the lines that are gem'rails'
  7. Run bundle install --path = vendor / bundle to install the gem under a specific path
  8. Launch with the rails s command

Now, let's write a concrete command. The environment assumes Ubuntu Linux.

Start with no Ruby. It is almost the same as "(Ubuntu) Ruby on rails 6.0 environment construction", but the order is different.

Preparation

#Update apt package information
sudo apt update -y
#Update the software installed on Ubuntu
sudo apt upgrade -y
#Install the packages needed to build Ruby via apt
sudo apt install build-essential -y
sudo apt install -y libssl-dev libreadline-dev zlib1g-dev

#When using sqlite3, install the library related to sqlite3
sudo apt install libsqlite3-dev

#For PostgreSQL, execute the following command
sudo apt-get install postgresql-common
sudo apt-get install libpq-dev

#For MySQL or MariaDB, execute the following command
sudo apt-get install libmysqlclient-dev

Install Ruby

#Install rbenv (package management tool)
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
#Set Path in environment variable
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
#Reboot the shell
exec $SHELL -l

# ruby-install build
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

#Install Ruby
rbenv install 2.6.2
rbenv global 2.6.2

#Install the bundler command as a gem
# Ruby 2.From 6 onwards, bundler is included, so the following steps are unnecessary. 2.Required before 5
gem install bundler

At this point, the ruby command, gem command, and bundler command are installed on Ubuntu Linux.

It's still a long way to start developing with Ruby on Rails.

However, in this state, you are ready to start writing Ruby programs.

** The procedure is different from here **

Next, install Node.js, which is required for Rails 6 or later.

# node.js,install npm
sudo apt install -y nodejs npm
#n package installed
sudo npm install n -g
#Install node using n package
sudo n stable
#The old node you put in first.js,Remove npm
sudo apt purge -y nodejs npm
#Re-login
exec $SHELL -l
#Install yarn package
sudo npm install yarn -g
# node.Check if the version of js is the latest
node -v

By following this procedure, the ruby (2.6.2) and gem commands, node, and yarn commands have been installed on Ubuntu Linux.

Let's take a break here.

With these preparations in place, you're ready to run Rails 6.

Next, create a directory for the project you want to create and set up Rails.

#Create a directory for your own project
mkdir example_project
#Move to the created directory
cd example_project

#Run bundle init to create a Gemfile file
bundle init

#Edit the created Gemfile and select "# gem 'rails'Comment in the part
# 「# gem "rails"Where it says "#(sharp)Erase "gem"'rails'In the form of
gedit Gemfile

#bundle install to install rails
bundle install --path vendor/bundle

#Run the rails new command to install all rails gems
bundle exec rails new .
(At this point, an English message like "Do you want to overwrite?" Is displayed, so enter "Y".)

By following these steps, Rails will be available.

bundle exec rails s

** Added on 2020/07/18 **

Please point out in the comments and mention about --path vendor / bundle when doing bundle install. Starting with Bundler 2.1, the --path option is deprecated (deprecated). Please refer to the description here for how to deal with this point.

Procedures to be taken when the warning "[DEPRECATED] The --path flag is deprecated" occurs during bundle install

Commentary

I will explain what kind of situation it was

First, the state with the library that is "preparation" is shown as a figure.

スクリーンショット 2020-07-17 22.48.57.png

These are the libraries installed on Linux. You can't execute Ruby commands with this alone (although it's natural because Ruby is not installed).

In the state of "Install Ruby", it will be in the following state

スクリーンショット 2020-07-17 23.01.28.png

The ruby and gem commands are installed based on the library of "Packages installed on Ubuntu Linux". The reason for the bundler command is that you are installing the bundle command with the gem install bundler command.

The following shows the state where "Node.js" is installed.

スクリーンショット 2020-07-17 23.24.08.png

The dotted line is because it was deleted with the sudo apt purge -y nodejs npm command.

In this state, the ruby, gem, bundler, node, npm, and yarn commands can be executed.

The point to be aware of is that when it comes to Ruby, only the ruby, gem, and bundler commands can be executed, and in JavaScript, the node, npm, and yarn commands can be executed. only.

Next, the state "Create the directory of the project you want to create and set up Rails" is shown.

スクリーンショット 2020-07-17 23.36.13.png

Yes, you now have the ** libraries required for your project ** installed on vendor / bundle.

I think that it is difficult to compare with this alone, so I will show the case where gem install rails is executed.

スクリーンショット 2020-07-17 23.39.03.png

It is shown in red because I want to emphasize it.

What I want to say here is that the gem of rails that executes the ** rails command and the version of" gems related to rails are stored under the vendor / bundle directory "may be different. There is ** </ font>.

This situation cannot be completely reproduced if the version of the gem installed on ruby does not match the version of the gem installed under vendor / bundle (idempotent). It means that it is possible to create a situation where there is no such thing.

On the other hand, the above method (method without gem install rails) does not depend on" programs installed under the HOME directory of Ubuntu Linux ".

Therefore, it is comfortable to make effective use of the bundler command to install the libraries required for the program under a specific directory, and to" keep the Ruby standard gem environment as simple as possible ". I think you can live a life in Ruby.

(But this story probably applies to ecosystems that have a package management system, such as Python.)

Therefore, I think it is important to properly ** understand the meaning of what role each command is in charge of and what it is suitable for, and execute it **.

Please forgive me for the random writing because I am writing with momentum.

Serpentine

  • Why do I have to install Node.js to build a Ruby on Rails environment! The story
  • Starting with Rails 6, webpacker has become the standard
  • webpacker is required to use yarn, which is an ecosystem of node.js
  • It's subtle that it's not complete in one language, but in the long run it's happier to get into the Node.js ecosystem than to worry about JS. I personally interpret it as.

Recommended Posts

Why I have 10 years of experience in Ruby and don't run the [absolutely] gem install rails command
I don't see an error in Rails bundle install ... the solution
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
I changed the Ruby version and now I can't bundle install
Method definition location Summary of how to check When defined in the project and Rails / Gem
[Webpacker] Summary of how to install Bootstrap and jQuery in Rails 6.0
[Ruby on Rails] Change the save destination of gem refile * Note
Erase N + 1 in acts_as_tree of tree structure Gem of Ruby on Rails
Install Rails in the development environment and create a new application
I summarized the flow until implementing simple_calendar in Ruby on Rails.
Things to remember and concepts in the Ruby on Rails tutorial
I want to change the value of Attribute in Selenium of Ruby
[Ruby on Rails] How to log in with only your name and password using the gem devise
What to do if you can't bundle update and bundle install after installing Ruby 3.0.0 in the Rails tutorial
I ran the rails server command on windows10, Ruby2.6.6 and got "cannot load such file --sqlite3 / sqlite3_native"
Implementation of ls command in Ruby
[Rails] Difference in behavior between delegate and has_many-through in the case of one-to-one-to-many
[Ruby] I want to extract only the value of the hash and only the key
[Ruby basics] About the role of true and break in the while statement
I tried to make full use of the CPU core in Ruby
Ruby on Rails When you don't know the cause of rollback when saving.