0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Rails】環境構築時のSQLite3のエラー(Windows/macOS対応)

Last updated at Posted at 2025-08-01

1. エラー内容と原因

❗ エラー内容

コマンドプロンプト
LoadError: cannot load such file -- sqlite3/sqlite3_native
LoadError: 指定されたプロシージャが見つかりません。

🔍 原因

Railsが sqlite3 を読み込もうとした際に、必要なネイティブファイルを見つけられずに落ちるエラーです。特にWindowsでは以下のような理由で発生しやすくなっています:

  • sqlite3 は Cで書かれた拡張を含むため、ビルドや環境依存が強い
  • .so, .dll が読み込めない
  • Gemのバージョン不整合

解決策:PostgreSQLに移行する!

SQLite3のエラーは根本的には解消しづらいため、PostgreSQLを使うほうが確実で安定します。
本番環境(HerokuやRender)でも標準的に使われており、SQLiteは非推奨です。

2. PostgreSQLのインストール

🪟 Windowsユーザー

1. 以下のリンクからダウンロードして下さい!

👉 https://www.enterprisedb.com/downloads/postgres-postgresql-downloads

2. バージョン15.13 Windows x86-64 のインストーラーを選択

次はPostgreSQLをインストールしていきます!

3. PostgreSQLをインストール

  1. インストール先はデフォルトでOK
  2. パスワード: postgres
  3. ポート番号はデフォルトの 5432
  4. StackBuilder:キャンセルでOK

🍎 macOSユーザー(Homebrew)

ターミナル
brew install postgresql@15
brew services start postgresql@15

起動確認:

ターミナル
psql -U postgres

3. Railsアプリの作成(PostgreSQL指定)

ターミナル
rails _〇.〇.〇_ new (アプリ名) --database=postgresql
cd (アプリ名)
bundle install

_〇.〇.〇_ には、rails -v をして 7.2.2.1 など自分のバージョンを入れてください!

※ macOSで pg_config が見つからない場合は以下を追加:

ターミナル
bundle config --local build.pg --with-opt-dir="$(brew --prefix postgresql@15)"
bundle install

4. config/database.ymlの設定(共通)

config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  username: postgres # 追加
  password: postgres # 追加
  host: localhost # 追加
  port: 5432 # 追加
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

5. Gemfileを編集

Gemfile
# gem 'sqlite3' コメントアウトする(8行目付近)

# SQLite3 → 開発用(37行目付近)
group :development, :test do
  gem 'pg' # 追加
end

6. データベース作成・起動(共通)

ターミナル
rails db:create
rails db:migrate
rails s

http://localhost:3000 を開いて初期画面が出れば成功 🎉

まとめ:こんな時はPostgreSQLに移行!

cannot load such file -- sqlite3/sqlite3_native SQLiteのC拡張が読み込めない ✅ PostgreSQLへ移行
指定されたプロシージャが見つかりません Windowsでビルド失敗 ✅ PostgreSQLへ移行
macOSで pg_config not found などのエラー Homebrewの設定が不足 bundle config で対応
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?