RVM viết tắt của từ Ruby Version Manager nghĩa là quản lý version Ruby, giúp chúng ta có thể cài nhiều bản ruby cùng 1 lúc
Trên ubutun ta có thể cài theo hướng dẫn : //github.com/rvm/ubuntu_rvm
Chạy rvm version để kiểm tra chúng ta đã cài đặt thành công
Sau khi cài xong rvm ta cài ruby:
rvm install ruby-2.7.1 => 2.7.1: version ruby muốn cài, bạn có thể cài nhiều bản ruby version khác nhau
rvm use 2.4.2 --default => --default là set version ruby mặc định trong máy bạn, ở đây 2.4.2 là ruby version mặc định
ruby -v => check xem ruby version hiện tại là bao nhiêu. Tùy từng dự án sẽ sử dụng ruby version khác nhau
Sử dụng RVM để cài sẽ có trường hợp bạn bật terminal lên ruby nó k nhận : ruby -v nó ra not found
Giải quyết bằng cách : //askubuntu.com/questions/624610/ruby-command-not-recognized-after-installing-it-with-rvm
Solution 1: Using ~/.bashrc file
Open your ~/.bashrc file and put these two lines [or last one] there.
Load RVM into a shell session as a function
[[ -s ""$HOME/.rvm/scripts/rvm"" ]] && source ""$HOME/.rvm/scripts/rvm""
Solution 2: Make the regular shell session as login shell
Or you can turn on your virtual terminal’s preference to consider the shell as Login Shell. The settings can be found in
Gnome Terminal: Menu > Edit > Profile Preference > Command Tab > Run command as a login shell
Mate Terminal: Menu > Edit > Profile Preference > Title & Command Tab > Run command as a login shell
Xfce4 Terminal: Menu > Edit > Preference > General Tab > Run command as login shell"
Với Rails chúng ta cần JavaScript Runtime để compile asset pipeline của Rails, trước khi cài Rails các bạn cần cài thêm Nodejs và Yarn
sudo apt install gcc g++ make
curl -sL //deb.nodesource.com/setup_14.x | sudo -E bash -
curl -sL //dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb //dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
sudo apt install yarn nodejs
check xem đã cài thành công chưa bằng cách
node --version
yarn --version
Giờ chúng ta có thể cài đặt Rails
Sau khi cài đặt hoàn tất check Rails version
Cài đặt Mysql
Sử dụng MySQL Cài đặt theo hướng dẫn : //linuxize.com/post/how-to-install-mysql-on-ubuntu-18-04/
Cài đặt git
Tạo tài khoản trên //github.com/ Cài đặt git :
sudo apt-get install git-core
Config git:
git config --global color.ui true
git config --global user.name ""Your Name""
git config --global user.email newuser@example.com
newuser@example.com: email tài khoản github bạn đăng ký
Vậy ta đã cài đặt git và config thành công rồi. bạn có thể kéo code về sử dụng https : bằng cách git clone
[[ -s ""$HOME/.rvm/scripts/rvm"" ]] && source ""$HOME/.rvm/scripts/rvm""
1
Nếu sử dụng https mỗi lần đẩy code lên github bạn sẽ phải nhập user name và password. để giảm thao tác đó chúng ta sẽ sử dụng ssh
Connect máy mình vs github thông qua ssh tham khảo thêm tại : //help.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
cat ~/.ssh/id_rsa.pub
Sau đó copy kết quả của dòng lệnh trên, mở github vào link setting ssh //github.com/settings/keys . Tạo mới ssh key, điền tên và paste kết quả dòng lệnh vừa rồi Vậy là đã xong. check bạn đã kết nối thành công bằng cách
[[ -s ""$HOME/.rvm/scripts/rvm"" ]] && source ""$HOME/.rvm/scripts/rvm""
2
Nếu hiện Hi excid3! You’ve successfully authenticated, but GitHub does not provide shell access. là đã kết nối github bằng ssh thành công rồi đó Giờ ta có thể thực hiện git clone bằng ssh :
git clone git@github.com:project_minh_muon_clone.git
Cài đặt sublime text
Sublime Text khác với các IDE khác ở đặc điểm nhẹ, linh hoạt trong tùy biến và cung cấp nhiều tính năng rất tiện lợi khi lập trình. Mình suggest các bạn dùng phần mềm này để code
We're going to use the Windows Subsystem for Linux [WSL] to accomplish this. This allows you to install a Linux distribution natively on Windows without a virtual machine.
Ruby on Rails will always be deployed to a Linux server, so it's best for us to use the same for development.
Windows allows you to run various Linux operating systems inside of Windows similar to a virtual machine, but natively implemented. We'll use this to install Ruby and run our Rails apps.
You must be running Windows 10 version 2004 and higher [Build 19041 and higher] or Windows 11.
Open Powershell and run:
wsl --install -d Ubuntu
Reboot your computer to finish the installation.
Once initial setup is finished, you will be prompted to create a username and password for your Ubuntu install.
You can search for "Ubuntu" in the Windows Start Menu anytime to open the Ubuntu terminal.
Congrats! You now have Ubuntu installed on Windows with WSL. You'll use this to run your Rails server and other processes for development.
The first step is to install dependencies for compiling Ruby. Open your Terminal and run the following commands to install them.
sudo apt-get update
sudo apt-get install git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev
Next we're going to be installing Ruby using a version manager called ASDF.
The reason we use ASDF over rbenv, rvm or others is that ASDF can manage other languages like Node.js too.
Installing
sudo apt-get update
sudo apt-get install git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev
6 is a simple two step process. First you install
sudo apt-get update
sudo apt-get install git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev
6, and then add it to your shell:
cd
git clone //github.com/excid3/asdf.git ~/.asdf
echo '. "$HOME/.asdf/asdf.sh"' >> ~/.bashrc
echo '. "$HOME/.asdf/completions/asdf.bash"' >> ~/.bashrc
echo 'legacy_version_file = yes' >> ~/.asdfrc
echo 'export EDITOR="code --wait"' >> ~/.bashrc
exec $SHELL
Then we can install ASDF plugins for each language we want to use. For Rails, we can install Ruby and Node.js for our frontend Javascript.
asdf plugin add ruby
asdf plugin add nodejs
Choose the version of Ruby you want to install:
To install Ruby and set the default version, we'll run the following commands:
asdf install ruby 3.3.0
asdf global ruby 3.3.0
# Update to the latest Rubygems version
gem update --system
Confirm the default Ruby version matches the version you just installed.
which ruby
#=> /home/username/.asdf/shims/ruby
ruby -v
#=> 3.3.0
Then we can install the latest Node.js for handling Javascript in our Rails apps:
asdf install nodejs 20.10.0
asdf global nodejs 20.10.0
which node
#=> /home/username/.asdf/shims/node
node -v
#=> 20.10.0
# Install yarn for Rails jsbundling/cssbundling or webpacker
npm install -g yarn
Configuring Git
We'll be using Git for our version control system so we're going to set it up to match our Github account. If you don't already have a Github account, make sure to register. It will come in handy for the future.
Replace my name and email address in the following steps with the ones you used for your Github account.
git config --global color.ui true
git config --global user.name "YOUR NAME"
git config --global user.email "YOUR@EMAIL.com"
ssh-keygen -t ed25519 -C "YOUR@EMAIL.com"
The next step is to take the newly generated SSH key and add it to your Github account. You want to copy and paste the output of the following command and paste it here.
cat ~/.ssh/id_ed25519.pub
Once you've done this, you can check and see if it worked:
ssh -T git@github.com
You should get a message like this:
sudo apt-get update
sudo apt-get install git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev
0
Choose the version of Rails you want to install:
sudo apt-get update
sudo apt-get install git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev
1
Now that you've installed Rails, you can run the
sudo apt-get update
sudo apt-get install git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev
8 command to make sure you have everything installed correctly:
sudo apt-get update
sudo apt-get install git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev
2
If you get a different result for some reason, it means your environment may not be setup properly.
For PostgreSQL, we're going to add a new repository to easily install a recent version of Postgres.
sudo apt-get update
sudo apt-get install git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev
3
You'll need to start postgresql each time you load your WSL environment.
The postgres installation doesn't setup a user for you, so you'll need to follow these steps to create a user with permission to create databases. Feel free to replace
sudo apt-get update
sudo apt-get install git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev
9 with your username.
sudo apt-get update
sudo apt-get install git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev
4
Let's create your first Rails application on Windows!
sudo apt-get update
sudo apt-get install git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev
5
You can now visit //localhost:3000 to view your new website!
Getting an "Access Denied" error?
If you received an error that said
cd
git clone //github.com/excid3/asdf.git ~/.asdf
echo '. "$HOME/.asdf/asdf.sh"' >> ~/.bashrc
echo '. "$HOME/.asdf/completions/asdf.bash"' >> ~/.bashrc
echo 'legacy_version_file = yes' >> ~/.asdfrc
echo 'export EDITOR="code --wait"' >> ~/.bashrc
exec $SHELL
0 then you need to edit the
cd
git clone //github.com/excid3/asdf.git ~/.asdf
echo '. "$HOME/.asdf/asdf.sh"' >> ~/.bashrc
echo '. "$HOME/.asdf/completions/asdf.bash"' >> ~/.bashrc
echo 'legacy_version_file = yes' >> ~/.asdfrc
echo 'export EDITOR="code --wait"' >> ~/.bashrc
exec $SHELL
1 file to match the database username and password.
Editing Code
Install VS Code on Windows and install the WSL extension. This will allow you to edit code in Windows but run commands and extensions in WSL. Read more about Developing in WSL.
That's it! Let us know in the comments below if you run into any issues or have any other protips to share!