Install Ruby on Rails on Debian 12

I recently tried to *[Install Ruby on Rails on Debian 12](https://docs.vultr.com/how-to-install-ruby-on-rails-on-debian-12)* for a project, and while I eventually got it working, the process was far from smooth. Here’s what I did, along with the issues I faced, in case it helps others avoid the same pitfalls.

---

### **Step 1: Install Ruby**
I started by installing Ruby using the default package manager, but ran into version issues. The default Ruby version on Debian 12 was too old for Rails, so I had to remove it and install a newer version using `rbenv`. Here’s what I did:

1. **Remove the default Ruby (if installed):**
   ```bash
   sudo apt remove ruby
   ```

2. **Install dependencies for rbenv:**
   ```bash
   sudo apt install git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libncurses5-dev libffi-dev libgdbm-dev
   ```

3. **Install rbenv and Ruby:**
   ```bash
   curl -fsSL https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-installer | bash
   echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
   echo 'eval "$(rbenv init -)"' >> ~/.bashrc
   source ~/.bashrc
   rbenv install 3.2.2  # Replace with the version you need
   rbenv global 3.2.2
   ```

   **Problem:** After installing Ruby, running `ruby -v` gave me an error about missing libraries. I had to manually install `libssl-dev` and `libreadline-dev` to fix it.


### **Step 2: Install Rails**
Once Ruby was installed, I tried to install Rails using `gem install rails`, but it failed due to permission issues. Here’s how I fixed it:

1. **Install Rails:**
   ```bash
   gem install rails
   ```

   **Problem:** The command failed with a permission error. I had to use `--user-install` to install Rails locally:
   ```bash
   gem install rails --user-install
   ```

2. **Add gems to PATH:**
   I added the following to my `.bashrc` to ensure Rails commands were recognized:
   ```bash
   export PATH="$HOME/.gem/ruby/3.2.2/bin:$PATH"
   source ~/.bashrc
   ```

---

### **Step 3: Set Up a Rails Project**
I created a new Rails project, but ran into database configuration issues:

1. **Create a new project:**
   ```bash
   rails new myapp
   ```

   **Problem:** The `rails new` command failed because `bundler` wasn’t installed. I had to install it manually:
   ```bash
   gem install bundler
   ```

2. **Install dependencies:**
   After fixing `bundler`, I ran:
   ```bash
   bundle install
   ```

   **Problem:** The `pg` gem (for PostgreSQL) failed to install because I didn’t have the PostgreSQL development libraries. I fixed it by running:
   ```bash
   sudo apt install libpq-dev
   ```

---

### **Step 4: Run the Rails Server**
Finally, I tried to run the Rails server, but it wouldn’t start:

1. **Start the server:**
   ```bash
   rails server
   ```

   **Problem:** The server failed to start because JavaScript runtime was missing. I installed Node.js to fix it:
   ```bash
   sudo apt install nodejs
   ```


### **Conclusion**
While I eventually managed to *Install Ruby on Rails on Debian 12*, the process was frustrating due to missing dependencies and permission issues. If you’re trying this, make sure to:
- Use `rbenv` for Ruby installation.
- Install all necessary development libraries (e.g., `libssl-dev`, `libpq-dev`, `nodejs`).
- Use `--user-install` if you encounter permission errors.