本文解釋瞭如何在Ubuntu 24.04上的Rails版本7上安裝Ruby。它通過提供結構化的方法來組織代碼,處理數據庫交互以及管理Web請求的路由來簡化創建Web應用程序的過程。

在Ubuntu上安裝Ruby,您必須首先安裝和設置Ruby,通常使用 rvm rvm rvm rc=“ https://i0.wp.com/geekrewind.com/wp-content/uploads/2025/03/install-ruby-on-rails-ubuntu.webp?Resize=1024%2C66667&sssssssssl=1″>

Ruby on Rails,您首先需要安裝Ruby。 In this tutorial, we will be installing Ruby version 3.

Run the command below to install Ruby.

sudo apt update
sudo apt install ruby​​3.2

After installing Ruby, run the command below to show the version installed.

ruby​​-v

Install Rails

Now that Ruby is installed, let’s proceed with installing Rails. For this tutorial, we will be using Rails version 7.

Run the command below to install necessary packages to support Rails.

sudo apt install ruby​​-dev libmysqlclient-dev gcc make yarnpkg libxml2 libxml2-dev libxslt-dev libyaml-dev nodejs git

After installing the packages above, use gem to install Rails.

sudo gem install bundler
sudo gem install nokogiri—-use-system-libraries
sudo gem install rails-N–version=’~> 7.0, <8.0'

After Rails is installed, run the command below to show the version installed.

rails-v

Configure Rails app

After installing Ruby and Rails, set up your environment to begin building your app.

In most cases, a database will be needed.

Run the command below to install MariaDB.

Once installed, create a blank database.

First, log on to the database server.

sudo mariadb

Then, create a blank database called [myapp].

CREATE DATABASE myapp CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

用密碼創建一個名為[ myuser ]的用戶。

創建用戶 myuser @localhost’ type_your_password_here ‘;

然後,授予用戶對數據庫的完全訪問。

myApp 上授予所有授予。

保存更改並退出。

flush特權;
exit

設置數據庫後,請繼續進行下一步以為您的應用程序配置它。

運行下面的命令以連接您的數據庫。然後,創建一個簡單的應用程序,稱為[ simpleapp ]。

sudo gem安裝mysql2 – – with-mysql-config=/usr/bin/mysql_config
rails rails raurs new SimpleApp-d mysql

更改simpleApp firsep

以下內容到文件中。輸入上面創建的數據庫。


默認值:&默認
適配器:mysql2
編碼:utf8mb4
池:<%=env.fetch(“ rails_max_threads)/var/run/mysqld/mysqld.sock

開發:
<<: *默認
數據庫: myApp

保存和exit。

接下來,打開應用程序文件。突出顯示的行允許您指定域名。如果您沒有指定域名,則只能使用localhost或IP地址訪問該應用程序。

要求“鐵路/全部”

#需要Gemfile中列出的GEM,包括任何GEMS

您已限制為:test,test,開發或:生產,生產最初生成的導軌>
config.defaults 7.2

#添加域名,如果您要通過域名訪問域名,則僅使用localhost或ip地址。

config.host <<“.ex.exm

導軌生成腳手架testApp名稱:字符串標題:字符串:
rails db:遷移

通過在下面運行命令啟動應用程序。

rails服務器-鍵入=0.0.0.0

應用程序運行,一旦運行,打開瀏覽器並瀏覽服務器IP地址或hostname offersname或port 3000 測試您的應用程序。

http://srv1.example.com:3000/testapps/

應該這樣做!

結論:

摘要中ed,因為這對於鐵軌至關重要。 Rails安裝:使用GEM軟件包管理器安裝Rails版本7。數據庫設置:安裝MariaiaDB,創建數據庫,並為您的Rails應用程序設置用戶。 應用程序配置:創建一個新的Rails應用程序,配置數據庫連接,並在需要時指定其他設置(例如域名)。 運行您的應用程序:啟動Rails Server並通過Web瀏覽器訪問您的應用程序。

通過遵循以下步驟,您將擁有一個功能齊全的軌道環境!

Categories: IT Info