File: //proc/1233/root/home/arjun/projects/buyercall/installation.md
BuyerCall Developer Setup Commands
==================================
> Note: This document is a reference for a Debian based linux distributions, to set up the Buyercall project.
> Includes scripting with Bash.
**1. Folder Structure**
```sh
mkdir BuyerCall && cd BuyerCall && mkdir app && cd app
```
* copy the git repo zip extracted to app folder and rename it to 'buyercall'
* OR
* clone the git repo to the folder 'buyercall'
**2. Install System Packages**
```sh
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
```
**3. Install Python 3.7**
Here you can install the python version **3.7** on your system globally or you can install a python version manager **pyenv**.
If you've other projects running on the version other than 3.7, then it is better to use pyenv.
- Globally install python 3.7
```sh
# Adding python repository
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
# Install python 3.7
sudo apt install python3.7
```
- OR
- Install python 3.7 with pyenv
```sh
# Install prerequisites
sudo apt-get install -y build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev python-openssl git
```
```sh
# Get the pyenv from git
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
```
```sh
# Add pyenv path to environment path
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init --path)"\nfi' >> ~/.bashrc
```
```sh
# Restart the shell
exec $SHELL
```
```sh
# Install python 3.7
pyenv install 3.7.13
```
**4. Install NodeJS 12.x**
- Install node system-wide
```sh
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install -f -y libpq-dev nodejs nodejs-legacy
```
- OR
- Install nodejs with NVM (Node Version manager)
```sh
# Download nvm and install
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
```
```sh
# Load the environment
source ~/.profile
```
```sh
# Install nodejs
nvm install 12.22.7
```
- You can install multiple versions of node with the above command.
```sh
# Activate a specific version of the node
nvm use 12.22.7
```
**5. Installing Virutualenv and Virtualwrappper**
- Install virtualenv
```sh
sudo apt install virtualenv
```
- OR
- Install virtualenvwrapper
```sh
sudo apt install virtualenvwrapper
```
Edit `~/.bashrc` file and add below lines:
```sh
export WORKON_HOME=~/.virtualenvs
. /usr/local/bin/virtualenvwrapper.sh
```
- Relaunch the shell
```sh
exec $SHELL
```
**6. Installing Docker and Docker-compose**
- Download and add docker gpg to apt
```sh
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
```
- Verify docker fingerprint
```sh
sudo apt-key fingerprint 0EBFCD88
```
Output will be:
```sh
pub rsa4096 2017-02-22 [SCEA]
9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid [ unknown] Docker Release (CE deb) <docker@docker.com>
sub rsa4096 2017-02-22 [S]
```
- Install docker
```sh
sudo apt-get install docker-ce docker-ce-cli containerd.io
```
- Test sample docker container
```sh
sudo docker run hello-world
```
- Install docker-compose
```sh
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
```
```sh
# Check version
docker-compose --version
```
```sh
# Set user permission
sudo usermod -aG docker ${USER}
sudo setfacl --modify user:$USER:rw /var/run/docker.sock
```
**7. Create a Virtual Environment**
```sh
cd <Project Folder>/BuyerCall/app
```
```sh
# If you are using virtualenv
virtualenv venvBuyerCall
# Activate the virtual environment
source venvBuyerCall/bin/activate
```
OR
```sh
# If you are using virtualenvwrapper
mkvirtualenv venvBuyerCall
# Activate the virtual environment
workon venvBuyerCall
```
**8. Install CLI tool, BuyerCall Project Dependencies and NPM**
```sh
cd buyercall/
pip install --editable .
pip install -r requirements.txt
npm install
```
**9. Pull Docker Images**
```sh
docker-compose pull
```
s- if it fails, try:
```sh
sudo docker-compose up
```
**10. Update the config for local**
- Edit `instance/settings.py`
**11. Build and Run the BuyerCall Project**
```sh
run assets build
```
- open another terminal (ctrl + shift + t)
```sh
docker-compose up
```
if postgres error run below command and try again:
```sh
sudo service postgresql stop
sudo update-rc.d postgresql disable
```
OR
```sh
lsof -i :5432 # Note down the porecess id(s)
kill -9 <PID>
```
If the error still shows that the port is being used, it's because some containers are already running on this port.
stop all containers using this command and try again:
```sh
docker stop $(docker ps -a -q)
```
- on previous terminal:
```sh
# Reset db
run db reset buyercall buyercall_test
# Run project
run all
```
**12. Create user and database inside docker container**
- Get the postgres container ID
```sh
docker ps
```
- Use the postgres container ID in the below command to get into docker bash
```sh
docker exec -it <PSQL-Container-ID> bash
```
- Get into postgres shell
```sh
psql -h localhost -p 5432 -U postgres
```
- Create postgres user
```sh
create user <username>;
```
- Set password for user
```sh
alter user <username> with encrypted password '<password>';
```
- Create postgres database
```sh
create database <dbname>;
```
- Get into created database
```sh
\c buyercall_demo;
```
- Add UUID extension to database
```sh
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
```
- Give permission on database to the user
```sh
grant all privileges on database <dbname> to <username> ;
```
- Check whether the extension is added
```sh
SELECT * FROM pg_extension;
```
**13. Using a Test Database**
- Run below commands in terminal
```sh
# Turn up the docker container
docker-compose up
```
```sh
# Copy the sql file(here sqlfile.sql) to docker container
docker cp sqlfile.sql buyercall_postgres_1:/var/lib/postgresql/data
```
```sh
# Get into the dokcer container and load the sql data
psql <database_name> < <sql_filename> -U <username>
```
OR
```sh
# Execute the sql file with psql inside docker. Give postgresql username and database name
docker exec buyercall_postgres_1 bash -c "cat /var/lib/postgresql/data/sqlfile.sql | psql -U <user> -d <database>"
```
Example:
```sh
docker exec buyercall_postgres_1 bash -c "cat /var/lib/postgresql/data/demo_new.sql | psql -U postgres -d buyercall_demo"
```
> **References:**
> - Docker: https://dev.to/shree_j/how-to-install-and-run-psql-using-docker-41j2
>
>