# Prerequisite handbook for GAAS: Linux tutorial

## We suggest developers to install GAAS mirror to use the project:

{% content-ref url="/pages/-Lt8dwFW49twRAzk32nO" %}
[GAAS v0.7 Release Mirror - x64](/guide/handy-tools/v0.7-release-mirror-x86.md)
{% endcontent-ref %}

## The Linux shell tutorial

It's necessary to be able to use the Linux shell proficiently since the PX4 and ROS are based on Linux operating system. Here is the terminal interface of the Linux shell:

![Linux terminal window](/files/-M-0T8izhNCAY7ioQnKi)

Similar as Windows, the file system of Linux is organized as a directory tree, a typical structure of  Linux file system is shown as follows:

![](/files/-M-0TEBg314u_hxpn_RZ)

`/` is the entry point of the Linux file system，there will be many other files and folders under `/`:

![](/files/-M-0TLr0RKUTLd1dexa9)

With the authority of the Linux file system, by defalut every user can only own the full access of their own Home folder, the path of a user's Home folder is  `/home/user`

For example, the Home folder path of the user `gi` is`/home/gi`,  it can be replaced with `~`, which means`/home/gi/GAAS` can be replaced with `~/GAAS`

&#x20;`./` represents the current directory. If you would like to execute a binary file in the current folder, the path should start with `./` . For example,  there is a binary file `hello`in the current folder,  it will be executed with the following command:

`./hello`&#x20;

Similarly，`../` represents the parent folder.&#x20;

Here is a list of useful commands:

`man` shows the user manual，you can use `man command` to show the manual of a command

`ls` lists the contents of your current working directory,  use`ls -l` to show them as a list;

`cd` changes the current working directory to another directory, use `cd ..` to change to the parent directory;

`mkdir` makes a directory;

`cp` copys a file or a folder , usage is `cp source target`, to copy a directory you need the parameter `-r` ;&#x20;

`mv` moves a file or a folder , the usage is similar with `cp`, but you needn't use `-r` to move a directory;

`cat` shows the content of a text file;

`clear` cleans up the contents in current terminal;&#x20;

`touch` creates a new blank text file;

You can press **Ctrl+C** to stop a running process.

For more information about the Linux shell, you can visit this site:

{% embed url="<http://www.ee.surrey.ac.uk/Teaching/Unix/index.html>" %}

## The ownership and  access authority of Linux

Use `ls -l` to see the details of files in a list：

![](/files/-M-0UnNzTsgdpueMKKCB)

At the directory of `/`, using `ls -l` you can find that most of the files are owned by the system (owner is `root`)，modifying these files will be rejected with `permission denied` **.** If the current user owns the `root` privilege, you can use `sudo` to launch a command with `root` privilege. For example, to make a blank file in `/` with the name `text.txt`, you can use `sudo touch text.txt`

A password will be required, notice that the characters you input will not be displayed:

![](/files/-M-0VAnSZkqA_FjuSNSB)

Here is a list of useful commands:（Remember to use `sudo`）：

`chown` changes the owner of a file or folder, you can use it with `sudo` to change a file's owner from  `root`  to the current user.

`chmod` changes the permission of a file, use`chmod +x`  to add the permission of executable to a file (e.g. a shell script).

## Linux variables

1、Introduction to the variables：

Variables are a way of passing information from the shell to programs when you run them. Programs look "in the environment" for particular variables and if they are found will use the values stored. Some are set by the system, others by you, yet others by the shell, or any program that loads another program.

Standard UNIX variables are split into two categories, environment variables and shell variables. In broad terms, shell variables apply only to the current instance of the shell and are used to set short-term working conditions; environment variables have a farther reaching significance, and those set at login are valid for the duration of the session.&#x20;

For example, the command `ls` is actually a executable binary file located in the `/bin` directory——use the command `which` to find the actual path of a command:

![](/files/-M-0Vg6RWw9OS5DGm7f9)

**Note: please check the variable first if an error like "xxx not found" occurs, here "xxx" is a component that has already been installed on your system.**

2、bashrc script

Each time you login to a UNIX host, the system looks in your home directory for initialisation files. Information in these files is used to set up your working environment. The Ubuntu is using the bash shell, and the bash shell uses the script of  `bashrc`.  This script is located in the home folder and its a hidden file (filename starts with `.` ),  with the actual path `~/.bashrc` .

You can add new variables in this file to make the variables taking efforts. Notice that when the bashrc is modified, you need to reopen a new terminal to apply the changes or use the following command:&#x20;

`source ~/.bashrc`

For example, when you have already installed the ROS with `apt-get`, you will be unable to launch `roscore` without setting the variables:

![](/files/-M-0Vwl7KfhcRZF8Tb48)

You need to add the following script to `bashrc`and run `source`：

`source /opt/ros/melodic/setup.bash`

When the script is applied, you can use the command `which` to find the actual path of `roscore`

![](file:///C:/Users/user/Evernote/TEMP/enhtmlclip/Image\(6\).png)3、echo command

The Linux shell is actually a kind of programming language, the `echo` command is to output contents on the screen, similar as the `print` function in python. This command is usually used to print descriptions to the screen when programming a shell script, or show the content of a variable. For example, use the following command to show the variable `GAZEBO_MODEL_PATH`:

![](/files/-M-0WWPpf3f0uob08kMy)

Notice that the name of a variable starts with  `$` . This variable will tell Gazebo where to find the model files. &#x20;

`echo $环境变量名`

## The usage of Package manager

1、Ubuntu package manager

The Linux operating system uses the package manager to download and install softwares precompiled. Ubuntu uses `apt` as the package manager.  The basic usage:

`sudo apt-get install package-name`

\
2、Sources of package manager

Some of the packages such as ROS is not included in the official package sources,  you can add the sources manually to install the third party packages with the package manager.

Here is the instruction of installing ROS on Ubuntu:

<http://wiki.ros.org/melodic/Installation/Ubuntu>

**Notice that you must set up the keys before using the sources of ROS.**

![Set up the keys of ROS sources](/files/-M-0X8cKhlBK3xmj74pT)

**You must use the following command to apply the sources modified**

`sudo apt-get update`

3、Python package manager&#x20;

The Python intepreter also includes a package manager for installing third party Python library. Since the ROS is partly based on Python, some Python packages are required to install.

The package manager of Python is pip, and should be installed through the following command:

`sudo apt-get install python-pip`

Usage:&#x20;

`pip install package-name`

\
When a "permission denied" error occurs, use the parameter `--user`

`pip install package-name --user`

**Notice that you had better not run pip with sudo!**

## Compile from source

1、software distribution&#x20;

The developers can distribute the software in two kinds of forms: source code distribution and binary distribution. Many open-source softwares are distributed in the form of source code, and the users should compile the source code by themselves. Some components of the GAAS are also released with source codes and should be compiled into executable binary files to use e.g. the SLAM component.

2、cmake and makefile

Many open-source softwares are built with C/C++ programming language, and the source code files should be compiled and linked in the sequence of dependency. The UNIX-compatible systems (e.g. Linux) are using makefile to define compiling rules and dependencies. Use the command `make` to launch a makefile and compile the codes in sequence, and the files which have already been built will be skipped.&#x20;

The C++ compiling toolchains of Ubuntu should be installed with the command:&#x20;

`sudo apt-get install build-essential`

Use make to execute a makefile：&#x20;

`make target`

Here the "target" is defined by the makefile. For example, the following command can build the PX4 firmware and Gazebo simulator:

`make posix_sitl_default gazebo`&#x20;

For most software, you can use a blank "target" directly:

`make`&#x20;

Use the parameter `-j` to compile in parallel with a number of subprocesses:

`make -j4`&#x20;

The parameter can take advantage of multi-core CPU, but the number of subprocesses cannot be more than actual CPU cores, or it may cause issues.

Cmake  is used to detect the current platform, toolchains and shared libraries, configure and generate a makefile automatically. You can use apt-get to install cmake in Ubuntu.&#x20;

3、Build and install open-source softwares

You can use the following steps to install most of the open-source softwares.&#x20;

(1) Get the source code, you can use git to clone a repository on GitHub&#x20;

`git clone (the git address of the repository)`

(2) Make a building directory.  Since CMake do not provide the function of cleaning, the best practise is to make a building directory for CMake to generate temporary files. Usually the folder is named as `build` .

`cd (directory of the repository)`&#x20;

`mkdir build`&#x20;

`cd build`&#x20;

(3) Generate makefile with CMake:

`cmake ..`&#x20;

Here the current directory is the `build` directory, and cmake will find the configuration file `CMakeLists.txt`. This file is provided by the software developer, and usually the file is located in the directory of the software repository, which is the parent directory of`build` . For most software, you can use the default parameters of CMake.&#x20;

(4) Compile and install：&#x20;

`make`&#x20;

`(Optional) sudo make install`&#x20;

For the shared library,  `install` can install the compiled binary files into the system library for other programs to use.&#x20;

[Support us on Patreon](https://www.patreon.com/gaas)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gaas.gitbook.io/guide/software-realization-build-your-own-autonomous-drone/prerequisite-handbook-for-gaas-linux-tutorial.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
