Advertisements

How to setup Raspbian Stretch Lite on Raspberry Pi 3 to run Python 3 applications

Raspbian Stretch Lite operating system is the Raspberry Pi Foundation's official supported operating system for running headless software. It is a good operating system for different Raspberry Pi 3 use cases. Some examples include:

If you are planning to use Raspberry Pi 3 GPIO to interface with the real world, it is recommended that you setup Raspbian Stretch Lite to run Python 3 applications.

This post provides the steps to setup Raspbian Stretch Lite on Raspberry Pi 3 for running Python 3 applications.

Recommended hardware list

Prerequisite hardware to install operating system onto the microSD

Raspberry Pi 3 board will load an operating system from a microSD card. Hence, we will need to have a computer with a SD card reader. If your computer does not come with a SD card reader, you have to get one.

Downloading a copy of Raspbian Stretch Lite for your Raspberry Pi 3

Once you had all the necessary hardware, proceed to download a version of the Raspbian Stretch Lite operating system for your Raspberry Pi 3. As of this writing, the one that was available was dated 29th November 2017:

Raspbian Stretch download page dated 20171129

Click on the Download Zip button for Raspbian Stretch Lite and save the zip file onto your file system.

Installing Raspbian Stretch Lite on the microSD card

After your browser had completed downloading the .zip file, extract the .img file from the .zip file. The .img file is the operating system image for installing Raspbian Stretch Lite onto the microSD card.

I use Etcher on my MacBook Pro to install operating system images onto microSD cards.

If you are using a windows machine, you can use Win32DiskImager to install the operating system for your Raspberry Pi 3.

Sitewide-10usd468x60

If you are using a Linux desktop, you should be able to Etcher to install the operating system for your Raspberry Pi 3.

Enabling SSH server on Raspbian Stretch Lite first boot

With an SSH server running on Raspbian Stretch Lite, we do not have to find a spare keyboard and monitor in order to configure Raspbian Stretch Lite after it had booted up for the first time. To ensure that the SSH server runs after the first boot, create a file named "ssh" in the boot partition of the microSD card:

Raspbian Stretch Lite 20171212 boot with ssh file

Assembling the hardware for the Raspberry Pi 3

Once the ssh file is created in the boot partition of the microSD card, remove the microSD card from your SD card reader and insert it to the microSD card slot on the Raspberry Pi 3 board. After that, proceed to assemble the Raspberry Pi 3 board to the Official Raspberry Pi case.

Starting the Raspbian Stretch Lite operating system

With the assembly of your Raspberry Pi 3 board and Official Raspberry Pi case, connect one end of the RJ45 cable to the RJ45 port on the Raspberry Pi 3 board and the other end of the cable to one of the switch port of your home router. After that, connect the micro USB power supply to the Raspberry Pi 3 board and a wall socket. Turn on the power socket to supply power to the Raspberry Pi 3 board.

Finding the IP address of the Raspberry Pi 3

When Raspbian Stretch Lite boots up, it will request an IP address from your router. This IP address is needed for us to SSH into the Raspberry Pi 3. Go to your router's address allocation table and find an entry with the name raspberrypi.

Getting into the Raspbian Stretch Lite operating system via SSH

Let's suppose that the router allocated 192.168.1.109 to our Raspbian Stretch Lite.

In addition, the default credentials to log into Raspberry Pi Stretch Lite is as follows:
Username: pi
Password: raspberry

With the default credentials and the IP address that the router had given to Raspbian Stretch Lite, we then run the following command to SSH into the operating system:

ssh pi@192.168.1.109

Enter raspberry when Raspbian Stretch Lite prompts for password. That will result in Raspbian Stretch Lite greeting you with output similar to the following:

Linux raspberrypi 4.9.59-v7+ #1047 SMP Sun Oct 29 12:19:23 GMT 2017 armv7l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.

SSH is enabled and the default password for the 'pi' user has not been changed.
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.

Changing the default password

Since Raspbian Stretch Lite suggests that using the default password for the 'pi' user is a security risk, the next thing that we should do is to change the default password. To do so, enter the following command:

passwd

For the three prompts that follow the command, enter raspberry and your favourite password twice.

Configuring the locale settings

The next thing that you will want to do is to configure the locale settings for your Raspbian Stretch Lite. To do so, open up the Raspberry Pi Software Configuration Tool:

sudo raspi-config

Once the Raspberry Pi Software Configuration tool had started, follow through the following screens to generate the "en_US.UTF-8" locale and set it as the default locale for the system environment:

raspi-config Raspbian Stretch Lite 20171129 with Localisation Options selected

raspi-config Raspbian Stretch Lite 20171129 with localisation options change locale selected

raspi-config Raspbian Stretch Lite screen for generating en-us utf-8 locale

raspi-config Raspbian Stretch Lite setting en-us utf-8 as default locale for system environment

After doing so, open up /etc/default/locale with nano:

sudo nano /etc/default/locale

And update the contents to look like the following:

LANG=en_US.UTF-8
LC_ALL=en_US.UTF-8
LANGUAGE=en_US.UTF-8

This will ensure that the locale settings for Raspbian Stretch Lite persist through system reboots.

Changing Timezone

The default timezone of Raspbian Stretch Lite is "Etc/UTC". However, if your Raspberry Pi 3 is not residing in that Timezone, you may want to change it.

For example, to change the Timezone on Raspbian Stretch Lite to Singapore's Timezone, use the Raspberry Pi Software Configuration Tool mentioned earlier and go through the following steps:

raspi-config Raspbian Stretch Lite 20171129 with Localisation Options selected

raspi-config Raspbian Stretch Lite 20171129 with Change Timezone selected

raspi-config Raspbian Stretch Lite 20171129 with Asia selected

raspi-config Raspbian Stretch Lite 20171129 with Singapore selected

Once you had configured the Timezone, restart Raspbian Stretch Lite with the following command:

sudo shutdown -r now

Get back into Raspbian Stretch Lite with the following command once it had restarted successfully:

ssh pi@192.168.1.109

Checking if Python 3 is included in Raspbian Stretch Lite by default

Raspbian Stretch Lite includes Python 3 by default. We can verify that by running the following command:

dpkg --get-selections | grep python3

which returns the following output:

libpython3-stdlib:armhf				install
libpython3.5:armhf				install
libpython3.5-minimal:armhf			install
libpython3.5-stdlib:armhf			install
python3						install
python3-apt					install
python3-minimal					install
python3.5					install
python3.5-minimal				install

Installing Python 3 package manager (pip3) on Raspbian Stretch Lite

This version of Raspbian Stretch Lite did not install the Python 3 Package Manager by default. The Python 3 package manager will be useful for managing the dependencies for your Python 3 applications. As such, proceed to install the Python 3 Package Manager on Raspberry Stretch Lite.

To install the Python 3 Package Manager, run the following command:

sudo apt-get install python3-pip -y

Once the Python 3 package manager is successfully installed, you can run the following command to see the Python 3 Packages that are available in the system:

pip3 list

Running the command can show output similar to the following:

cryptography (1.7.1)
idna (2.2)
keyring (10.1)
keyrings.alt (1.3)
pip (9.0.1)
pyasn1 (0.1.9)
pycrypto (2.6.1)
pygobject (3.22.0)
python-apt (1.1.0b5)
pyxdg (0.25)
SecretStorage (2.3.1)
setuptools (33.1.1)
six (1.10.0)
wheel (0.29.0)

Installing Virtualenv on Raspbian Stretch Lite

Virtualenv is a tool that allows us to create isolated environments in the same machine. This is useful if you have to run multiple applications with conflicting python dependencies on your Raspberry Pi 3.

To install Virtualenv, I run the following command:

sudo apt-get install virtualenv -y

Buying your Raspberry Pi 3 components from Amazon

If you do not have the Raspberry Pi 3 components mentioned in this post yet, you may want to purchase them from Amazon. Simply click on the button below to add the Raspberry Pi 3 components to your cart. You may remove anything that you already have or replace some of the components with other components.


Advertisements

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people.

Udemy.com Homepage 300x250
Advertisements