The title is fishing.
The subject is too big and I'm a little nervous. Now, let's talk about what the title says.
gem install xxx
command will basically be installed in the Ruby environment.bundler
, I think the situation of executing gem install xxx
will be limited.gem install
and bundle install
commandsThere 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.
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 **.
When I build a Rails project, I take the following steps: First, I will give an overview and then write specific commands.
rbenv
bundle init
command under the project directory to create a Gemfile
file.Gemfile
file and enable (comment in) the lines that are gem'rails'
bundle install --path = vendor / bundle
to install the gem under a specific pathrails s
commandNow, 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.
#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 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.
I will explain what kind of situation it was
First, the state with the library that is "preparation" is shown as a figure.
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
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.
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.
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.
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.
Recommended Posts