Setup Nginx, SSL , Firewall | Moving micro-services into AWS EC2 instance – Part 4

Install Nginx proxy server. Nginx also act like a load-balacer which is helpful for the balancing of network traffic.

sudo apt-get update
sudo apt-get install nginx

Commands to stop, start, restart, check status

sudo systemctl stop nginx
sudo systemctl start nginx
sudo systemctl restart nginx

# after making configuration changes
sudo systemctl reload nginx
sudo systemctl disable nginx
sudo systemctl enable nginx

Install SSL – Letsencrypt

Install packages needed for ssl

sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx

Install the SSL Certificate:

certbot -d '*.domain.com' -d domain.com --manual --preferred-challenges dns certonly

Your certificate and chain have been saved at:
   /etc/letsencrypt/live/domain.com/fullchain.pem

Your key file has been saved at:
   /etc/letsencrypt/live/domain.com/privkey.pem
SSL certificate auto renewal

Let’s Encrypt’s certificates are valid for 90 days. To automatically renew the certificates before they expire, the certbot package creates a cronjob which will run twice a day and will automatically renew any certificate 30 days before its expiration.

Since we are using the certbot webroot plug-in once the certificate is renewed we also have to reload the nginx service. To do so append –renew-hook “systemctl reload nginx” to the /etc/cron.d/certbot file so as it looks like this:

/etc/cron.d/certbot
0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew --renew-hook "systemctl reload nginx"

To test the renewal process, use the certbot –dry-run switch:

sudo certbot renew --dry-run

Renew your EXPIRED certificate this way:

sudo certbot --force-renewal -d '*.domain.com' -d domain.com --manual --preferred-challenges dns certonly

Are you OK with your IP being logged?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.<domain>.com with the following value:

O3bpxxxxxxxxxxxxxxxxxxxxxxxxxxY4TnNo

Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue

You need to update the DNS txt record for _acme-challenge.<domain>.com

sudo systemctl restart nginx # restart nginx to take effect

Configure the Firewall

Next, we’ll update our firewall to allow HTTPS traffic.

Check firewall status in the system. If it is inactive enable firewall.

sudo ufw status # check status

# enable firewall
sudo ufw enable
sudo ufw allow ssh
sudo ufw allow OpenSSH

Enable particular ports where your micro-services are running. Example:

sudo ufw allow 4031/tcp # Authentication service
sudo ufw allow 4131/tcp # File service
sudo ufw allow 4232/tcp # Search service

You can delete the ‘Authentication service’ firewall rule by:

sudo ufw delete allow 4031/tcp
Advertisement

Setup Ruby, ruby-build, rbenv-gemset | Conclusion – Moving micro-services into AWS EC2 instance – Part 3

In this post let’s setup Ruby and ruby gemsets for each project, so that your package versions are maintained.

Install ruby-build # ruby-build is a command-line utility for rbenv

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

# Add ruby build path

echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc # OR
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.zshrc

# load it

source ~/.bashrc # OR
source ~/.zshrc


For Mac users – iOS users


# verify rbenv
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-doctor | bash

If you are using zsh add the following to `~/.zshrc`

# rbenv configuration
eval "$(rbenv init -)"
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.1)"

Install Ruby 2.5.1 using rbenv

rbenv install 2.5.1

rbenv global 2.5.1 # to make this version as default

ruby -v # must display 2.5.1 if installed correctly

which ruby # must show the fully qualified path of the executable

echo "gem: --no-document" > ~/.gemrc # to skip documentation while installing gem

rbenv rehash # latest version of rbenv apparently don't need this. Nevertheless, lets use it to avoid surprises.

gem env home # See related details

# If a new version of ruby was installed, ensure RubyGems is up to date.
gem update --system --no-document


Install rbenv gemset – https://github.com/jf/rbenv-gemset

git clone git://github.com/jf/rbenv-gemset.git ~/.rbenv/plugins/rbenv-gemset

If you are getting following issue:

fatal: remote error:
  The unauthenticated git protocol on port 9418 is no longer supported.
# Fix
 git clone https://github.com/jf/rbenv-gemset.git ~/.rbenv/plugins/rbenv-gemset

Now clone your project and go inside the project folder -Micro-service folder (say my-project) which has Gemfile in it and do the following commands.

cd my-project

my-project $ rbenv gemset init # NOTE: this will create the gemset under the current ruby version.

my-project $ rbenv gemset list # list all gemsets

my-project $ rbenv gemset active # check this in project folder

my-project $ gem install bundler -v '1.6.0'

my-project $ rbenv rehash

my-project $ bundle install  # install all the gems for the project inside the gemset.

my-project $ rails s -e production # start rails server
my-project $ puma -e production -p 3002 -C config/puma.rb # OR start puma server
# OR start the server you have configured with rails. 

Do this for all the services and see how this is running. The above will install all the gems inside the project gemset that acts like a namespace.

So our aim is to setup all the ruby micro-services in the same machine.

  • I started 10 services together in AWS EC2 (type: t3.small).
  • Database is running in t2.small instance with 2 volumes (EBS) attached.
  • For Background job DB (redis) is running in t2.micro instance.

So for 3 ec2 instance + 2 EBS volumes –$26 + elastic IP addresses ( aws charges some amount – $7.4) 1 month duration, it costs me around $77.8, almost 6k rupees. That means we reduced the aws-cloud cost to half of the previous cost.

Steps to reinstall rvm in ubuntu

Uninstall RVM – do the following

unset GEM_HOME

rvm implode

rm -rf ~/.rvm

sudo rm -rf /usr/share/rvm

sudo rm /etc/profile.d/rvm.sh

sudo rm /etc/rvmrc

sudo rm ~/.rvmrc

vim ~/.zshrc  # or ~/.bash_profile related to machine/software you use and remove rvm related lines

Install RVM:

sudo apt-get install build-essential

unset rvm_path

sudo apt install gnupg2

Goto https://rvm.io/rvm/install and add gpg keys to verify

gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
 
\curl -sSL https://get.rvm.io | bash -s stable

If the following error occurs while adding keys:

➜ gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
gpg: connecting dirmngr at '/run/user/1000/gnupg/S.dirmngr' failed: IPC connect call failed
gpg: keyserver receive failed: No dirmngr

then do:

sudo apt-get update

then you will get some errors regarding the keys, add those keys to following command:

gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

then do:

\curl -sSL https://get.rvm.io | bash -s stable

Make sure that your ~/.bash_profile or ~/.zshrc file contains the following lines to load rvm to the shell:

# RVM manual script for loading rvm to shell
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" 

After installing check RVM by:

➜ rvm list

# No rvm rubies installed yet. Try 'rvm help install'.

➜ rvm install 2.7.2
Searching for binary rubies, this might take some time.
Found remote file https://rubies.travis-ci.org/ubuntu/18.04/x86_64/ruby-2.7.2.tar.bz2
Checking requirements for ubuntu.
Requirements installation successful.
ruby-2.7.2 - #configure
ruby-2.7.2 - #download
 ..............
No checksum for downloaded archive, recording checksum in user configuration.
ruby-2.7.2 - #validate archive
ruby-2.7.2 - #extract
ruby-2.7.2 - #validate binary
ruby-2.7.2 - #setup
ruby-2.7.2 - #gemset created ~/.rvm/gems/ruby-2.7.2@global
ruby-2.7.2 - #importing gemset ~/.rvm/gemsets/global.gems..................................
ruby-2.7.2 - #generating global wrappers.......
ruby-2.7.2 - #gemset created ~/.rvm/gems/ruby-2.7.2
ruby-2.7.2 - #importing gemsetfile ~/.rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.7.2 - #generating default wrappers.......

➜ rvm list
=* ruby-2.7.2 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

➜ rvm gemset list

gemsets for ruby-2.7.2 (found in ~/.rvm/gems/ruby-2.7.2)
=> (default)
   global

➜ rvm gemset create foobar
ruby-2.7.2 - #gemset created ~/.rvm/gems/ruby-2.7.2@foobar
ruby-2.7.2 - #generating foobar wrappers.......

➜ rvm gemset list              

gemsets for ruby-2.7.2 (found in ~/.rvm/gems/ruby-2.7.2)
=> (default)
   foobar
   global

➜ rvm gemset use foobar
Using ruby-2.7.2 with gemset foobar

➜ rvm gemset list           

gemsets for ruby-2.7.2 (found in ~/.rvm/gems/ruby-2.7.2)
   (default)
=> foobar
   global

➜ rvm list gemsets

rvm gemsets

   ruby-2.7.2 [ x86_64 ]
=> ruby-2.7.2@foobar [ x86_64 ]
   ruby-2.7.2@global [ x86_64 ]

For preserving the gemset for the current directory create .rvmrc file:

vim .rvmrc
# add this: rvm --rvmrc use foobar

If rvm is not loading into the shell after changing the terminal preferences, check the rvm_path env variable.

$rvm_path      
zsh: no such file or directory: /usr/share/rvm

If you don’t have that directory you must change the above path to a correct rvm installed path.

By default rvm installed in this path: ${HOME}/.rvm So you can add this path to rvm_path

Set it like:

export rvm_path="${HOME}/.rvm"

You can add this line into your ~/.zshrc OR ~/.bash_profile file.

You can check rvm env variables and info by:

env | grep rvm  
rvm info

Check ruby version by: ruby -v If ruby is not loading try to add the following line into your bash_profile:

export PATH=~/.rvm/gems/ruby-2.7.2/bin:$PATH # change version: ruby-2.7.2 to your installed version
source ~/.bash_profile OR source ~/.zshrc # whatever you use
ruby -v
 

Rails 5.2.0 API Only application with latest Ruby 2.5.1

Check for the new ruby and rails versions
https://www.ruby-lang.org/en/downloads/
https://rubygems.org/gems/rails/versions

Here we are going to install Ruby – 2.5.1 & Rails – 5.2.0 (API only application)

Get rbenv into action
If you are not installed rbenv, you can install it from here:
https://github.com/rbenv/rbenv
After the installation make sure that, your $PATH has included rbenv/shims path. Else rbenv will not work.

1. $ rbenv install --list # Gets the list of ruby versions available

$ rbenv install 2.5.1

ruby-build: definition not found: 2.5.1

The following versions contain `2.5.1' in the name:
  rbx-2.5.1

See all available versions with `rbenv install --list'.

If the version you need is missing, try upgrading ruby-build:

  brew update && brew upgrade ruby-build

Oops..!

rbenv cannot find the version: 2.5.1

Upgrade ruby-build

Mac OSX:

$ brew upgrade ruby-build --HEAD

Now install ruby 2.5.1

$ rbenv install 2.5.1

Create a new gemset:

Rbenv gemset is a separate script and not coming with rbenv. If you are not installed this, you can install it from here:
https://github.com/jf/rbenv-gemset

$ rbenv gemset create 2.5.1 demo-app
That set up a directory for you in ~/.rbenv/versions/2.5.1/gemsets/demo-app

Set the ruby version to the newest

$ rbenv local 2.5.1

$ rbenv version
=> 2.5.1

    Activate New Gemset


For activating a gemset we need to create a .rbenv-gemsets file in the current directory.

$ touch .rbenv-gemsets
$ echo demo-app > .rbenv-gemsets

Check active gemset:

$ rbenv gemset active

Install Rails 5.2.0 API only Application

$ gem install rails -v '5.2.0'

$ rails -v
Rails 5.2.0

Later we can delete this .rbenv-gemsets file and add a new file named ‘.ruby-gemset’ in the rails project directory. I cannot find any other option for doing this. If anyone know more about this, please give a comment. I appreciate that.

Create a New Rails app

$ rails new demo-app  --api -T # API only skip the testing framework altogether

For Full Rails:

$ rails new demo-app -T -d postgresql # skip the testing framework altogether, uses Postgres Database

-T to exclude Minitest – the default testing framework if you are planning to use RSpec to test your API.

Rspec test framework:
https://github.com/rspec/rspec-rails

You can use the following with Rspec.
Shoulda Matchers:
Collection of testing matchers extracted from Shoulda (http://matchers.shoulda.io)
https://github.com/thoughtbot/shoulda-matchers

Database Cleaner:
Strategies for cleaning databases in Ruby. Can be used to ensure a clean state for testing
https://github.com/DatabaseCleaner/database_cleaner

Faker:
A library for generating fake data such as names, addresses, and phone numbers.
https://github.com/stympy/faker

use option: –no-rdoc –no-ri # skips the documentation

Remove Rbenv Gemset and add Ruby gems file

$ rm .rbenv-gemsets

$ cd demo-app
$ touch .ruby-gemset
$ echo demo-app > .ruby-gemset
$ rails s
=> Booting Puma
=> Rails 5.2.0 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.11.4 (ruby 2.5.1-p57), codename: Love Song
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

Goto http://localhost:3000/

rails-5.2.api-application

Done! Lets go…

Install Learning Locker (NodeJS) in your Mac OSX

Visit the following link to install LL on CentOS, Fedora, Ubuntu, and Debian OS
http://docs.learninglocker.net/guides-installing/

Mac OS is not supported in the installation process specified above.

You can install Learning Locker in Mac OSX by custom installation.

Steps to Install LL in Mac OSX:

1. Clone the repo from learning locker git repo

$ git clone https://github.com/LearningLocker/learninglocker.git

Enter into the directory and install the requirements:

$ yarn install

You can be built 5 distinct services with this codebase.
2. Install services

If you want to install all services on the same machine, you can use the following one command:
$ yarn build-all

Install Services separately if you want to install each service in different servers

Install the UI Server
$ yarn build-ui-server

Install the UI Client
$ yarn build-ui-client

Install the API Server
$ yarn build-api-server

Install the Worker
$ yarn build-worker-server

Install CLI
$ yarn build-cli-server

Note: Copy the .env.example into a new .env file and edit as required

RUNNING THE SERVICES VIA PM2

Install pm2 , if you have not yet installed
$ npm i -g pm2

To start API, Scheduler, UI, Worker services, navigate to the LL working directory and run:

$ pm2 start pm2/all.json

INSTALLING THE XAPI SERVICE

Step 1: Clone the repo
$ git clone https://github.com/LearningLocker/xapi-service.git

Step 2: Enter into the directory and install the requirements and build

$ yarn install
$ yarn build

Note: Copy the .env.example into a new .env file and edit as required

To start the xAPI service, navigate to the xAPI Service working directory and run:

$ pm2 start pm2/xapi.json

You can check the service status:

$ pm2 status

$ pm2 restart all # restart all services

$ pm2 logs # view logs

Launch the UI:
http://localhost:3310/login, note that I have change the UI port in .env to 3310, as other services running in default port

TheLearningLockerMac

Now you have to create User for logging in. Lets create an Admin User by the following command.

$ node cli/dist/server createSiteAdmin [email] [organisation] [password]

In order to use this command you have to install the CLI server for LL, that I already mentioned in the installation steps.

An Admin Example
$ node cli/dist/server createSiteAdmin "MyEmailId" "CoMakeIT" “myPassword”

You can run the migrations by the following command if any pending migrations exists

$ node cli/dist/server migrateMongo

LL-sign-in-error

Now try to Login with the credentials.

Ooops… there is an issue, and we can’t login

After doing some research I found the issue. We need to add a secret key base to our Application to work with JWT tokens.
Open .env file and find:

# Unique string used for hashing
# Recommended length, 256 bits
APP_SECRET=

Create a 256 bit length hash key and give as a value. As I am a Rails developer, It is easy for me to create one by going into any my Rails project and type

$ rake secret

I get One! 🙂

Or you can use some online applications like: https://randomkeygen.com/

And do:
$ pm2 restart all

from LL project folder

LL-dashboard

Try to sign in again

Wohh, Its done. Great. Now you can try some bit of dashboard items like Graphs. Go on.

Laravel – PHP web framework Vs Rails, Installation

Laravel is a php web framework which following the model–view–controller (MVC) architectural pattern. It is gaining more and more popularity nowadays.

It is almost followed Ruby on Rails framework pattern, not only for MVC structure but also for the pattern that Rails adopt for doing various tasks. It uses composer that installs the software snippets you needed to achieve a particular task as Rails does it with bundler to pick gems.

The drawback of Laravel is it cannot achieve the power of Ruby like meta-programming through php. So I need to write some more code in Laravel and its actually feels irritating if you want to write less code without the same repetitive patterns and keep it DRY.

Get and install composer

Goto (https://getcomposer.org/) Click download.

Run the following:

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

$ php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

$ php composer-setup.php

$ php -r "unlink('composer-setup.php');"


This will install ‘composer.phar ‘ in the current directory.

Move that to bin folder to get it globally available as a command and rename it to composer

$ mv composer.phar /usr/local/bin/composer

Open new tab and Type

$ composer

Install Lareval

Using composer

$ composer create-project --prefer-dist laravel/laravel blog "5.4.*"


From laravel/laravel github repo OR via larval installer

$ laravel new blog
-bash: laravel: command not found

Install it:

$ composer global require "laravel/installer"

this makes it globally accessible

Make sure to place the $HOME/.composer/vendor/bin directory (or the equivalent directory for your OS) in your $PATH so the laravel executable can be located by your system.

Open ~/.bash_profile OR ~/.bashrc in Mac

and add the following line:

export PATH="$PATH:$HOME/.composer/vendor/bin"

save and exit

Open New tab type:

$ laravel
Laravel Installer 1.4.1
Usage:
  command [options] [arguments]

Check Laravel version:

$ php artisan --version
Laravel Framework 5.4.27

You are ready to go!

Install latest PHP version on Mac using homebrew

Check php latest version here: http://php.net/downloads.php
=> Update your homebrew

$ brew update
$ brew upgrade

=> Install a centralized repository for PHP-related brews: homebrew-php
https://github.com/Homebrew/homebrew-php

Requirements
* Homebrew
* Yosemite, El Capitan, and Sierra. Untested everywhere else.

Run the following in your command-line:

$ brew tap homebrew/homebrew-php

$ brew search php

will show you all php formula

We will Install php 7.1, because php 7.1.8 is the latest stable version till now (Aug 2017)

$ brew install php71

Check php version installed

$ php --version
PHP 7.1.8 (cli) (built: Aug  7 2017 15:02:19) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

You are done.

PostgreSQL 9.3 : Installation on ubuntu 14.04

Hi guys, I just started installing postgres on my ubuntu VM. I referred some docs, and followed this one: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-14-04

Its pretty much explained in this page. But just explaining here the important things.

You can install postgres by ubuntu’s own apt packaging system. Update local apt repository.

$ sudo apt-get update
$ sudo apt-get install postgresql postgresql-contrib

Postgres uses role based access for the unix users. After the installation a default role called ‘postgres’ will be created. You can login to postgres account and start using or creating new roles with Postgres.

Sign in as postgres user

$ sudo -i -u postgres

Access the postgres console by

$ psql

But i cannot enter into the console and I got the following error:

postgres@8930a29k5d05:/home/rails/my_project$ psql
psql: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

What could be the reason for this error?

So just gone through Postgres doc (http://www.postgresql.org/docs/9.3/static/server-start.html). You can see the same error under the section 17.3.2. Client Connection Problems. But the solution is not mentioned.

Original Reason: PostgreSQL Server was not running after the installation.

I tried rebooting the system and via init script the server should run automatically. But the server is not running again. I understood that something prevents postgres from running the server. What is it?

Just check your postgres server is running or not

$ sudo -aux | grep post
postgres@8930a29k5d05:/home/rails/my_project$ ps -aux | grep postgres
root       136  0.0  0.2  47124  3056 ?        S    06:10   0:00 sudo -u postgres -s
postgres   137  0.0  0.3  18164  3220 ?        S    06:10   0:00 /bin/bash
postgres   140  0.0  0.2  15572  2192 ?        R+   06:10   0:00 ps -aux
postgres   141  0.0  0.0   4892   336 ?        R+   06:10   0:00 grep post

The server is not running.

Run the server manually by

root@8930a29k5d05:/home/rails/my_project#  /etc/init.d/postgresql start
 * Starting PostgreSQL 9.3 database server
                                                                                                                                                         [ OK ] 
root@8930a29k5d05:/home/rails/my_project# ps aux | grep postgres
postgres   158  0.1  2.0 244928 20752 ?        S    06:28   0:00 /usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main -c config_file=/etc/postgresql/9.3/main/postgresql.conf
postgres   160  0.0  0.3 244928  3272 ?        Ss   06:28   0:00 postgres: checkpointer process

postgres   161  0.0  0.4 244928  4176 ?        Ss   06:28   0:00 postgres: writer process

postgres   162  0.0  0.3 244928  3272 ?        Ss   06:28   0:00 postgres: wal writer process

postgres   163  0.0  0.5 245652  6000 ?        Ss   06:28   0:00 postgres: autovacuum launcher process

postgres   164  0.0  0.3 100604  3336 ?        Ss   06:28   0:00 postgres: stats collector process

root       178  0.0  0.0   8868   884 ?        S+   06:28   0:00 grep --color=auto post
root@8930a29k5d05:/home/rails/my_project#

Now the server starts running. If still not works, then try to reconfigure your locales as mentioned here

$ dpkg-reconfigure locales

It is strange that, after installing such a popular database software, it doesn’t provide any information regarding the failure of its own server. It should give the developers some clue so that they can save their precious time.

The reason of this failure, what I concluded is
1. After installation we have to run the server manually
OR
2. I tried resetting the locales (So if no locales set in the machine may prevented the postgres from starting automatically?)

Details about pg gem error: Can’t find the ‘libpq-fe.h header when doing bundle in Rails

If you are getting errors like:

installing pdf-reader 1.3.3
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

/home/vagrant/.rvm/rubies/ruby-2.2.0/bin/ruby -r ./siteconf20150306-5919-1rr483p.rb extconf.rb
checking for pg_config... yes
Using config values from /usr/bin/pg_config
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.

when trying to install pg gem,
you are just missing some dependancy library related to libpq. Install the following package


$ sudo apt-get install libpq-dev

then do


$ gem install pg -v 'version-no'

then do


bundle install