Check Internet Speed with speedtest-cli on Debian and Ubuntu

Internet connection speed is something that we always check at our homes and offices. The most common method that we use is by visiting a speed test website like speedtest.net. On that site, a javascript application is loaded in the web browser which selects the best (nearby) server based on the ping time and then the speed test results for that server are shown. Speedtest.net also uses a flash player to produce the result graphically.

The problem using these web based speed tests is that it does not allow you to schedule the speed test at regular intervals, e.g. as a cronjob and you can't use them on headless servers. One application that can solve this problem is “speedtest-cli”. This application allows you to check your internet speed using the command line.

What is speedtest-cli?

Speedtest-cli is a script written in the Python programming language which measures the internet speed bidirectionally. This application allows you to check your internet speed upon distance in km, it can test against specific servers and it also provides a URL so that you can share your result on the internet.

How to install speedtest-cli on Linux?

In order to install the speedtest-cli application, you must have a Python 2.4-3.4 or an upgraded version installed in your system.

You can install the speedtest-cli tools using two methods. The first method includes the use of python-pip package manager, the second method is to download the Python script manually, make it executable and then run the application.

Method 1: Installing speedtest-cli using python-pip

The next step has to be run as root user, as I don't want to prepend sudo for each command, please run the following command on Ubuntu first to become root user:

sudo su

1. The first step is to make sure your list of packages from all Ubuntu repositories and PPA's is up to date by running the following command:

apt-get update

Sample output:

apt-get update result.

2. Next step is to install the pip package manager on your system. As you can see, the command will install many dependencies as well.

apt-get install python-pip

The following NEW packages will be installed:
binutils build-essential dpkg-dev fakeroot g++ g++-4.8 gcc gcc-4.8
libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl
libasan0 libatomic1 libc-dev-bin libc6-dev libdpkg-perl libfakeroot
libfile-fcntllock-perl libgcc-4.8-dev libgomp1 libitm1 libquadmath0
libstdc++-4.8-dev libtsan0 linux-libc-dev make manpages-dev
python-chardet-whl python-colorama python-colorama-whl python-distlib
python-distlib-whl python-html5lib python-html5lib-whl python-pip
python-pip-whl python-requests-whl python-setuptools python-setuptools-whl
python-six-whl python-urllib3-whl python-wheel python3-pkg-resources
The following packages will be upgraded:
libc6
1 upgraded, 43 newly installed, 0 to remove and 6 not upgraded.
Need to get 6,698 kB/40.6 MB of archives.
After this operation, 102 MB of additional disk space will be used.
Do you want to continue? [Y/n] 

Press Y to continue   

Sample output:

Installing pip.

3.  After installing the package, you can install the application using this pip command:

pip install speedtest-cli

Downloading/unpacking speedtest-cli
Downloading speedtest_cli-0.3.4-py2.py3-none-any.whl
Installing collected packages: speedtest-cli
Successfully installed speedtest-cli
Cleaning up...

4. If you want to upgrade the speedtest-cli application in future, you can use this command:

pip install speedtest-cli –-upgrade

Requirement already up-to-date: speedtest-cli in /usr/local/lib/python2.7/dist-packages
Cleaning up...

Method 2: Installing the latest speedtest-cli manually from GitHub

1. First you have to download the python script from GitHub using the command

cd /tmp
wget https://github.com/sivel/speedtest-cli/archive/master.zip

Resolving github.com (github.com)... 192.30.252.130
Connecting to github.com (github.com)|192.30.252.130|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://codeload.github.com/sivel/speedtest-cli/zip/master [following]
--2016-02-19 11:25:34-- https://codeload.github.com/sivel/speedtest-cli/zip/mas ter
Resolving codeload.github.com (codeload.github.com)... 192.30.252.161
Connecting to codeload.github.com (codeload.github.com)|192.30.252.161|:443... c onnected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/zip]
Saving to: ‘master.zip’
[ <=> ] 17,653 --.-K/s in 0.006s
2016-02-19 11:25:34 (2.72 MB/s) - ‘master.zip’ saved [17653]

2. The second step is to unpack the file that you have downloaded (master.zip) and extract it using the unzip command:

unzip master.zip

Archive: master.zip
7b09d8759fcbbda8a71aa49a4eaa825581b07439
creating: speedtest-cli-master/
inflating: speedtest-cli-master/.gitignore
inflating: speedtest-cli-master/.travis.yml
inflating: speedtest-cli-master/CONTRIBUTING.md
inflating: speedtest-cli-master/LICENSE
inflating: speedtest-cli-master/MANIFEST.in
inflating: speedtest-cli-master/README.rst
extracting: speedtest-cli-master/setup.cfg
inflating: speedtest-cli-master/setup.py
inflating: speedtest-cli-master/speedtest-cli.1
inflating: speedtest-cli-master/speedtest_cli.py
inflating: speedtest-cli-master/tox.ini

3. When you have extracted the file, make the extracted script executable by going to the extracted directory and using the chown command.

cd speedtest-cli-master/

chmod 755 speedtest_cli.py

4. To avoid typing the full path every time, move the executable file to /usr/local/bin by using the following command:

sudo mv speedtest_cli.py /usr/local/bin/speedtest-cli

How to test internet connection speed with speedtest-cli?

Run the speedtest-cli command to test the download and upload speed of your internet connection without any argument:

speedtest-cli

Testing download speed........................................
Download: 916.39 Mbit/s
Testing upload speed..................................................
Upload: 489.57 Mbit/s

If you want to check the speed result in bytes instead of bits, then use this command:

speedtest-cli --bytes

Testing download speed........................................
Download: 114.15 Mbyte/s
Testing upload speed..................................................
Upload: 49.61 Mbyte/s

If you want to share the speed result with your friends, then you can download the image of the result with URL provided after the result.

speedtest-cli --share

Download: 900.64 Mbit/s
Testing upload speed..................................................
Upload: 445.14 Mbit/s
Share results: http://www.speedtest.net/result/5100016286.png

Below is the picture of my speed test result from the test above.

Speedtest result.

If you want information only on ping, download and upload then use:

speedtest-cli --simple

Ping: 5.23 ms
Download: 877.56 Mbit/s
Upload: 162.21 Mbit/s

You can also check the list of server based upon the distance in km by using:

speedtest-cli --list

2628) Telstra (Adelaide, Australia) [17098.98 km]
4051) Vodafone Hutchison Australia (AE) (Adelaide, Australia) [17098.98 km]
...........................................................................
3254) Vodafone Hutchison Australia (PH) (Perth, Australia) [18700.26 km]
1931) IX Australia (Perth, Australia) [18700.26 km]

List of Speedtest servers.

By searching the list in terms of distance, you might have generated a big list of the servers. If you want to check the result for servers of a specific area, then you can check that by piping the result to the grep command (Australia is an example of the area):

speedtest-cli --list | grep –i Australia

2604) Telstra (Brisbane, Australia) [15501.92 km]
1594) 'Yes' Optus (Brisbane, Australia) [15501.92 km]
...............................................................................................
3254) Vodafone Hutchison Australia (PH) (Perth, Australia) [18700.26 km]
1931) IX Australia (Perth, Australia) [18700.26 km]

Speedtest results for Australia.

To test the connection speed against a specific server, you have to use the server ID generated in step 5 and 6.

speedtest-cli --server [server ID]

speedtest-cli --server 2604

Here server ID 2604 is used as an example:

Hosted by Telstra (Brisbane) [15501.92 km]: 452.614 ms
Testing download speed........................................
Download: 28.79 Mbit/s
Testing upload speed..................................................
Upload: 11.71 Mbit/s

If you want to check the help and version number of the speedtest-cli tool, use these commands:

speedtest-cli --version

0.3.4

speedtest-cli --help

usage: speedtest_cli [-h] [--bytes] [--share] [--simple] [--list]
[--server SERVER] [--mini MINI] [--source SOURCE]
[--timeout TIMEOUT] [--secure] [--version]
Command line interface for testing internet bandwidth using speedtest.net.
--------------------------------------------------------------------------
https://github.com/sivel/speedtest-cli
optional arguments:
-h, --help show this help message and exit
--bytes Display values in bytes instead of bits. Does not affect
the image generated by --share
--share Generate and provide a URL to the speedtest.net share
results image
--simple Suppress verbose output, only show basic information
--list Display a list of speedtest.net servers sorted by
distance
--server SERVER Specify a server ID to test against
--mini MINI URL of the Speedtest Mini server
--source SOURCE Source IP address to bind to
--timeout TIMEOUT HTTP timeout in seconds. Default 10
--secure Use HTTPS instead of HTTP when communicating with
speedtest.net operated servers
--version Show the version number and exit

Creating an internet speed log with speedtest-cli

Speedtest-cli can be run as cronjob to get an internet speed test log. Open the crontab with this command:

crontab -e

And add this line (I'll assume that speedtest_cli.py is installed in /usr/local/bin) to the crontab:

30 00 * * *  /usr/local/bin/speedtest-cli >> /tmp/speedlog.txt

Save the changes and exit the crontab editor. This will run a speed test at 0:30 each day and appends the result to the file /tmp/speedlog.txt.

Conclusion

This application is lightweight, easy to use and runs without any issue making it a must have tool for developers and system administrators.

 

Share this page:

9 Comment(s)

Add comment

Comments

From: tpeterson

Thanks for this tutorial, but running "speedtest_cli.py" in a terminal did not work for me. After researching this a bit, I found that simply running "speedtest-cli" works flawlessly. Love that I can run a speed test in a terminal!

From: till

Thank you for the notice. The command was differently indeed depending on the installation method. I've updated the tutorial so that the command is the same when you install it with pip or manually.

From: Luis M. Prous

There is a little error in the pip upgrade method... it says "-upgrade" and it must be "--upgrade" at the end of the line.

From: Jan Fikar

to share the image, you need the switch --share

From: maroman

there is misspelling error in the command sudo mv speedtest_cli.py /usr/local/bin/peedtest-cli (should be sudo mv speedtest_cli.py /usr/local/bin/speedtest-cli).

From: Cem

# speedtest-cli 

Retrieving speedtest.net configuration...

Retrieving speedtest.net server list...

Traceback (most recent call last):

  File "/usr/local/bin/speedtest-cli", line 796, in ?

    main()

  File "/usr/local/bin/speedtest-cli", line 790, in main

    speedtest()

  File "/usr/local/bin/speedtest-cli", line 644, in speedtest

    print_('Testing from %(isp)s (%(ip)s)...' % config['client'])

  File "/usr/local/bin/speedtest-cli", line 146, in print_

    write(arg)

  File "/usr/local/bin/speedtest-cli", line 111, in write

    fp.write(data)

UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 14: ordinal not in range(128)

 

From: bhimraya ssjjan

Thanks for this tutorial, speedtest_cli.py its working fine,but its shows diffrent result compare to browser speed test   

From: paul

wow all that just for the program to tell me that speedtest-cli is replaced with speedtest.py

so this whole post is obsolete now please remove it, or update the instructions to the correct packages

From: paul

please combine this with my other post. Here is the results after doing all these steps:

~ $ speedtest-cli

The file speedtest_cli.py has been deprecated in favor of speedtest.py

and is available for download at:

 

https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py

sorry that i did not add this to my first post