Creating a Bot/Setting up a development environment

You've downloaded Sublime Text or Visual Studio Code and you're ready to get started, but what now? What else do I need to set up to run my code?

This guide covers setting up a simple bot development environment and installing node.js, Python, and other useful tools.

The only requirement is Windows 10, or if you're already on Linux then use that. A virtual machine is also an option if you prefer completely isolating your dev-tools from your main operating system install or if you have a Mac for some reason. If you're using Linux or running a Linux VM then skip the WSL section.

If you're not comfortable with a command line, turn back now. Otherwise keep reading.

*Python and node.js users can install Visual Studio Community Edition and skip this entire guide, since both languages are supported by it. However, if you want more customization and want to dive more in-depth, this guide is for you. This guide contains information useful to anyone interested in software development, bot or not.*

## Setting up a dev environment

*"This won't take long." - Windows Setup*

### Setting up WSL

If you don't have a Linux system and don't want to bother with a virtual machine, WSL is for you!

Windows Subsystem for Linux is an optional Windows 10 feature -- it acts as a software layer that runs a full install of Ubuntu Linux on top of Windows with support for many native Linux programs. We'll be using this for our dev-environment to keep all our bot stuff in its own container.

Installing it is pretty easy actually:

1. Open Settings; go to Updates & Security > For developers, and turn on Development Mode.
2. Go back to Settings home, then Apps. Under Related settings, click Programs and features. Click Turn Windows features on or off in the sidebar, and ensure that Windows Subsystem For Linux (near the bottom) is checked, then click OK to install it. You may need to reboot after doing this.
3. Go to the Microsoft Store, then search and download Ubuntu (or Debian, or another if you prefer, but this guide uses Ubuntu).
4. Open a Command Prompt or PowerShell window, then type 'ubuntu' and Enter to finish setting up.

Congrats, you now have your own Linux coding environment running on Windows!

When you log in to Ubuntu, you'll be in your 'home' folder. Note that this is *not* your Windows home folder, this is your Linux home folder. Your Windows home folder is probably located somewhere like **/mnt/c/Users/YourUsername**. You'll want to make sure your code is stored somewhere in there (or at least somewhere in **/mnt/** so you can edit it with Windows tools).

If you don't want to deal with the hassle of navigating there with the 'cd' command, you can run

```
sudo apt-get install mc

```

to install a 'graphical' program (with mouse support) for navigating files and folders called Midnight Commander.

Either way, first thing you should do (even before installing mc) is run the following command:

```
sudo apt-get update

```

This just refreshes the Linux package manager so you can install stuff.

Now let's install the tools we need.

### Installing dev tools

To start developing and running bots, we need to install the language runtimes: these allow us to run the code that we write.

#### node.js

To install node.js, we'll add a package repository that allows us to download newer versions of it (since the one included with the default repos is horribly outdated), then install it.

```
curl -sL https://deb.nodesource.com/setup\_14.x | sudo -E bash -

sudo apt-get install -y nodejs

```

All done! Yep, it was that easy. If 'curl' isn't installed by default on your system run 'sudo apt-get install curl' to get it.

#### Python (the sane way)

I have never had a good experience installing Python from a package manager, and managing different versions is a nightmare, so we'll use a tool called **pyenv** to install and manage Python. You will thank yourself later for it.

First, install the prerequisites:

```
sudo apt-get update

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl

```

Now, run the pyenv install script:

```
curl https://pyenv.run | bash

```

In order to use it, you'll need to add some lines to the top of your .bashrc file: .bashrc is a file that is used to customize your terminal session, so by adding the following lines to it you'll be able to use pyenv from anywhere on your filesystem.

Open up this file by running

```
nano ~/.bashrc

```

This opens the file in the nano text editor. Add these lines to the top of the file, then press CTRL-O to save, then CTRL-X to exit.

```
export PATH="/home/[YOUR USERNAME HERE]/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

```

Now log out of your Linux session (WSL users can just close the window or type 'exit'), and reopen it.

Now we can install Python properly without mangling the system!

```
pyenv install 3.7.6

```

Python 3.7.6 is now installed, so let's tell the system that this is the default version of Python we want to use for all projects:

```
pyenv global 3.7.6

```

Pyenv allows you to install and manage multiple versions of Python, depending on your bot's needs. It's recommended to stick with the latest version unless you have a good reason not to.

If you want to set up an environment for a specific project that uses a different Python version or set of pip modules (without affecting/destroying anything else on the system), you can do that by creating a *virtual environment*.

Go to your project folder, and run

```
pyenv virtualenv [python version] [project-name]

pyenv local [project-name]

```

The first command creates a virtual environment with the specified name and Python version, and the second one assigns it to the current working folder. Now, while the rest of your system is still using the default global Python version, that specific folder will be using your new virtual environment. Perfect!

#### PHP

PHP is another common language for bots. Despite the many elite-ist memes against it, it's really not that bad of an option. Every language is bad in their own ways :)

Install it with:

```
sudo apt-get install php php-mcrypt php-curl php-cli php-imagick php-json php-mbstring php-readline php-xml php-mysql

```

We've included a bunch of php modules in here, so what do they do?

* php-imagick is a connector for the image manipulation program ImageMagick (see Other Tools)
* php-mbstring adds support for multi-byte characters in strings (such as Emojis and other UTF-8 characters used in non-English languages)
* php-cli is required and allows PHP to be run at the command line
* php-curl allows PHP to use 'curl' to download remote files in a clean way
* php-json adds support for processing JSON-formatted data
* php-xml allows for parsing XML and DOM (Document Object Model) documents, such as HTML. Useful for pulling data from webpages when used with php-curl
* php-mysql adds support for connecting to a MySQL database for storing and retrieving data

#### MySQL

MySQL is a database system that can be used to store and retrieve data that your bots may use. Databases can be shared between multiple codebases as well.

Install it with:

```
sudo apt-get install mysql-server mysql-client

```

Set it up with

```
mysql-secure-installation

```

#### Java

Java is another (less recommended but still probably doable if you can find a working FB api for it) language choice.

While it may not be recommended for your actual bot, it's great if you have some data processing you want to keep separate from your bot logic (example: HydraulicPressBot 5000000 consists of both PHP and Java; the PHP portion manages the general logic of the bot, and the Java portion is used to actually generate the frames for a requested crush video).

Install it with:

```
sudo apt-get install default-jre default-jdk

```

#### Other tools

##### ImageMagick

Used for image manipulation on the command line

```
sudo apt-get install imagemagick

```

##### ffmpeg

Used for audio/video file manipulation on the command line

```
sudo apt-get install ffmpeg

```

### What now?

So you're all set up and ready to make bots! Check out the other guides at Creating a Bot to get started!

Or continue reading for one more useful tip that I use on all my Linux installs.

#### Add filesystem bookmarks to Linux

The 'cd' command is used to **c**hange your current working **d**irectory. Using it to switch between multiple directories can be annoying, so this is where filesystem bookmarks come in. (I did not write this code snippet, but I'm sharing it because it is immeasurably useful)

Open up your .bashrc file as we did earlier, then add the following mess to the top of it:

```
alias ..="cd ../";
alias ...="cd ../../";

```

```
# filesystem bookmarking
export MARKPATH=$HOME/.marks
function jump {
    cd -P "$MARKPATH/$1" 2>/dev/null || echo "No such mark: $1"
}
function mark {
    mkdir -p "$MARKPATH"; ln -s "$(pwd)" "$MARKPATH/$1"
}
function unmark {
    rm -i "$MARKPATH/$1"
}
function marks {
    ls -l "$MARKPATH" | sed 's/  / /g' | cut -d' ' -f9- | sed 's/ -/\t-/g' && echo
}

```

Log out of your Linux session and then log back in; now you have the following abilities:

* The **..** command takes you one level up in the directory tree (if you are in **/mnt/d/projects/mygreatbot/src**, this command will take you to **/mnt/d/projects/mygreatbot**)
* The **...** command takes you *two* levels up in the directory tree (if you are in **/mnt/d/projects/mygreatbot/src**, this command will take you to **/mnt/d/projects/**)
* The **mark** *[bookmark-name]* command marks your current working directory with a bookmark name you can use to easily access it later.
* The **jump** *[bookmark-name]*command allows you to quickly change your current working directory to a previously bookmarked one.
* The **unmark** *[bookmark-name*] command removes a named bookmark.
* The **marks** command lists the names and destinations of all bookmarks you've created.