How to install multiple version of Ruby and PostgreSQL on Mac OS Catalina πŸ‘¨πŸ»β€πŸ’»

Saurabh Kumar
KhojChakra
Published in
3 min readMay 3, 2020

Hey are you facing problem in switching different ruby versions..?? is multiple tabs in your terminal is showing you different ruby version..?? or whenever you restart your MacBook, and run your project, it is throwing error onbundle install or rails s command..??

If yes, then don’t worry I have a solution for you which works on my machine πŸŽ‰

image source: google

I recently installed Ruby and PostgreSQL on my mac for some project development on Ruby on Rails which requires some old version of ruby and PostgreSQL. Ruby 2.3.3 and PostgreSQL 9.5 to be exact.

The purpose of this post here is to share my experience of installing those and telling about a silly problem I faced for ruby after that on Catalina OS because of my own mistake πŸ™ˆ

so please allow me to start with installation steps first then I will tell you how I fix my problem after installing ruby, in order to install ruby with multiple versions. first, we have to install rbenv or rvm which will make our life easier 😁 I personally prefer rbenv. so this post will have rbenv instructions only.

brew install rbenv

the above command ☝️ will install rbenv in your system

after that run the below two commands

eval "$(rbenv init -)"

and

echo "eval \"\$(rbenv init -)\"" >> ~/.bash_profile 

this will make an entry to the ~/.bash_profile

after that install the desired ruby version with the command

rbenv install 2.3.3

2.3.3 in my case, yours may be different, replace 2.3.3 with your desired ruby version.

once it is successful, run the below command to use that ruby version

rbenv local 2.3.3

done..!! ok now run the below command after it. don’t forget about it

rbenv rehash

then run

gem install bundle

and after that again run

rbenv rehash

OK..!! enough of running commands.. wait a minute.. let me tell you that you have successfully installed ruby 2.3.3 in your system.. πŸ‘»

so now about the problem i mentioned in the top of the post. If you are using Catalina OS or zsh as your default shell. sometime you will notice that while running ruby -v you are not getting the same ruby version you selected from rbenv local 2.3.3 command. instead of getting result as 2.3.3 you may be getting 2.6 or something.

it is maybe your ~/.bash_profile is not working properly. it just needs to be reloaded again by running the command

source ~/.bash_profile

but you have to do that every time when you face the problem..!!! 😟

why not think of a better solution instead of that… πŸ€”πŸ’­ well I have a solution for that also... we can make sure that every time our system start it automatically reloaded the ~/.bash_profile for that have to configure~/.zshrc file

in the terminal open the ~/.zshrc file with the command

nano ~/.zshrc 

paste the below code snippet over there

if [ -f ~/.bash_profile ]; then. ~/.bash_profilefi

save the file by hitting control + X and reload the ~/.zshrc file with the command

source ~/.zshrc

bravo..!! now your issue will be resolved πŸ‘

PostgreSQL

sorry, I have mentioned about the installation of PostgreSQL also at the beginning but I got carried away by Ruby.. πŸ˜… now we are back in business 😎 in order to install PostgreSQL with a different version run the below command

brew install postgresql@9.5

9.5 in my case. feel free to change the version according to your need

once the installation is done, make an entry of that in ~/.bash_profile by running the below command

echo 'export PATH="/usr/local/opt/postgresql@9.5/bin:$PATH"' >> ~/.bash_profile

yeah..!! you have done with the formalities, now you want to start the postgreSQL service right..?? for that hit the below 2 commands one by one in the terminal

brew services start postgresql@9.5pg_ctl -D /usr/local/var/postgresql@9.5 start

If you have a dump of data and want to restore that in your local, then run the below command

pg_restore -U <user> -h <host> -Fc --data-only -d <database> <dumpfile>

where

user: postgres
host: localhost
database: your_db_name

cheers.. now build an awesome product with PostgreSQL and ruby.. 🀘

About the author

feel free to follow me on GitHub https://github.com/SK-CSE

or connect with me on LinkedIn https://www.linkedin.com/in/saurabh-kumar-7748015a/

for more post like this follow https://medium.com/khojchakra

--

--

Saurabh Kumar
KhojChakra
Editor for

a guy who believe in spreading love, peace and knowledge.. a Senior Software Engineer working remotely