How to install multiple version of Ruby and PostgreSQL on Mac OS Catalina π¨π»βπ»
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 on
bundle install
orrails s
command..??
If yes, then donβt worry I have a solution for you which works on my machine π
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